网卡禁用 启用演示程序

#include "StdAfx.h" #include #include #include #include #include #pragma comment(lib,"ws2_32.lib") #pragma comment(lib,"setupapi.lib") #define UnknownDevice TEXT("") //NewState = DICS_DISABLE–>禁用网络 //NewState = DICS_ENABLE—>恢复网络 BOOL StateChange(DWORD NewState, DWORD SelectedItem,HDEVINFO hDevInfo) { SP_PROPCHANGE_PARAMS PropChangeParams = {sizeof(SP_CLASSINSTALL_HEADER)}; SP_DEVINFO_DATA DeviceInfoData = {sizeof(SP_DEVINFO_DATA)}; // Get a handle to the Selected Item. if (!SetupDiEnumDeviceInfo(hDevInfo,SelectedItem,&DeviceInfoData)) { printf("SetupDiEnumDeviceInfo FALSE"); return FALSE; } // Set the PropChangeParams structure. PropChangeParams.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE; PropChangeParams.Scope = DICS_FLAG_GLOBAL; PropChangeParams.StateChange = NewState; if (!SetupDiSetClassInstallParams(hDevInfo,&DeviceInfoData, (SP_CLASSINSTALL_HEADER *)&PropChangeParams, sizeof(PropChangeParams))) { printf("SetupDiSetClassInstallParams FALSE"); return FALSE; } // Call the ClassInstaller and perform the change. if (!SetupDiCallClassInstaller(DIF_PROPERTYCHANGE, hDevInfo, &DeviceInfoData)) { printf("SetupDiCallClassInstaller TRUE"); return TRUE; } return TRUE; } //判断设备Guid对应的设备是否为网卡设备 //TRUE 网卡 //FALSE 非网卡 BOOL IsClassNet( GUID * ClassGuid ) { #define MAX_NUM 50 HKEY hKeyClass; LONG lRet; char ClassType[MAX_NUM]; char NetClass[MAX_NUM] = "Net"; DWORD dwLength = MAX_NUM,dwType = REG_SZ; if (hKeyClass = SetupDiOpenClassRegKey(ClassGuid,KEY_READ)) { lRet = RegQueryValueEx(hKeyClass, TEXT("Class"), NULL, &dwType, LPBYTE(ClassType), &dwLength); RegCloseKey(hKeyClass); if (lRet != ERROR_SUCCESS) return FALSE; if (!strcmp(ClassType,NetClass)) return TRUE; } return FALSE; } int main(int argc, char* argv[]) { HDEVINFO hDevInfo = 0; hDevInfo = SetupDiGetClassDevs(NULL,NULL,0,DIGCF_PRESENT|DIGCF_ALLCLASSES); if (hDevInfo == INVALID_HANDLE_VALUE) { printf("SetupDiGetClassDevs"); return 0; } SP_DEVINFO_DATA DeviceInfoData = {sizeof(SP_DEVINFO_DATA)}; for (DWORD i=0;SetupDiEnumDeviceInfo(hDevInfo,i,&DeviceInfoData);i++) { if (IsClassNet(&DeviceInfoData.ClassGuid)) { //DICS_DISABLE–>禁用网络 //DICS_ENABLE—>恢复网络 if (StateChange(DICS_ENABLE,i,hDevInfo)) printf("已经禁用/r/n"); } } return 0; } 不解释 看代码

 

你可能感兴趣的:(网络编程,header,网络,null,include,class)