windows禁用启动设备API(c/cpp)

时间:2021-06-02 20:00:15   收藏:0   阅读:0
  1 #include <time.h>
  2 #include <stdio.h>
  3 #include <errno.h>
  4 #include <string.h>
  5 #include <stdlib.h>
  6 
  7 #include <winsock2.h>
  8 #include <iphlpapi.h>
  9 #include <windows.h>
 10 #include <Pdh.h>
 11 #include <PdhMsg.h>
 12 #include <winreg.h>
 13 #include <setupapi.h>
 14 #include <devguid.h>
 15 #include <initguid.h>
 16 #include <devpkey.h>
 17 #include <cfgmgr32.h>
 18 
 19 
 20 //
 21 // set link exports lib!!!
 22 //
 23 #pragma comment(lib, "Pdh.lib")
 24 #pragma comment(lib, "Advapi32.lib")
 25 #pragma comment(lib, "IPHLPAPI.lib")
 26 #pragma comment(lib, "ws2_32.lib")
 27 #pragma comment(lib, "SetupAPI.lib")
 28 #pragma comment(lib, "Cfgmgr32.lib")
 29 
 30 
 31 
 32 
 33 #define WINDOWS_DEVICE_DISABLE_CODE        (0x16)
 34 //
 35 // windows netcard info 
 36 //
 37 struct NetCardInfoWin {
 38     int pci_slot;        // pci slot
 39     int pci_func;        // pci function
 40     int pci_bus;        // pci bus
 41     char InstanceId[256]; // instanceid
 42     char ServiceName[32]; // driver service name
 43     char CardName[256];      // cardName
 44     char NetshConnetName[256];
 45 };
 46 #define NETCARD_LIST_SIZE    (5)
 47 struct NetCardInfoWin NetCardInfoWinList[NETCARD_LIST_SIZE];
 48 
 49 //
 50 // we need enable the netcard if that is disabled!!!
 51 // if the driver is not work, wait for driver load!!
 52 //
 53 int ScanPCINetCardList(struct NetCardInfoWin *netcardlist, int max)
 54 {
 55     CONFIGRET cr = CR_SUCCESS;
 56     HDEVINFO hdevinfo;
 57     SP_DEVINFO_DATA DeviceInfoData;
 58     DWORD DeviceIndex = 0;
 59     char ServiceName[256];
 60     char InstanceId[256];
 61     DWORD size, address, bus, func,slot ;
 62     int devicecount = 0;
 63     int flag = 0;
 64     DWORD problem_code = 0;
 65     DEVPROPTYPE devptype = DEVPROP_TYPE_INT32;
 66 
 67     
 68     hdevinfo = SetupDiGetClassDevs(&GUID_DEVCLASS_NET, "PCI", NULL,  DIGCF_PRESENT);
 69     
 70     
 71     ZeroMemory(&DeviceInfoData, sizeof(SP_DEVINFO_DATA));
 72     DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
 73     
 74     
 75     while (SetupDiEnumDeviceInfo(
 76                                 hdevinfo,
 77                                 DeviceIndex,
 78                                 &DeviceInfoData)) {
 79         DeviceIndex++;
 80 
 81         if (SetupDiGetDeviceInstanceId(hdevinfo, &DeviceInfoData, InstanceId, 256, &size)) {
 82             printf("instance id :%s\n", InstanceId);
 83         }
 84         
 85         if (SetupDiGetDeviceRegistryProperty(hdevinfo, &DeviceInfoData, SPDRP_ADDRESS, NULL, (PBYTE)&address, 1024, NULL)) {
 86             printf("slot : %x\n", address);
 87         } else {
 88             printf("GetLastError :%d\n", GetLastError());
 89         }
 90         if (SetupDiGetDeviceRegistryProperty(hdevinfo, &DeviceInfoData, SPDRP_BUSNUMBER, NULL, (PBYTE)&bus, 1024, NULL)) {
 91             printf("bus : %x\n",(bus));
 92         } else {
 93             printf("GetLastError :%d\n", GetLastError());
 94         }
 95         slot = (address >> 16) & 0xFFFF;
 96         func = address & 0xFFFF;
 97         
 98         if (SetupDiGetDeviceRegistryProperty(hdevinfo, &DeviceInfoData, SPDRP_SERVICE, NULL, (PBYTE)ServiceName, 1024, NULL)) {
 99             printf("service name : %s\n", ServiceName);
100         } else {
101             printf("GetLastError :%d\n", GetLastError());
102         }
103         if (SetupDiGetDevicePropertyW(hdevinfo, 
104                                     &DeviceInfoData, 
105                                     &DEVPKEY_Device_ProblemCode, &devptype,
106                                     (PBYTE)&problem_code, sizeof(problem_code), NULL, 0)) {
107             // this device is disable, try to enable it
108             if (problem_code == WINDOWS_DEVICE_DISABLE_CODE) {
109                 enable_device(hdevinfo, &DeviceInfoData);
110             } else {
111                 printf("%s , problem code :%d\n", InstanceId, problem_code);
112             }
113         } else {
114             printf("SetupDiGetDeviceProperty error : %d\n", GetLastError());
115         }
116         
117         netcardlist[devicecount].pci_slot = slot;
118         netcardlist[devicecount].pci_func = func;
119         netcardlist[devicecount].pci_bus = bus;
120         memcpy(netcardlist[devicecount].InstanceId, InstanceId, 256);
121         memcpy(netcardlist[devicecount].ServiceName, ServiceName, 256);
122         devicecount++;
123     }
124 
125     (void)SetupDiDestroyDeviceInfoList(hdevinfo);
126     printf("count : %d\n", DeviceIndex);
127     
128     return devicecount;
129 }
130 int main()
131 {
132     
133     return ScanPCINetCardList(NetCardInfoWinList, NETCARD_LIST_SIZE);
134     
135 }
136 int enable_device(HDEVINFO Devs, PSP_DEVINFO_DATA DevInfo)
137 {
138     SP_PROPCHANGE_PARAMS pcp;
139     SP_DEVINSTALL_PARAMS devParams;
140     
141     pcp.ClassInstallHeader.cbSize = sizeof(SP_CLASSINSTALL_HEADER);
142     pcp.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;
143     pcp.StateChange = DICS_ENABLE;
144     pcp.Scope = DICS_FLAG_GLOBAL;
145     pcp.HwProfile = 0;
146     //
147     // don‘t worry if this fails, we‘ll get an error when we try config-
148     // specific.
149     if(SetupDiSetClassInstallParams(Devs,DevInfo,&pcp.ClassInstallHeader,sizeof(pcp))) {
150        SetupDiCallClassInstaller(DIF_PROPERTYCHANGE,Devs,DevInfo);
151     } else {
152         printf("SetupDiSetClassInstallParams DICS_FLAG_GLOBAL ERROR\n");
153     }
154     //
155     // now enable on config-specific
156     //
157     pcp.ClassInstallHeader.cbSize = sizeof(SP_CLASSINSTALL_HEADER);
158     pcp.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;
159     pcp.StateChange = DICS_ENABLE;
160     pcp.Scope = DICS_FLAG_CONFIGSPECIFIC;
161     pcp.HwProfile = 0;
162     //
163     // local enable
164     //
165     if(SetupDiSetClassInstallParams(Devs,DevInfo,&pcp.ClassInstallHeader,sizeof(pcp))) {
166        SetupDiCallClassInstaller(DIF_PROPERTYCHANGE,Devs,DevInfo);
167     }else {
168         printf("SetupDiSetClassInstallParams DICS_FLAG_CONFIGSPECIFIC ERROR\n");
169     }
170     return 0;
171 }

部分代码参考devcon,这个帖子的目的就是为了让大家少踩坑

这个代码是启用禁用的PCI网卡的设备,不要ctrl c, ctrl v,读懂可以自行修改,使用vs2010说找不到 cfgmgr32.lib,

正常的确实找不到,在 C:\windows\system32\cfgmgr32.dll 拷贝到一个目录

使用 pexports(mingw工具) :pexports -o cfgmgr32.dll > cfgmgr32.def

导出cfgmgr32.dll 的符号表def

然后使用 lib : lib /DEF:cfgmgr32.def  /MACHINE:X64

OK,这样就生成了cfgmgr32.lib

编译的时候选上就ok了,把cfgmgr32.lib放在代码同一个目录

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!