1 year ago

#73158

test-img

Victor

How to receive WdfDevicePostEvent 's notify events in C# application

https://learn.microsoft.com/en-us/windows-hardware/drivers/ddi/wdfdevice/nf-wdfdevice-wdfdevicepostevent

I have an WDF driver use WdfDevicePostEvent() to notify WdfEventBroadcast type event, and have another application build in C# to use WdfDevicePostEvent() receive it, but nothing received.

Driver:

         str = 11;
    Status = WdfDevicePostEvent(Device, &GUID_DEVINTERFACE_BIOMETRIC_READER, WdfEventBroadcast, &str, sizeof(str));
    if (Status == STATUS_SUCCESS) {
        OmsWriteLog("+++***WdfDevicePostEvent ***+++");
    }

Application:

 public const int DbtDevicearrival = 0x8000; // system detected a new device        
        public const int DbtDeviceremovecomplete = 0x8004; // device is gone      
        public const int Dbtcustom = 0x8006; // device is gone  
        public const int WmDevicechange = 0x0219; // device change event      
        private const int DbtDevtypDeviceinterface = 5;
        private static readonly Guid GuidDevinterfaceUSBDevice = new Guid("e2b5183a-99ea-4cc3-ad6b-80ca8d715b80"); // My devices


 public static void RegisterUsbDeviceNotification(IntPtr windowHandle)
        {
            DevBroadcastDeviceinterface dbi = new DevBroadcastDeviceinterface
            {
                DeviceType = DbtDevtypDeviceinterface,
                Reserved = 0,
                ClassGuid = GuidDevinterfaceUSBDevice,
                Name = 0
            };

            dbi.Size = Marshal.SizeOf(dbi);
            IntPtr buffer = Marshal.AllocHGlobal(dbi.Size);
            Marshal.StructureToPtr(dbi, buffer, true);

            notificationHandle = RegisterDeviceNotification(windowHandle, buffer, 0);
        }

protected override void WndProc(ref Message m)
        {
        base.WndProc(ref m);
        //Log("WndProc");
        if(m.Msg== 0x8006)
        {
            Log("0x8006");
        }
        if (m.Msg == UsbNotification.WmDevicechange)
        {
            m.
            Log("WmDevicechange");
            switch ((int)m.WParam)
            {
                case UsbNotification.DbtDeviceremovecomplete:
                    Log("Usb_DeviceRemoved");                       
                    break;
                case UsbNotification.DbtDevicearrival:
                    Log("Usb_DeviceAdded");
                    break;
                case UsbNotification.Dbtcustom:
                    Log("0x8006");
                    break;
            }
        }
    }

Note. I am able to receive USB WmDevicechange events via RegisterDeviceNotification()

wdf

0 Answers

Your Answer

Accepted video resources