windows禁用启动设备API(c/cpp) [操作系统入门]

编程

  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//

37struct NetCardInfoWin {

38int pci_slot; // pci slot

39int pci_func; // pci function

40int pci_bus; // pci bus

41char InstanceId[256]; // instanceid

42char ServiceName[32]; // driver service name

43char CardName[256]; // cardName

44char NetshConnetName[256];

45};

46#define NETCARD_LIST_SIZE (5)

47struct 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//

53int ScanPCINetCardList(struct NetCardInfoWin *netcardlist, int max)

54{

55 CONFIGRET cr = CR_SUCCESS;

56 HDEVINFO hdevinfo;

57 SP_DEVINFO_DATA DeviceInfoData;

58 DWORD DeviceIndex = 0;

59char ServiceName[256];

60char InstanceId[256];

61 DWORD size, address, bus, func,slot ;

62int devicecount = 0;

63int 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

75while (SetupDiEnumDeviceInfo(

76 hdevinfo,

77 DeviceIndex,

78 &DeviceInfoData)) {

79 DeviceIndex++;

80

81if (SetupDiGetDeviceInstanceId(hdevinfo, &DeviceInfoData, InstanceId, 256, &size)) {

82 printf("instance id :%s

", InstanceId);

83 }

84

85if (SetupDiGetDeviceRegistryProperty(hdevinfo, &DeviceInfoData, SPDRP_ADDRESS, NULL, (PBYTE)&address, 1024, NULL)) {

86 printf("slot : %x

", address);

87 } else {

88 printf("GetLastError :%d

", GetLastError());

89 }

90if (SetupDiGetDeviceRegistryProperty(hdevinfo, &DeviceInfoData, SPDRP_BUSNUMBER, NULL, (PBYTE)&bus, 1024, NULL)) {

91 printf("bus : %x

",(bus));

92 } else {

93 printf("GetLastError :%d

", GetLastError());

94 }

95 slot = (address >> 16) & 0xFFFF;

96 func = address & 0xFFFF;

97

98if (SetupDiGetDeviceRegistryProperty(hdevinfo, &DeviceInfoData, SPDRP_SERVICE, NULL, (PBYTE)ServiceName, 1024, NULL)) {

99 printf("service name : %s

", ServiceName);

100 } else {

101 printf("GetLastError :%d

", GetLastError());

102 }

103if (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

108if (problem_code == WINDOWS_DEVICE_DISABLE_CODE) {

109 enable_device(hdevinfo, &DeviceInfoData);

110 } else {

111 printf("%s , problem code :%d

", InstanceId, problem_code);

112 }

113 } else {

114 printf("SetupDiGetDeviceProperty error : %d

", 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

", DeviceIndex);

127

128return devicecount;

129}

130int main()

131{

132

133return ScanPCINetCardList(NetCardInfoWinList, NETCARD_LIST_SIZE);

134

135}

136int 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.

149if(SetupDiSetClassInstallParams(Devs,DevInfo,&pcp.ClassInstallHeader,sizeof(pcp))) {

150 SetupDiCallClassInstaller(DIF_PROPERTYCHANGE,Devs,DevInfo);

151 } else {

152 printf("SetupDiSetClassInstallParams DICS_FLAG_GLOBAL ERROR

");

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//

165if(SetupDiSetClassInstallParams(Devs,DevInfo,&pcp.ClassInstallHeader,sizeof(pcp))) {

166 SetupDiCallClassInstaller(DIF_PROPERTYCHANGE,Devs,DevInfo);

167 }else {

168 printf("SetupDiSetClassInstallParams DICS_FLAG_CONFIGSPECIFIC ERROR

");

169 }

170return0;

171 }

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

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

正常的确实找不到,在 C:windowssystem32cfgmgr32.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放在代码同一个目录

 

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

以上是 windows禁用启动设备API(c/cpp) [操作系统入门] 的全部内容, 来源链接: utcz.com/z/519689.html

回到顶部