none
Eject USB device from PnP notify callback RRS feed

  • Question

  • Hi,

    I've got a Windows Service that's listening for PnP events:

        HCMNOTIFICATION hcmCtx;
        CM_NOTIFY_FILTER cnf;

        cnf.cbSize = sizeof(CM_NOTIFY_FILTER);
        cnf.Flags = 0;    // TODO Check this if we're into trouble
        cnf.FilterType = CM_NOTIFY_FILTER_TYPE_DEVICEINTERFACE;
        cnf.Reserved = 0;
        cnf.u.DeviceInterface.ClassGuid = GUID_DEVINTERFACE_USB_DEVICE;

        CM_Register_Notification(
            &cnf,
            NULL,
            PcmNotifyCallback,
            &hcmCtx);

    When I plug in a USB drive, my callback function PcmNotifyCallback is called, and I can open the device with CreateFile:

    DWORD CALLBACK PcmNotifyCallback(
        _In_ HCMNOTIFICATION hNotify,
        _In_opt_ PVOID context,
        _In_ CM_NOTIFY_ACTION action,
        _In_reads_bytes_(EventDataSize) PCM_NOTIFY_EVENT_DATA eventData,
        _In_ DWORD eventDataSize)
    {
        HANDLE hVolume;
        hVolume = CreateFile(
            eventData->u.DeviceInterface.SymbolicLink,
            GENERIC_READ | GENERIC_WRITE,
            FILE_SHARE_READ | FILE_SHARE_WRITE,
            NULL,
            OPEN_EXISTING,
            0,
            NULL);
    }

    However, I can't perform any of the storage IOCTLs from that HANDLE. I've tried all of the following and neither of them works (DeviceIoControl returns FALSE).

        - IOCTL_STORAGE_GET_DEVICE_NUMBER

        - FSCTL_LOCK_VOLUME

        - FSCTL_DISMOUNT_VOLUME

        - IOCTL_STORAGE_MEDIA_REMOVAL

        - IOCTL_STORAGE_EJECT_MEDIA

    What I want to do is to eject some USB devices as soon as they arrive. I've seen many programs on the Internet that do this, but most of them (all?) take a drive letter. But when my callback is called what I get is a symlink to the device, something like: "\\?\USB#VID_090C&PID_1000#1111111111111111111111#{xxxxxxxxxxxxxxxxxxxxxxxxxxxx}". Is there any way to accomplish what I want in this context (PnP callback)?

    Thursday, September 24, 2015 3:23 PM

Answers

  • GUID_DEVINTERFACE_USB_DEVICE is a generic usb device interface, it is not a storage interface. all of the IOCTLs you list are for a disks and volumes. When you say "eject", what specifically do you mean in the context of a generic usb device? that it is not started // does not work?

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

    Thursday, September 24, 2015 4:31 PM

All replies

  • GUID_DEVINTERFACE_USB_DEVICE is a generic usb device interface, it is not a storage interface. all of the IOCTLs you list are for a disks and volumes. When you say "eject", what specifically do you mean in the context of a generic usb device? that it is not started // does not work?

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

    Thursday, September 24, 2015 4:31 PM
  • GUID_DEVINTERFACE_USB_DEVICE is a generic usb device interface, it is not a storage interface. all of the IOCTLs you list are for a disks and volumes. When you say "eject", what specifically do you mean in the context of a generic usb device? that it is not started // does not work?

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

    What I mean with "eject" is the same effect as "safely removing a USB drive". That's what I want to achieve programmatically. As I said, there are thousands of programs out there that do that. But I want to be notified whenever a mass storage USB drive is plugged in. I'm not really sure whether the context in which I should do this is that of a generic USB device. Maybe I'm in a too low level? You say that GUID_DEVINTERFACE_USB_DEVICE is a generic USB device, not a storage one. Maybe I should listen for other more specific kind GUID? If so, which GUIDs are available?

    Thanks.

    Friday, September 25, 2015 12:01 PM
  • guid_devinterface_disk would represent all disks. from that instance you can query the storage type and filter on usb. you request safely removing a usb drive using CM APIs, you don't need to send an IOCTL or open a handle.

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

    Friday, September 25, 2015 5:15 PM