Equivalent of “Scan For Hardware Changes” in devic

2019-06-14 09:26发布

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:

  1. User already plugged in the USB device

  2. No driver found

  3. My installer adds the right INF-File into the Windows/inf directory

  4. CM_Reenumerate_DevNode doesn't cause the installation of the new driver

  5. Wheras "Scan for hardware changes" in device manager starts the installation with the new driver

2条回答
等我变得足够好
2楼-- · 2019-06-14 10:15

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.

查看更多
男人必须洒脱
3楼-- · 2019-06-14 10:30

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 returned CR_ACCESS_DENIED, which is #define CR_ACCESS_DENIED (0x00000033) // NT ONLY in Cfgmrg32.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.

查看更多
登录 后发表回答