c++實現"掃描檢測硬件改動"

這裏須要用到cfgmgr32.h,參考了網上好幾篇博文。windows

#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...」api

而後繼續查資料,提到要引入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
.net

相關文章
相關標籤/搜索