如何利用vc禁用/启用网卡本地连接

flybird12 2007-05-08 01:31:00
如题,2000系统,通过vc程序禁用/启用网卡本地连接
...全文
2338 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
黑皑皑 2012-01-11
  • 打赏
  • 举报
回复
不mark,直接用。。。。
haokongdashi 2009-03-11
  • 打赏
  • 举报
回复
MARK
UltraBejing 2008-05-01
  • 打赏
  • 举报
回复
我也想知道,正在找這方面的資料~~~~~
cshzxing 2007-10-17
  • 打赏
  • 举报
回复
mark
g20044111 2007-05-10
  • 打赏
  • 举报
回复
MARK....study..
zjh824 2007-05-09
  • 打赏
  • 举报
回复
MARK+收藏
清歌谁与和 2007-05-09
  • 打赏
  • 举报
回复
mark
masterz 2007-05-09
  • 打赏
  • 举报
回复
×××××××××××××××××××××××××××××
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;
}

masterz 2007-05-09
  • 打赏
  • 举报
回复
The following code snippet was seen in http://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;

}
masterz 2007-05-09
  • 打赏
  • 举报
回复
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.

2,640

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 硬件/系统
社区管理员
  • 硬件/系统社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧