Borysko Danko
Borysko Danko

Reputation: 165

Windows USB device refresh in C\C++

I am trying to find a way to pragmatically re-enumerate the USB devices connected to the USB ports of PC.

In Windows, it is possible to open the Device Manager, then right click on the PC name and select "Scan for Hardware Changes".

How can I execute this (or something similar but specific to the USB bus) from a C/C++ program?

Upvotes: 1

Views: 2472

Answers (1)

Cezar F.
Cezar F.

Reputation: 53

You can use cfgmgr32.lib library in your project. It's easy. If you don't know how to use it, you can ask me again.

BOOL ScanForHardwareChanges() 
{
    DEVINST     devInst;
    CONFIGRET   status;

   // 
   // Get the root devnode.
   // 

   status = CM_Locate_DevNode(&devInst, NULL, CM_LOCATE_DEVNODE_NORMAL);

   if (status != CR_SUCCESS) {
       printf("CM_Locate_DevNode failed: %x\n", status);
       return FALSE;
   }

   status = CM_Reenumerate_DevNode(devInst, 0);

   if (status != CR_SUCCESS) {
       printf("CM_Reenumerate_DevNode failed: %x\n", status);
       return FALSE;
   }

   return TRUE;
}    

Upvotes: 3

Related Questions