What kind of variable type should I use to get the virtual address given by WDF? RRS feed

  • Question

  • typedef struct _DEVICE_CONTEXT
    	WDFDEVICE                       Device;
    	PVOID				MemBaseAddress;
    	ULONG				MemLength;
    pDeviceContext->MemBaseAddress = MmMapIoSpace(
    pDeviceContext->MemLength = descriptor->u.Memory.Length;

    I defined a structure including MemBaseAddress with type PVOID,because MmMapIoSpace() returns a PVOID value.

    In my IO callbacks,I need to add an offset address(0x20000) to the MemBaseAddress.

    So what kind of variable type should I use to get the virtual address given by WDF?

    In other words,in following sentences,which one do I need to choose?(My PC OS is Windows 32bits )

    PUCHAR      pRAM;//??? 
    PVOID       pRAM;//??? 
    PUSHORT     pRAM;//??? 
    PULONG      pRAM;//??? 
    pRAM = pDeviceContext->MemBaseAddress; 
    pRAM += 0x20000;

    Do I need to use PULONG?

    Wednesday, May 6, 2015 6:54 AM


All replies

  • You need to think about how you are going to reference the data.  Is this offset in memory a structure, an array of one of the base types (UCHAR, USHORT, ULONG, etc.) or what?   Assume it is some type you define as MYTYPE then the calculation becomes:

          MTYPE *pRAM;

          pRAM = (MYTYPE *)(((PUCHAR) pDeviceContext->MemBaseAddress) + 0x20000);

    The only reason to have PUCHAR in there is so that 0x20000 which one presumes is a byte offset is properly added to the MemBaseAddress.   Just change MTYPE to whatever type of reference you need for your device.

    Don Burn Windows Driver Consulting Website:

    Wednesday, May 6, 2015 10:00 AM
  • WDF provides a utility function that does the addition for you, see WDF_PTR_ADD_OFFSET you can then use whatever type you like, so for instance PFOO pf = (PFOO) WDF_PTR_ADD_OFFSET(pDeviceContext->MemBaseAddress, 0x20000);

    d -- This posting is provided "AS IS" with no warranties, and confers no rights.

    Wednesday, May 6, 2015 4:42 PM
  • Thanks Burn.
    Thursday, May 7, 2015 2:25 AM
  • Thanks Holan
    Thursday, May 7, 2015 2:25 AM