x64 porting: about type casting

Discussion in 'Windows Vista Drivers' started by Morris Chang, Dec 9, 2005.

  1. Morris Chang

    Morris Chang Guest

    Dear all:

    I am porting another driver to X64 platform.

    1. ULONG to PUCHAR
    2. pointer truncation from PUCHAR to ULONG


    I use the original DDK 3790.1830 Server2003 free x86 to build
    is fine.
    But it don't work for Server2003 x64.

    Anybody know how to do ?


    Thanks
     
    Morris Chang, Dec 9, 2005
    #1
    1. Advertisements

  2. is the ULONG a register value? if so, the cast should be fine as long as
    you are passing the value into a register read/write routine For #2, you
    are definitely truncating data and 99% of the time you will lose data.

    can you give specific code samples which demonstrate what you are trying to
    do ?

    d
     
    Doron Holan [MS], Dec 9, 2005
    #2
    1. Advertisements

  3. Morris Chang

    Morris Chang Guest

    1.
    error C4312: 'type cast' : conversion from 'ULONG' to 'PUCHAR' of
    greater size

    fdoExtension->ControllerAddress.Status
    =
    (PUCHAR)(partial->u.Port.Start.LowPart);

    partial->u.Port.Start.LowPart is not a register value.

    2.
    error C4311: 'type cast' : pointer truncation from 'PUCHAR' to 'ULONG'
    ((partial->u.Port.Start.LowPart & 0xFFFFFFF8) !=
    (ULONG)TranslatedConfigBase)
     
    Morris Chang, Dec 9, 2005
    #3
  4. Morris Chang

    Ian Blake Guest

    Use ULONG_PTR not ULONG

    On 32 bit system

    Pointers 32bit
    ULONG 32bit
    ULONG_PTR 32bit

    On 64 bit system

    Pointers 64 bit
    ULONG still 32bit
    ULONG_PTR 64 bit
     
    Ian Blake, Dec 9, 2005
    #4
  5. You need an intermediate cast to bring the value to the right size:

    (PUCHAR)(ULONG_PTR)(partial->u.Port.Start.LowPart);

    The cast to ULONG_PTR brings the size up to 64-bits, allowing the cast to
    PUCHAR to work properly.

    It's hard to tell if what you're trying to do here is OK. Be very weary
    about truncation casts, as they are likely the wrong thing to do.
    Again, you need an intermediate cast:

    ((partial->u.Port.Start.LowPart & 0xFFFFFFF8) !=
    (ULONG)(ULONG_PTR)TranslatedConfigBase)

    Note that a truncating cast like this is likely the wrong things to do...
    How about comparing partial->u.Port.Start.QuadPart?

    - Fab
     
    Fabian Tillier, Dec 10, 2005
    #5
  6. Hello,

    See the "64-bit issues" in the Windows DDK docs. Especially the articles
    "Porting Issues Checklist" and "The New Data Types".
     
    Vladimir Zinin, Dec 12, 2005
    #6
  7. Replace such ULONGs with ULONG_PTRs.
     
    Maxim S. Shatskih, Dec 13, 2005
    #7
  8. Morris Chang

    Morris Chang Guest

    Thank you all. The problem has been solved.
     
    Morris Chang, Dec 13, 2005
    #8
  9. Morris Chang

    Chris Doré Guest

    How? What did you do? It's always nice to share.


    Chris
     
    Chris Doré, Dec 13, 2005
    #9
  10. Morris Chang

    Morris Chang Guest

    Just like the way Fabian Tillier and Maxim said:


    Replace such ULONGs with ULONG_PTRs.
     
    Morris Chang, Dec 14, 2005
    #10
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.