How to disable a device

Look up SetupDiChangeState function in MSDN, pay attention to DICS_DISABLE flag.
Disables the device. If the device is disableable but this function cannot disable the device dynamically, 
this function marks the device to be disabled the next time the machine reboots.

The following code snippet was seen inhttp://expert.csdn.net/Expert/topic/1121/1121940.xml?temp=1.285952E-02
How to disable NIC under Win2K/Win98 
Answer by: flyboycsdn(飞仔)   2002-10-25 13:18:37 

 

// Hardware.cpp : Implementation of CHardware
#include "stdafx.h"
#include "NetCA_HARDWARE.h"
#include "Hardware.h"
#define UnknownDevice TEXT("<Unknown Device>")

/
// CHardware

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

      parameter: DriverID[in]--unique ID of the device in registry
                 pVal[out,retval]--,return 0 if succeed,return 1 if fail
                 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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值