Get WDFDevice from a functional PDEVICE_OBJECT in NDIS Miniport driver IRP_MJ_DEVICE_CONTROL callback function

Lokesh Prajapati 61 Reputation points
2021-06-07T13:31:49.22+00:00

TL;DR: How do I get device context structure inside IRP_MJ_DEVICE_CONTROL callback function for an NDIS Miniport driver.

I am writing an NDIS Miniport drivers based on "netvmini" in the Windows Driver Samples.
I am allocating a context structure to a WDFDevice I get from WdfDeviceMiniportCreate() call. I get FunctionalDeviceObject, NextDeviceObject, and PhysicalDeviceObject to pass into WdfDeviceMiniportCreate() from NdisMGetDeviceProperty() call.

I have found WdfWdmDeviceGetWdfDeviceHandle() function that I can call from the IRP_MJ_DEVICE_CONTROL callback function to get the WDFDevice and then get device context from WDFDevice. But according to https://learn.microsoft.com/en-us/windows-hardware/drivers/ddi/wdfdevice/nf-wdfdevice-wdfwdmdevicegetwdfdevicehandle, I can't use this function: "the structure cannot represent any of the WDM device objects that the driver specified in a previous call to WdfDeviceMiniportCreate."

Other option I have is to use fDeviceObject->DeviceExtension member. But I don't get a chance to set up this DeviceExtension since I get fDeviceObject from NdisMGetDeviceProperty() call.

How else can I get device context in IRP_MJ_DEVICE_CONTROL callback function?

Windows Hardware Performance
Windows Hardware Performance
Windows: A family of Microsoft operating systems that run across personal computers, tablets, laptops, phones, internet of things devices, self-contained mixed reality headsets, large collaboration screens, and other devices.Hardware Performance: Delivering / providing hardware or hardware systems or adjusting / adapting hardware or hardware systems.
1,546 questions
0 comments No comments
{count} votes

Accepted answer
  1. Doron Holan 1,801 Reputation points
    2021-06-08T17:19:36.773+00:00

    You can store the WDFDEVICE in a reserved extension. From the docs for NdisRegisterDeviceEx

    https://learn.microsoft.com/en-us/windows-hardware/drivers/ddi/ndis/nf-ndis-ndisregisterdeviceex

    If an NDIS driver requires space for context information in the device object, the driver can pass a nonzero value for the ExtensionSize member in the NDIS_DEVICE_OBJECT_ATTRIBUTES structure at the DeviceObjectAttributes parameter. In this case, NDIS allocates the extension for the driver, and the driver can call the NdisGetDeviceReservedExtension function to get a pointer to the extension.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful