C2220 & 32bit app on 64 bit driver

Discussion in 'Windows Vista Drivers' started by Prafulla, Mar 9, 2010.

  1. Prafulla

    Prafulla Guest

    Hi,

    During the driver build, I am getting the C2220 warning as error on WDK-6000
    build environment, how to disable this warning.

    I am supporting 32 bit app on 64 bit drivers, so I am passing IOCTL
    variables in form of ULONG32 & ULONG_PTR, so that, width of these variable
    will be always same in 32/64 bit process.
    I am also using WdfRequestIoIs32bitProcess where ever required, but avoided
    for most of IOCTl's by abpve method.

    IOCTL struct ==> ULONG_PTR var
    Driver output ==> void * pointer

    var = (ULONG_PTR)pointer
    Above line causing the C2220 warning.

    In the sources file, there is no /wx flag added, I guess it is taken by
    default in WDK-6000.

    Is there any way to disable this?, I tried Pragma, but not working.

    Thanks,
    Kota
     
    Prafulla, Mar 9, 2010
    #1
    1. Advertisements

  2. I am supporting 32 bit app on 64 bit drivers, so I am passing IOCTL
    Do you have pointers or handles in the IOCTL structures? If not - then forget all 32/64 issues and use usual ULONG-style types.

    If not so - then make and #ifdef _WIN64 in the header file, and, in this #ifdef, declare the same structure, but with something like:

    VOID *POINTER_32 hEvent;

    for all handles and pointers in it. The new structure should be declared with the name postfixed with "32", or like this.

    The apps are the built without paying attention to this, they just use the usual structure type name.

    In the driver, use WdfRequestIoIs32bitProcess in this IOCTL path, and interpret the IOCTL's buffer either as usual structure type or as "32" structure type. Just sign-extend the pointer and handle values (like (PVOID)(ULONG_PTR)(ULONG)) from the "32" structure in this case.

    Surely this is only for 64bit driver build, 32bit driver build just blindly uses the usual structure type.

    I think all of this is well-covered in MSDN.
    I cannot understand what these things mean.
     
    Maxim S. Shatskih, Mar 9, 2010
    #2
    1. Advertisements

  3. another warning. Second, you state that you pass things in the form of
    Correct.

    I would never use ULONG_PTR (or size_t which is another name of it) in IOCTL buffer.

    If the numeric value is needed - ULONG or ULONGLONG are much better, and pointers should be used as pointers (as PVOID in the worst case).

    But, in general C/C++ code, I would use size_t whenever possible for all sizes and pointer offsets and differences. If necessary, size_t is downcasted to ULONG exactly near the DeviceIoControl call, and near most Win32 or NT kernel calls (historically they do not use size_t).
     
    Maxim S. Shatskih, Mar 9, 2010
    #3
  4. size_t is 64 bit in x64 build.

    Correct.

    I would never use ULONG_PTR (or size_t which is another name of it) in IOCTL
    buffer.

    If the numeric value is needed - ULONG or ULONGLONG are much better, and
    pointers should be used as pointers (as PVOID in the worst case).

    But, in general C/C++ code, I would use size_t whenever possible for all
    sizes and pointer offsets and differences. If necessary, size_t is
    downcasted to ULONG exactly near the DeviceIoControl call, and near most
    Win32 or NT kernel calls (historically they do not use size_t).
     
    Alexander Grigoriev, Mar 9, 2010
    #4
  5. Another name of ULONG_PTR, as I mentioned.
     
    Maxim S. Shatskih, Mar 9, 2010
    #5
  6. Prafulla

    Kota Guest

    Hi,

    Thanks for your inputs.
    I have used the "PtrToUlong" function for coverting the void * pointer to my
    datatype ULONG_PTR, it avoided the C2220 error and working fine.

    I am using the below typedef to replace void pointer in my IOCTL structures
    and replaced the ULONG to ULONG32 in my structures.
    All os my IOCTL structures variables are void * or ULONG32 size variables.

    So that, I am making sure that, this same structure works fine for a 64 bit
    driver for both 32 & 64 bit process, I also ensured that, size of structure
    is matching fine in both 32 & 64 bit process at driver level before handling
    the IOCTL in driver.

    #ifndef ULONG_PTR
    typedef ULONG32 MY_ADDR;
    #else
    typedef ULONG_PTR MY_ADDR;
    #endif
    Here ULONG_PTR is 4 bytes to hold a pointer address in 32 bit app & it is 8
    bytes to hold pointer address in 64 bit app.

    With this, I have not used the WdfRequestIoIS32bitProcess to diffrentiate
    and all are working fine.

    Please let me know, if you see any issues in this logic.

    Thanks,
    Kota
     
    Kota, Mar 10, 2010
    #6
  7. Here ULONG_PTR is 4 bytes to hold a pointer address in 32 bit app & it is 8
    This conradicts what you said above about the structure size "matching fine". It does not.

    If you want to simplify the driver - then replace all pointers in the structures with ULONGLONGs (8byte on all platforms), and, in 32bit apps, cast (ULONGLONG)(ULONG)Ptr.

    In the 32bit version of the driver, you will need to cast down.
     
    Maxim S. Shatskih, Mar 10, 2010
    #7
    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.