none
Why EvtDriverDeviceAdd will be always called once even without any physical device? RRS feed

  • Question

  • I am learning to develop Windows Driver, so I have written a very simple driver using KMDF.

    #include <ntddk.h>
    #include <wdf.h>
    
    EVT_WDF_DRIVER_UNLOAD DriverUnload;
    NTSTATUS DeviceAdd(WDFDRIVER Driver, PWDFDEVICE_INIT DeviceInit);
    
    NTSTATUS DriverEntry(PDRIVER_OBJECT driver, PUNICODE_STRING reg_path) {
    	KdBreakPoint();
    	WDF_DRIVER_CONFIG config;
    	NTSTATUS status = STATUS_SUCCESS;
    	WDFDRIVER wdfdriver;
    	
    	KdPrint(("Driver loading!\n"));
    	WDF_DRIVER_CONFIG_INIT(&config, DeviceAdd);
    	config.EvtDriverUnload = DriverUnload;
    	WdfDriverCreate(driver, reg_path, NULL, &config, &wdfdriver);
    	return status;
    }
    
    NTSTATUS DeviceAdd(WDFDRIVER Driver, PWDFDEVICE_INIT DeviceInit) {
    	Driver = Driver; /*keep it happy*/
    	DeviceInit = DeviceInit;/*keep it happy*/
    	KdPrint(("One Device added\n"));
    	return STATUS_SUCCESS;
    }
    
    _Use_decl_annotations_ VOID DriverUnload(WDFDRIVER Driver) {
    	Driver = Driver;/*keep it happy*/
    	KdPrint(("Driver Unload!\n"));
    	return;
    }

    I have installed it on testing virtual machine, and windbg outputs:

    Driver loading!
    One Device added
    Driver Unload!

    From EvtDriverDeviceAdd doc , it says

    The framework calls your driver's EvtDriverDeviceAdd callback function after a bus driver detects a device that has a hardware identifier (ID) that matches a hardware ID that your driver supports. 

    So my questions are:

    1. There is no physical device, why EvtDriverDeviceAdd is called?

    2. What does the device created at the first time stand for?

    3. Does every driver have at least one device(even virtual)?  If so, what if I use WDM without any call to IoCreateDevice?

    I have read docs about device stack and codes of the toaster sample in WDK, but my mind is still in a mess.





    • Edited by MioMate Friday, February 2, 2018 11:53 AM Add description
    Friday, February 2, 2018 9:26 AM

Answers

All replies

  • EvtDeviceAdd will not be called without a physical device or a logical device.

    EvtDeviceAdd is there the call to create a WDFDEVICE which is the KMDF wrapper for a Windows DEVICE_OBJECT see https://docs.microsoft.com/en-us/windows-hardware/drivers/kernel/introduction-to-device-objects    Every driver has at least one device object, without a device object there is no way to reference of call the driver.


    Don Burn Windows Driver Consulting Website: http://www.windrvr.com

    • Marked as answer by MioMate Friday, February 2, 2018 2:24 PM
    Friday, February 2, 2018 11:57 AM
  • Thanks for your answer! I am much clear now.

    By the way, if I want to write a driver for a physical device, how can I make sure that I create a DEVICE_OBJECT for the physical device, not any logical device? Is it relavant to the Hardware ID defined in the INF file?

    Friday, February 2, 2018 2:23 PM
  • Yes you want to have the Hardware ID defined in the INF file.


    Don Burn Windows Driver Consulting Website: http://www.windrvr.com

    Friday, February 2, 2018 2:26 PM