Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在Windows进程回调中断开连接时获取设备信息(硬件ID)?_C++_Windows_Notifications_Device_Handle - Fatal编程技术网

C++ 如何在Windows进程回调中断开连接时获取设备信息(硬件ID)?

C++ 如何在Windows进程回调中断开连接时获取设备信息(硬件ID)?,c++,windows,notifications,device,handle,C++,Windows,Notifications,Device,Handle,通过对设备更改使用Windows进程回调,我能够在设备到达时设置句柄,让我以一种简单的方式查看它的设备属性,如硬件ID,而无需执行任何设备枚举 但是,在设备断开连接时,收到的句柄无效,这似乎是正确的,因为设备不再连接,但我无法查看设备属性。有没有一种方法可以继续使用手柄 DEV_BROADCAST_HDR* devHDR = reinterpret_cast<DEV_BROADCAST_HDR*>(lParam); if (devHDR->dbch_devicetype ==

通过对设备更改使用Windows进程回调,我能够在设备到达时设置句柄,让我以一种简单的方式查看它的设备属性,如硬件ID,而无需执行任何设备枚举

但是,在设备断开连接时,收到的句柄无效,这似乎是正确的,因为设备不再连接,但我无法查看设备属性。有没有一种方法可以继续使用手柄

DEV_BROADCAST_HDR* devHDR = reinterpret_cast<DEV_BROADCAST_HDR*>(lParam);
if (devHDR->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE)
{
    DEV_BROADCAST_DEVICEINTERFACE* devInterface = reinterpret_cast<DEV_BROADCAST_DEVICEINTERFACE*>(lParam);
    DeviceHandle = CreateFile(devInterface->dbcc_name, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, NULL, NULL);
    if(DeviceHandle ==  != INVALID_HANDLE_VALUE){
        // arrive gets here
    } else {
        // disconnect gets here
    }
}

基本上,因为我无法获得断开连接的有效句柄,所以我无法从DEV_BROADCAST_DEVICEINTERFACE结构获取硬件ID和其他数据。是否有其他方法可以在断开连接时获取设备硬件ID?

当设备连接时,将断开连接时所需的信息存储在地图中,在地图中使用设备特有的信息作为密钥

当设备断开连接时,使用断开连接事件中获得的密钥在地图中查找信息,然后删除条目

例如:

using String = std::basic_string<TCHAR>;

// a struct with all the properties you'd like to use on disconnect
struct device_info {
    CHANGER_PRODUCT_DATA cpd; // just an example
    String something;
};

int main() {
    // a map where the device_name is the key and device_info the value
    std::unordered_map<String, device_info> devices;

    {   // on connect, create a device_info struct and fill it with the info you need on
        // disconnect
        DEV_BROADCAST_DEVICEINTERFACE* devInterface =
            reinterpret_cast<DEV_BROADCAST_DEVICEINTERFACE*>(lParam);
        String new_dev_name { devInterface->dbcc_name };

        device_info di{}; // store what you need from the opened device here
                          // and put it in the map
        devices.emplace(new_dev_name, di);
    }

    {   // on disconnect, find the entry in the map using the disconnected device_name
        DEV_BROADCAST_DEVICEINTERFACE* devInterface =
            reinterpret_cast<DEV_BROADCAST_DEVICEINTERFACE*>(lParam);
        String disc_dev_name{ devInterface->dbcc_name };

        auto fit = devices.find(disc_dev_name);

        if (fit != devices.end()) {
            // we found the device, extract it
            device_info disc_di = fit->second;
            // and erase it from the map
            devices.erase(fit);
            std::wcout << "\"" << disc_di.something << "\" disconnected\n";
        }
    }
}

你的方法不正确

您必须首先检查wParam并测试==DBT\U DEVICEARRIVAL和==DBT\U DeviceMoveComplete


在DBT_DeviceMoveComplete测试中,您将填充LPRAM以获得DEV_BROADCAST_句柄

我在这些情况下使用了一个开关。我会检查一个DEV_BROADCAST_句柄,但我怎样才能从中准确地获取信息?@karamazovbros这取决于您想要获取什么样的信息。显然,您无法打开设备。如果您需要的信息没有出现在断开连接时得到的DEV_BROADCAST_DEVICEINTERFACE结构中,那么您需要在断开连接之前存储这些信息,正如我在回答中所示。如果您没有获取断开连接的设备的句柄,请使用该设备独有的其他东西作为映射中的密钥。我喜欢这种方法。我能够使自己的操作更简单,因为我只需要将设备路径与与windows proc使用的与我的用户数据关联的当前设备路径进行比较。用户数据是动态的,因此如果当前设备不再存在,它可以自动连接到具有相同硬件ID的另一个设备。谢谢@卡拉马佐夫兄弟听起来很棒!干杯我对它进行了一点更新,以便以后阅读它的人更了解它。