使用哪个代码片段来处理USB设备插入

Which code fragment to use to handle USB device insertion?

本文关键字:USB 处理 插入 片段 代码      更新时间:2023-10-16

应用程序可以使用RegisterDeviceNotification函数来注册以接收来自系统的通知消息。这个例子展示了如何注册设备接口的事件通知,这些设备接口是接口类的成员,其GUID传递给函数。

在这个例子中,我应该使用哪个代码片段来处理USB设备插入消息?

搜索DBT_DEVICEARRIVAL:这是USB设备插入消息处理的地方。

 case WM_DEVICECHANGE:
{
    //
    // This is the actual message from the interface via Windows messaging.
    // This code includes some additional decoding for this particular device type
    // and some common validation checks.
    //
    // Note that not all devices utilize these optional parameters in the same
    // way. Refer to the extended information for your particular device type 
    // specified by your GUID.
    //
    PDEV_BROADCAST_DEVICEINTERFACE b = (PDEV_BROADCAST_DEVICEINTERFACE) lParam;
    TCHAR strBuff[256];
    // Output some messages to the window.
    switch (wParam)
    {
    case DBT_DEVICEARRIVAL:
        msgCount++;
        StringCchPrintf(
            strBuff, 256, 
            TEXT("Message %d: DBT_DEVICEARRIVALn"), msgCount);
        break;