www.pudn.com > EthernetOperation.rar > jyqywk.cpp, change:2005-12-18,size:4485b


/***************************************************************************** 
演示如何编程实现启用禁用网卡 
Mady By ZwelL 
2004.7.29 
zwell@sohu.com 
*****************************************************************************/ 
#include <windows.h> 
#include <setupapi.h> 
#include <tchar.h> 
#include <stdio.h> 
#pragma comment(lib,"ws2_32.lib") 
#pragma comment(lib,"setupapi.lib") 
 
BOOL DisableNetInterface(bool bStatus) 
{ 
    IN LPTSTR HardwareId ; 
    //硬件ComponentId,注册表地址:system\currentcontrolset\class\{4D36E972-E325-11CE-BFC1-08002BE10318}\0000 
     
    HardwareId="PCI\\VEN_10EC&DEV_8139&SUBSYS_813910EC" ; 
     
    DWORD NewState ; 
     
    if(bStatus) 
    { 
        NewState=DICS_DISABLE ; 
        //禁用 
    } 
    else  
    { 
        NewState=DICS_ENABLE ; 
        //启用 
    } 
     
    //调用ddk函数,来禁用网卡 
     
    DWORD i,err ; 
    BOOL Found=false ; 
     
    HDEVINFO hDevInfo ; 
    SP_DEVINFO_DATA spDevInfoData ; 
     
    //访问系统的硬件库 
    hDevInfo=SetupDiGetClassDevs(NULL,"PCI",NULL,DIGCF_ALLCLASSES|DIGCF_PRESENT); 
    if(hDevInfo==INVALID_HANDLE_VALUE) 
    { 
        printf("访问系统硬件出错!"); 
        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_HARDWAREID, 
    &DataT, 
    (PBYTE)buffer, 
    buffersize, 
    &buffersize)) 
        { 
            if(GetLastError()==ERROR_INVALID_DATA) 
            { 
                //不存在HardwareID. Continue. 
                break ; 
            } 
            else if(GetLastError()==ERROR_INSUFFICIENT_BUFFER) 
            { 
                //buffer size不对. 
                if(buffer) 
                LocalFree(buffer); 
                buffer=(char*)LocalAlloc(LPTR,buffersize); 
            } 
            else  
            { 
                //未知错误 
                goto cleanup_DeviceInfo ; 
            } 
        } 
         
        if(GetLastError()==ERROR_INVALID_DATA) 
        continue ; 
         
        //比较,找到和网卡ID相同的项 
        for(p=buffer;*p&&(p<&buffer[buffersize]);p+=lstrlen(p)+sizeof(TCHAR)) 
        { 
             
            if(!_tcscmp(HardwareId,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 ; 
            //禁用:DICS_DISABLE,DICS_ENABLE启用 
             
            // 
            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 : 
     printf("成功禁用网络!"); 
                break ; 
                case DICS_ENABLE : 
     printf("成功启用网络!"); 
                break ; 
            } 
             
            break ; 
        } 
         
    } 
     
    //退出时,清理工作环境 
    cleanup_DeviceInfo : 
    err=GetLastError(); 
    SetupDiDestroyDeviceInfoList(hDevInfo); 
    SetLastError(err); 
     
    return true ; 
} 
 
void usage(char *exefile) 
{ 
printf("Usage:%s [-e|-d]\r\n", exefile); 
printf("\t-e: Enable the network card.\r\n"); 
printf("\t-d: Disable the network card.\r\n"); 
exit(0); 
} 
 
 
int main(int argc,char**argv) 
{ 
if(argc<2) 
  usage(argv[0]); 
if(!DisableNetInterface((strstr(argv[1],"-d")>0?TRUE:FALSE))) 
  printf("对网卡操作失败!"); 
    return 0; 
}