简体   繁体   中英

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

I am writing a Windows service that auto starts. It would seem my service starts before Windows is done enumerating hardware; specifically USB flash drives. Is there anyway to know when Windows is done with its initial hardware scan? I'm pretty sure there is because Explorer will display a prompt when that scan determines a reboot is necessary to complete hardware installation. I've thought about just sleeping for some long period of time. I'd rather find a more elegant solution. I'm developing with Visual Studio 2017 using C++ and targeting Windows 7 and Windows 10.

I don't know if this is just delaying my app long enough for all the drives to show up or if it's forcing the drive to show up. Here's my solution:

#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;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM