禁用和启用网卡.使用windows DDK - David liao Stl C - ...

来源:百度文库 编辑:神马文学网 时间:2024/03/29 08:37:11
#include 
#include 
#include 
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 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 ¦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 = "";
                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 ¦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;