#include "StdAfx.h"
#include "NetCardCtrl.h"
NetCardCtrl::NetCardCtrl(void)
{
}
NetCardCtrl::~NetCardCtrl(void)
{
}
//参数: bool bStatus
// true: 禁用网络
// false: 启用网络
BOOL NetCardCtrl::DisableNetCard( BOOL bStatus )
{
IN LPTSTR NetCardClassGuidID ;
NetCardClassGuidID= _T("{4D36E972-E325-11CE-BFC1-08002BE10318}");
DWORD NewState ;
if(bStatus)
{
NewState=DICS_DISABLE ;
}
else
{
NewState=DICS_ENABLE ;
}
DWORD i = 0;
BOOL Found = FALSE ;
HDEVINFO hDevInfo ;
SP_DEVINFO_DATA spDevInfoData ;
//访问系统的硬件库
hDevInfo=SetupDiGetClassDevs(NULL,_T("PCI"),NULL,DIGCF_ALLCLASSES|DIGCF_PRESENT);
if(hDevInfo==INVALID_HANDLE_VALUE)
{
MessageBox(NULL,_T("访问系统硬件出错!"),_T(""),0);
return FALSE ;
}
//枚举硬件,获得需要的接口
spDevInfoData.cbSize=sizeof(SP_DEVINFO_DATA);
for(i=0;SetupDiEnumDeviceInfo(hDevInfo,i,&spDevInfoData);i++)
{
DWORD DataT ;
LPTSTR p,buffer=NULL ;
DWORD buffersize=0 ;
while(!SetupDiGetDeviceRegistryProperty(hDevInfo,&spDevInfoData,SPDRP_CLASSGUID,&DataT, (PBYTE)buffer,buffersize,&buffersize))
{
if(GetLastError()==ERROR_INVALID_DATA)
{
break ;
}
else if(GetLastError()==ERROR_INSUFFICIENT_BUFFER)
{
if(buffer)
LocalFree(buffer);
buffer=(LPTSTR)LocalAlloc(LPTR,buffersize);
}
else
{
SetupDiDestroyDeviceInfoList(hDevInfo);
return FALSE ;
}
}
if(GetLastError()==ERROR_INVALID_DATA)
continue ;
for(p=buffer;*p&&(p<&buffer[buffersize]);p+=lstrlen(p)+sizeof(TCHAR))
{
if(!_tcscmp(NetCardClassGuidID,p))
{
//找到网卡
Found=TRUE ;
break ;
}
}
if(buffer)
LocalFree(buffer);
if(Found)
{
SP_PROPCHANGE_PARAMS spPropChangeParams ;
spPropChangeParams.ClassInstallHeader.cbSize=sizeof(SP_CLASSINSTALL_HEADER);
spPropChangeParams.ClassInstallHeader.InstallFunction=DIF_PROPERTYCHANGE ;
spPropChangeParams.Scope=DICS_FLAG_GLOBAL ;
spPropChangeParams.StateChange=NewState ;
//
if(!SetupDiSetClassInstallParams(hDevInfo,&spDevInfoData,(SP_CLASSINSTALL_HEADER*)&spPropChangeParams,sizeof(spPropChangeParams)))
{
DWORD errorcode=GetLastError();
}
if(!SetupDiCallClassInstaller(DIF_PROPERTYCHANGE,hDevInfo,&spDevInfoData))
{
DWORD errorcode=GetLastError();
}
switch(NewState)
{
case DICS_DISABLE :
MessageBox(NULL,_T("成功禁用网络!"),_T(""),0);
break ;
case DICS_ENABLE :
MessageBox(NULL,_T("成功启用网络!"),_T(""),0);
break ;
}
break ;
}
}
SetupDiDestroyDeviceInfoList(hDevInfo);
return TRUE ;
}
- 1
- 2
- 3
- 4
- 5
- 6
前往页