zoukankan      html  css  js  c++  java
  • c++实现"扫描检测硬件改动"

    这里需要用到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
    ", status);
           return FALSE;
       }
    
       status = CM_Reenumerate_DevNode(devInst, 0);
    
       if (status != CR_SUCCESS) {
           printf("CM_Reenumerate_DevNode failed: %x
    ", 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

  • 相关阅读:
    Ubuntu16.04更新记
    「BZOJ2153」设计铁路
    [UVA-11995]I Can Guess the Data Structure!
    [UVA-11100] The Trip
    [UVA-11039]Children's Game
    [BZOJ1008][HNOI2008]越狱
    NOIP2018退役祭
    修马路
    [NOIP2005]过河
    [POJ1958][Strange Tower of Hanoi]
  • 原文地址:https://www.cnblogs.com/sherlock-merlin/p/10793522.html
Copyright © 2011-2022 走看看