您的位置:首页 > Web前端

请教如何禁用网卡,Win2K/Win98

2004-12-14 09:02 429 查看
// Hardware.cpp : Implementation of CHardware

#include "stdafx.h"

#include "NetCA_HARDWARE.h"

#include "Hardware.h"

#define UnknownDevice TEXT("<Unknown Device>")

/////////////////////////////////////////////////////////////////////////////

// CHardware

/*****************禁用设备********************************

    参数说明: DriverID[in]--设备在注册表中的唯一标识

  pVal[out,retval]--返回值,成功为0,失败为1

    创建时间: 2002-6-21

**********************************************************/

STDMETHODIMP CHardware::Disable(BSTR DriverID, long *pVal)

{

 BOOL ShowHidden = 0;

 HDEVINFO hDevInfo = 0;

 long len;

//init the value

 TIndex = -1;

 len = wcstombs(NULL,DriverID,wcslen(DriverID));

 len = len + 1;

 DrvID = (char *)malloc(len);

 memset(DrvID,0,len+1);

 wcstombs(DrvID,DriverID,wcslen(DriverID));

 if (INVALID_HANDLE_VALUE == (hDevInfo =

                SetupDiGetClassDevs(NULL,NULL,NULL,

                DIGCF_PRESENT|DIGCF_ALLCLASSES)))

    

        {

            *pVal = -1;

   return S_OK;

        }

//get the index of drv in the set

 EnumAddDevices(ShowHidden,hDevInfo);

//disable the drv 

      

// if ((IsDisableable(TIndex,hDevInfo))&&(!(TIndex==-1)))

 if (!IsDisabled(TIndex,hDevInfo))

  if (IsDisableable(TIndex,hDevInfo))

   if (StateChange(DICS_DISABLE,TIndex,hDevInfo))

    *pVal = 0;

   else

    *pVal = -1;   

  else

   *pVal = 1;   

 else

  *pVal = 0;

 if(hDevInfo)

  SetupDiDestroyDeviceInfoList(hDevInfo);

 return S_OK;

}

BOOL CHardware::EnumAddDevices(BOOL ShowHidden, HDEVINFO hDevInfo)

{

 DWORD i, Status, Problem;

    LPTSTR IOName=NULL;

 DWORD len=0;

    SP_DEVINFO_DATA DeviceInfoData = {sizeof(SP_DEVINFO_DATA)};

 

 

    //

    // Enumerate though all the devices.

    //

    for (i=0;SetupDiEnumDeviceInfo(hDevInfo,i,&DeviceInfoData);i++)

    {

        //

        // Should we display this device, or move onto the next one.

        //

        if (CR_SUCCESS != CM_Get_DevNode_Status(&Status, &Problem,

                    DeviceInfoData.DevInst,0))

        {

           

            continue;

        }

        if (!(ShowHidden || !((Status & DN_NO_SHOW_IN_DM) ||

            IsClassHidden(&DeviceInfoData.ClassGuid))))

            continue;

 

        //

        // Get a friendly name for the device.

        //

   

        ConstructDeviceName(hDevInfo,&DeviceInfoData,

       &IOName,

       (PULONG)&len);

  if (strcmp(IOName,DrvID) == 0)

  {

   TIndex = i;

   return TRUE;

  }

 }

 return TRUE;

  

}

BOOL CHardware::IsClassHidden(GUID *ClassGuid)

{

 BOOL bHidden = FALSE;

    HKEY hKeyClass;

    //

    // If the devices class has the NoDisplayClass value then

    // don't display this device.

    //

    if (hKeyClass = SetupDiOpenClassRegKey(ClassGuid,KEY_READ))

    {

        bHidden = (RegQueryValueEx(hKeyClass,

            REGSTR_VAL_NODISPLAYCLASS,

            NULL, NULL, NULL, NULL) == ERROR_SUCCESS);

        RegCloseKey(hKeyClass);

    }                                

    return bHidden;

}

BOOL CHardware::ConstructDeviceName(HDEVINFO DeviceInfoSet, PSP_DEVINFO_DATA DeviceInfoData, PVOID Buffer, PULONG Length)

{

 if (!GetRegistryProperty(DeviceInfoSet,

        DeviceInfoData,

        SPDRP_DRIVER ,

        Buffer,

        Length))

    {

        if (!GetRegistryProperty(DeviceInfoSet,

            DeviceInfoData,

            SPDRP_DEVICEDESC ,

            Buffer,

            Length))

        {

            if (!GetRegistryProperty(DeviceInfoSet,

                DeviceInfoData,

                SPDRP_CLASS ,

                Buffer,

                Length))

            {

                if (!GetRegistryProperty(DeviceInfoSet,

                    DeviceInfoData,

                    SPDRP_CLASSGUID ,

                    Buffer,

                    Length))

                {

                     *Length = (_tcslen(UnknownDevice)+1)*sizeof(TCHAR);

                     Buffer =(char *)malloc(*Length);

                     _tcscpy(*(LPTSTR *)Buffer,UnknownDevice);

                }

            }

        }

    }

   

 return TRUE;

}

BOOL CHardware::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)

        {

            //

            // We need to change the buffer size.

            //

            if (*(LPTSTR *)Buffer)

                LocalFree(*(LPTSTR *)Buffer);

            *(LPTSTR *)Buffer = (PCHAR)LocalAlloc(LPTR,*Length);

        }

        else

        {

            //

            // Unknown Failure.

            //

       

            return FALSE;

        }           

    }

    return (*(LPTSTR *)Buffer)[0];

}

BOOL CHardware::StateChange(DWORD NewState, DWORD SelectedItem, HDEVINFO hDevInfo)

{

 SP_PROPCHANGE_PARAMS PropChangeParams = {sizeof(SP_CLASSINSTALL_HEADER)};

    SP_DEVINFO_DATA DeviceInfoData = {sizeof(SP_DEVINFO_DATA)};

   

    //

    // Get a handle to the Selected Item.

    //

    if (!SetupDiEnumDeviceInfo(hDevInfo,SelectedItem,&DeviceInfoData))

    {

        return FALSE;

    }

    //

    // Set the PropChangeParams structure.

    //

    PropChangeParams.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;

    PropChangeParams.Scope = DICS_FLAG_GLOBAL;

    PropChangeParams.StateChange = NewState;

    if (!SetupDiSetClassInstallParams(hDevInfo,

        &DeviceInfoData,

        (SP_CLASSINSTALL_HEADER *)&PropChangeParams,

        sizeof(PropChangeParams)))

    {

   

        return FALSE;

    }

    //

    // Call the ClassInstaller and perform the change.

    //

    if (!SetupDiCallClassInstaller(DIF_PROPERTYCHANGE,

        hDevInfo,

        &DeviceInfoData))

    {

       

        return TRUE;

    }

   

    return TRUE;

}

BOOL CHardware::IsDisabled(DWORD SelectedItem, HDEVINFO hDevInfo)

{

 SP_DEVINFO_DATA DeviceInfoData = {sizeof(SP_DEVINFO_DATA)};

    DWORD Status, Problem;

    //

    // Get a handle to the Selected Item.

    //

    if (!SetupDiEnumDeviceInfo(hDevInfo,SelectedItem,&DeviceInfoData))

    {

        return FALSE;

    }

    if (CR_SUCCESS != CM_Get_DevNode_Status(&Status, &Problem,

                DeviceInfoData.DevInst,0))

    {

        return FALSE;

    }

    return ((Status & DN_HAS_PROBLEM) && (CM_PROB_DISABLED == Problem)) ;

}

BOOL CHardware::IsDisableable(DWORD SelectedItem, HDEVINFO hDevInfo)

{

 SP_DEVINFO_DATA DeviceInfoData = {sizeof(SP_DEVINFO_DATA)};

    DWORD Status, Problem;

    //

    // Get a handle to the Selected Item.

    //

    if (!SetupDiEnumDeviceInfo(hDevInfo,SelectedItem,&DeviceInfoData))

    {

        return FALSE;

    }

    if (CR_SUCCESS != CM_Get_DevNode_Status(&Status, &Problem,

                DeviceInfoData.DevInst,0))

    {

        return FALSE;

    }

    return ((Status & DN_DISABLEABLE) &&

        (CM_PROB_HARDWARE_DISABLED != Problem));

}

STDMETHODIMP CHardware::Enable(BSTR DriverID, long *pVal)

{

 BOOL ShowHidden = 0;

 HDEVINFO hDevInfo = 0;

 long len;

//init the value

 TIndex = -1;

 len = wcstombs(NULL,DriverID,wcslen(DriverID));

 len = len + 1;

 DrvID = (char *)malloc(len);

 memset(DrvID,0,len+1);

 wcstombs(DrvID,DriverID,wcslen(DriverID));

 if (INVALID_HANDLE_VALUE == (hDevInfo =

                SetupDiGetClassDevs(NULL,NULL,NULL,

                DIGCF_PRESENT|DIGCF_ALLCLASSES)))

    

        {

            *pVal = -1;

   return S_OK;

        }

//get the index of drv in the set

 EnumAddDevices(ShowHidden,hDevInfo);

//disable the drv 

      

 if (IsDisabled(TIndex,hDevInfo))

  if (StateChange(DICS_ENABLE,TIndex,hDevInfo))

   *pVal = 0;

  else

   *pVal = -1; 

 else

  *pVal = 0; 

 

 if(hDevInfo)

  SetupDiDestroyDeviceInfoList(hDevInfo);

 return S_OK;

}

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  buffer null header class byte