关闭

禁用和启用网卡.使用windows DDK

标签: windowsbuffernullddkincludeheader
1659人阅读 评论(0) 收藏 举报

 

#include <SetupAPI.h>   
#include 
<cfgmgr32.h>     
#include 
<list>
using namespace std;
//     cfgmgr32.h 在Microsoft Windows 2000 DDK 中.   
//     要用 CM_Get_DevNode_Status(...) 来查询状态.   
//---------------------------------------------------------------------------   
typedef struct  NetCardStruct   
{   
    DWORD    Id;         
// 网卡设备号   
    AnsiString   Name;     // 网卡名   
    bool     Disabled;     // 当前是否禁用   
    bool     Changed;         // 是否更改过   
}
TNetCardStruct;   
typedef TNetCardStruct
*  PNetCardStruct;   
typedef list
<TNetCardStruct> TList;

//---------------------------------------------------------------------------   
//     EnumNetCards 枚举出网卡   
//---------------------------------------------------------------------------   
void  __fastcall EnumNetCards(TList  *NetDeviceList)   
{   
    AnsiString     DevValue;   
    PNetCardStruct NetCard;   
    DWORD  Status, Problem;   
    LPTSTR Buffer   
= NULL;   
    DWORD  BufSize  
= 0;   
    HDEVINFO hDevInfo   
= 0;  

    
if(INVALID_HANDLE_VALUE==(hDevInfo=SetupDiGetClassDevs(NULL,NULL,0,DIGCF_PRESENT &brvbar;DIGCF_ALLCLASSES)))   
        
return

    SP_DEVINFO_DATA  DeviceInfoData 
={sizeof(SP_DEVINFO_DATA)};

    HKEY hKeyClass;   
    
char DeviceName[200];   
    
for(DWORD DeviceId=0;SetupDiEnumDeviceInfo(hDevInfo,DeviceId,&DeviceInfoData);DeviceId++)   
    
{   
        
if (CM_Get_DevNode_Status(&Status, &Problem, DeviceInfoData.DevInst,0!= CR_SUCCESS)   
            
continue;   
        DevValue.SetLength(
0);   
        
if(GetRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_CLASS , &Buffer, (PULONG)&BufSize))   
            DevValue 
= Buffer;   

        
if (DevValue == "Net")   
        
{   
            DevValue.SetLength(
0);   

            
if (GetRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_ENUMERATOR_NAME , &Buffer, (PULONG)&BufSize))   
                DevValue 
= Buffer;   

            
if (DevValue != "ROOT")   
            
{   
                NetCard 
= new TNetCardStruct;   
                NetCard
->Id = DeviceId;   
                NetCard
->Name = "<Unknown Device>";   
                
if (GetRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_DRIVER , &Buffer, (PULONG)&BufSize))   
                    
if (GetRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_DEVICEDESC , &Buffer, (PULONG)&BufSize))   
                        NetCard
->Name = Buffer;   
                NetCard
->Disabled = (Status & DN_HAS_PROBLEM) && (CM_PROB_DISABLED == Problem);   
                NetCard
->Changed = false;   
                NetDeviceList
->Add(NetCard);   
            }
   
        }
   
    }
   
}
   

//---------------------------------------------------------------------------   
bool __fastcall GetRegistryProperty(HDEVINFO DeviceInfoSet,   
                                    PSP_DEVINFO_DATA DeviceInfoData, 
                                    ULONG Property, 
                                    PVOID Buffer,
                                    PULONG Length)   
{   
    
while (!SetupDiGetDeviceRegistryProperty(DeviceInfoSet,   
        DeviceInfoData, Property, NULL, (BYTE 
*)*(TCHAR **)Buffer, *Length, Length))   
    
{   
        
if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)   
        
{   
            
if (*(LPTSTR *)Buffer) LocalFree(*(LPTSTR *)Buffer);   
            
*(LPTSTR *)Buffer = (PCHAR)LocalAlloc(LPTR,*Length);   
        }
   
        
else return false;   
    }
   
    
return (*(LPTSTR *)Buffer)[0];   
}
   


//---------------------------------------------------------------------------   
//     NetCardStateChange 网卡的启用与禁用   
//             NetCardPoint 是 PNetCardStruct 的指针.   
//             Enabled     true = 启用     false = 禁用   
//---------------------------------------------------------------------------   
bool __fastcall NetCardStateChange(void * NetCardPoint, bool Enabled)   
{   
    PNetCardStruct NetCard 
= (PNetCardStruct)NetCardPoint;   
    DWORD DeviceId 
= NetCard->Id;   
    HDEVINFO hDevInfo 
= 0;   
    
if (INVALID_HANDLE_VALUE == (hDevInfo =   
        SetupDiGetClassDevs(NULL,NULL,
0,DIGCF_PRESENT &brvbar;DIGCF_ALLCLASSES)))   
        
return false;   
    SP_DEVINFO_DATA DeviceInfoData 
= ;   
    DWORD Status, Problem;   
    
if (!SetupDiEnumDeviceInfo(hDevInfo,DeviceId,&DeviceInfoData))   
        
return false;   

    
if (CM_Get_DevNode_Status(&Status, &Problem,   
        DeviceInfoData.DevInst,
0!= CR_SUCCESS)   
        
return false;   

    SP_PROPCHANGE_PARAMS PropChangeParams 
= ;   
    PropChangeParams.ClassInstallHeader.InstallFunction 
= DIF_PROPERTYCHANGE;   
    PropChangeParams.Scope 
= DICS_FLAG_GLOBAL;   
    
if (Enabled)   
    
{   
        
if (!((Status & DN_HAS_PROBLEM) && (CM_PROB_DISABLED == Problem)))   
        
{   
            NetCard
->Disabled = false;   
            
return false;   
        }
   
        PropChangeParams.StateChange 
= DICS_ENABLE;   
    }
   
    
else   
    
{   
        
if ((Status & DN_HAS_PROBLEM) && (CM_PROB_DISABLED == Problem))   
        
{   
            NetCard
->Disabled = true;   
            
return false;   
        }
   
        
if (!((Status & DN_DISABLEABLE) && (CM_PROB_HARDWARE_DISABLED != Problem)))   
            
return false;   
        PropChangeParams.StateChange 
= DICS_DISABLE;   
    }
   

    
if (!SetupDiSetClassInstallParams(hDevInfo, &DeviceInfoData,   
        (SP_CLASSINSTALL_HEADER 
*)&PropChangeParams, sizeof(PropChangeParams)))   
        
return false;   
    
if (!SetupDiCallClassInstaller(DIF_PROPERTYCHANGE, hDevInfo, &DeviceInfoData))   
        
return false;   
    
if (CM_Get_DevNode_Status(&Status, &Problem,   
        DeviceInfoData.DevInst,
0== CR_SUCCESS)   
        NetCard
->Disabled = (Status & DN_HAS_PROBLEM) && (CM_PROB_DISABLED == Problem);   
    
return true;   
}
  
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:56866次
    • 积分:795
    • 等级:
    • 排名:千里之外
    • 原创:20篇
    • 转载:10篇
    • 译文:0篇
    • 评论:7条
    最新评论