question

LokeshPrajapati-5114 avatar image
0 Votes"
LokeshPrajapati-5114 asked LokeshPrajapati-5114 commented

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

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://docs.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-hardwarewindows-hardware-code-ndis
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

DoronHolan avatar image
1 Vote"
DoronHolan answered LokeshPrajapati-5114 commented

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

https://docs.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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Perfect, thank you. I was using the DriverObject's dispatch table which was causing all the problem. This function solved all the probles.

0 Votes 0 ·