网卡禁用和启用

2010-11-10 21:34:23
#include <stdio.h>
#include <windows.h>
//#include "Setupapi.h"
//#include "objbase.h"
#include <setupapi.h>
#pragma comment(lib,"setupapi.lib")
void EnumDevices(HDEVINFO hDevInfo)
{
 SP_DEVINFO_DATA DeviceInfoData = {sizeof(SP_DEVINFO_DATA)};
 LPOLESTR guid;
 char devName[128];
 char instanceId[128];
 for (int i=0;SetupDiEnumDeviceInfo(hDevInfo,i,&DeviceInfoData);i++)
 {
  StringFromCLSID(DeviceInfoData.ClassGuid,&guid);
  SetupDiClassNameFromGuid(&DeviceInfoData.ClassGuid,devName,128,NULL);
  if(!strcmp(devName,"Net"))
  {
   SetupDiGetDeviceInstanceId(hDevInfo,&DeviceInfoData,instanceId,128,NULL);
   if(!strncmp(instanceId,"PCI",3))
   {
   
// wprintf(L"%s/n",guid);
   
// printf("%s/n",devName);
  
//  printf("%s/n",instanceId);
    SP_PROPCHANGE_PARAMS params = {sizeof(SP_CLASSINSTALL_HEADER )};
    params.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;
    params.Scope = DICS_FLAG_CONFIGSPECIFIC;

    
//params.StateChange = DICS_DISABLE;
    params.StateChange = DICS_ENABLE; 
//禁用:DICS_DISABLE,DICS_ENABLE启用
    params.HwProfile = 0;
    SetupDiSetClassInstallParams(hDevInfo, &DeviceInfoData, (SP_CLASSINSTALL_HEADER*)&params, sizeof(SP_PROPCHANGE_PARAMS));
    SetupDiChangeState(hDevInfo, &DeviceInfoData);
   }
  }
  CoTaskMemFree(guid);
 }
}
int main(int argc, char* argv[])
{
 HDEVINFO hDevInfo = INVALID_HANDLE_VALUE;
 hDevInfo = SetupDiGetClassDevs(NULL,NULL,NULL, DIGCF_PRESENT | DIGCF_ALLCLASSES);
 if(INVALID_HANDLE_VALUE == hDevInfo)
  return GetLastError(); 
 EnumDevices(hDevInfo);
 SetupDiDestroyDeviceInfoList(hDevInfo);
 return 0;
}

你可能感兴趣的:(网卡禁用和启用)