According to the docs the C++ functions CM_Locate_DevNodeA
and CM_Reenumerate_DevNode
of CfgMgr32.dll
should be the programatical equivalant to the menu item scan for changed hardware in the device manager.
See 2nd answer to this question for managed code in C#.
But in contrary to the docs, the programmatical approach doesn't work always, when the Device manager operation works. This is my case:
User already plugged in the USB device
No driver found
My installer adds the right INF-File into the Windows/inf directory
CM_Reenumerate_DevNode
doesn't cause the installation of the new driverWheras "Scan for hardware changes" in device manager starts the installation with the new driver
The driver gets recognized after reboot, when using
SetupCopyOEMInf
(C# example in accepted answer to this question) instead of copying the inf file to the C:\windows\inf directory. This method seems to be the equivalent of "Right click on inf file --> Install".In my case the window "Windows can't verify the publisher of this driver software" came up because the manufacturer didn't register it. But this is a different story. However, this could have been another cause for my problem.
I had a similar issue trying to write C++ code to replicate the Device Manager's Scan For Hardware command.
I tried using the code at https://support.microsoft.com/en-us/kb/259697.
However, it didn't work. I noticed
CM_Reenumerate_DevNode
returnedCR_ACCESS_DENIED
, which is#define CR_ACCESS_DENIED (0x00000033) // NT ONLY
inCfgmrg32.h
.According to CM_Reenumerate_DevNode function, callers of this function need
SeLoadDriverPrivilege
.To get around setting this privilege in code, I ran the .exe as an administrator by right-clicking on .exe and selecting Run as administratror.