如何知道初始即插即用枚举何时在 Windows 中完成

How to know when initial plug and play enumeration is complete in Windows

本文关键字:Windows 何时 枚举 何知道 即插即用      更新时间:2023-10-16

我正在编写一个自动启动的Windows服务。似乎我的服务在 Windows 完成枚举硬件之前启动;特别是USB闪存驱动器。无论如何,有没有知道Windows何时完成其初始硬件扫描?我很确定有,因为当扫描确定需要重新启动才能完成硬件安装时,资源管理器将显示提示。我想过睡很久。我宁愿找到一个更优雅的解决方案。我正在使用C++使用Visual Studio 2017进行开发,并针对Windows 7和Windows 10。

我不知道

这是否只是延迟了我的应用程序足够长的时间以使所有驱动器都显示出来,或者它是否强制驱动器显示。这是我的解决方案:

#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 enumn"));
        }else{
            _tprintf(TEXT("Enum failed: %un"),(UINT)CM_MapCrToWin32Err(crErr,0));
        }
    }else{
        _tprintf(TEXT("Locate failed: %un"),(UINT)CM_MapCrToWin32Err(crErr,0));
    }
    return 0;
}