这里需要用到cfgmgr32.h,参考了网上好几篇博文。
#include <windows.h> #include <stdio.h> #include <cfgmgr32.h> #pragma comment(lib,"setupapi.lib") int main() { DEVINST devInst; CONFIGRET status; 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 0; }
网上的文章基本上没提到要引入lib,我用VS2010测试会报错,提示“...error LNK2019: 无法解析的外部符号 __imp__CM_Reenumerate_DevNode@8...”、“...error LNK2019: 无法解析的外部符号 __imp__CM_Locate_DevNodeA@12...”
然后继续查资料,提到要引入lib,按照操作了,vs2010又提示错误“无法打开文件 cfgmgr32.lib” 随后在stackoverflow找到答案了,参考 https://stackoverflow.com/questions/27178969/why-cant-i-find-cfgmgr32-lib-in-the-windows-sdk
以上是最终版,经测试在设备管理器里卸载某设备,执行该程序可以实现等同扫描硬件改动的效果。
参考链接:
https://stackoverflow.com/questions/33420994/windows-usb-device-refresh-in-c-c
https://docs.microsoft.com/en-us/windows/desktop/devinst/cfgmgr32-h
https://bbs.csdn.net/topics/30248104
原文链接: https://www.cnblogs.com/sherlock-merlin/p/10793522.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/294367
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!