【问题标题】:How to know when initial plug and play enumeration is complete in Windows如何知道 Windows 中初始即插即用枚举何时完成
【发布时间】:2019-07-26 03:25:16
【问题描述】:
我正在编写一个自动启动的 Windows 服务。似乎我的服务在 Windows 完成硬件枚举之前启动;特别是 USB 闪存驱动器。无论如何要知道 Windows 何时完成其初始硬件扫描?我很确定这是因为当该扫描确定需要重新启动以完成硬件安装时,Explorer 将显示提示。我曾经想过要睡很长一段时间。我宁愿找到一个更优雅的解决方案。我正在使用 C++ 和面向 Windows 7 和 Windows 10 的 Visual Studio 2017 进行开发。
【问题讨论】:
标签:
c++
windows
plug-and-play
【解决方案1】:
我不知道这是否只是延迟了我的应用程序足够长的时间以使所有驱动器都显示出来,或者它是否会强制驱动器显示出来。这是我的解决方案:
#define _WIN32_WINNT _WIN32_WINNT_WIN7
#include <windows.h>
#include <cfgmgr32.h>
#include <tchar.h>
#pragma comment(lib,"cfgmgr32.lib")
INT _tmain(){
DEVINST diDevice = 0;
CONFIGRET crErr = CM_Locate_DevInst(&diDevice,nullptr,CM_LOCATE_DEVNODE_NORMAL);
if (crErr == CR_SUCCESS){
crErr = CM_Reenumerate_DevNode(diDevice,CM_REENUMERATE_SYNCHRONOUS);
if (crErr == CR_SUCCESS){
_tprintf(TEXT("Finished enum\n"));
}else{
_tprintf(TEXT("Enum failed: %u\n"),(UINT)CM_MapCrToWin32Err(crErr,0));
}
}else{
_tprintf(TEXT("Locate failed: %u\n"),(UINT)CM_MapCrToWin32Err(crErr,0));
}
return 0;
}