如何根据网卡的名称或者设备号等信息,重启一个网卡
在网上搜索了很久,但是还是没有解决,决定提问吧!
如何根据网卡的名称或者设备号等信息,重启一个网卡。我搜索了很多资料,但是好像都不是很详细,没有办法使用,只好自己提问了。
外一问:如何更改当前用户的用户名?
获取我知道用API函数
//---------------------------------------
//获得当前用户名字符串函数
String GetUsername()
{
String UserName ;
char *username = new char[100] ;
unsigned long *size = new unsigned long[10] ;
if(GetUserName(username,size)) //获得当前用户名 UserName = AnsiString(username) ;
delete[] username ;
delete[] size ;
return UserName ;
}
//---------------------------------------
[解决办法]
在google以及csdn上搜索了一下关于网卡禁用的程序,发现所示用的方法都一样,都是使用SetupAPI相关函数,本人也用同样方法写了一个程序,但是发现一个问题(在2k以及XP下都有),使用程序启用和禁用网卡都很正常,但如果是手工禁用网卡(在设备管理器中选择网卡,并停用),则无法用程序启用,而程序却报告启用成功,事实上此时网卡无法使用。本人登陆OS是Administrator身份。现将程序贴上,希望大家指点迷津。
#include <windows.h>
#include <SetupAPI.h>
#include <stdio.h>
#include <tchar.h>
#pragma comment(lib, "setupapi.lib ")
DWORD CtrlNetcard(BOOL bStatus);
void _tmain(int arg, TCHAR** argv)
{
BOOL b = FALSE;
DWORD err = 0;
if(2 == arg)
{
argv++;
if(!_tcsicmp(TEXT( "enable "), *argv))
{
b = TRUE;
err = CtrlNetcard(TRUE);
}
else if(!_tcsicmp(TEXT( "disable "), *argv))
{
b = FALSE;
err = CtrlNetcard(FALSE);
}
else
{
goto main_end;
}
_tprintf( "\n%s NetAdapter %s !\n ",
b ? TEXT( "Enable ") : TEXT( "Disable "),
err ? TEXT( "fail ") : TEXT( "success "));
return;
}
main_end:
_tprintf(TEXT( "\nenable NetCard: enable "));
_tprintf(TEXT( "\ndisable NetCard: disable\n "));
}
DWORD CtrlNetcard(BOOL bStatus)
{
LPCTSTR HardwareId = TEXT( "{4D36E972-E325-11CE-BFC1-08002BE10318} ");
GUID g = {0X4D36E972, 0XE325, 0X11CE, {0XBF, 0XC1, 0X08, 0X00, 0X2B, 0XE1, 0X03, 0X18}};
DWORD NewState = 0;
//调用ddk函数,来禁用网卡
DWORD i = 0, err = 0;
HDEVINFO hDevInfo = NULL;
SP_DEVINFO_DATA spDevInfoData = {0};
NewState = bStatus ? DICS_ENABLE : DICS_DISABLE;
//访问系统的硬件库
hDevInfo = SetupDiGetClassDevs(NULL, TEXT( "PCI "), NULL, DIGCF_ALLCLASSES | DIGCF_PRESENT);
if(INVALID_HANDLE_VALUE == hDevInfo)
{
_tprintf(TEXT( "访问系统硬件出错! "));
return GetLastError();
}
//枚举硬件,获得需要的接口
spDevInfoData.cbSize=sizeof(SP_DEVINFO_DATA);
memcmp(&(spDevInfoData.ClassGuid), &g, sizeof(GUID));
for(i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &spDevInfoData); i++)
{
PBYTE PropertyBuffer = NULL;
DWORD PropertyBufferSize = 0;
//获得硬件的属性值
while(!SetupDiGetDeviceRegistryProperty(
hDevInfo,
&spDevInfoData,
SPDRP_CLASSGUID,
NULL,
PropertyBuffer,
PropertyBufferSize,
&PropertyBufferSize))
{
if(ERROR_INSUFFICIENT_BUFFER == GetLastError())
{
if(PropertyBuffer)
{
free(PropertyBuffer);
}
PropertyBuffer = (PBYTE)malloc(PropertyBufferSize);
}
else
{
break;
}
}
if(!PropertyBuffer)
{
continue;
}
if(!_tcsicmp(HardwareId, (LPTSTR)PropertyBuffer))
{
SP_PROPCHANGE_PARAMS spPropChangeParams ;
free(PropertyBuffer);
spPropChangeParams.ClassInstallHeader.cbSize = sizeof(SP_CLASSINSTALL_HEADER);
spPropChangeParams.ClassInstallHeader.InstallFunction=DIF_PROPERTYCHANGE;
spPropChangeParams.Scope = DICS_FLAG_GLOBAL;
spPropChangeParams.StateChange = NewState;
if(!SetupDiSetClassInstallParams(hDevInfo,&spDevInfoData,(SP_CLASSINSTALL_HEADER*)&spPropChangeParams,sizeof(spPropChangeParams)))
{
err = GetLastError();
goto cleanup_DeviceInfo;
}
if(!SetupDiCallClassInstaller(DIF_PROPERTYCHANGE,hDevInfo,&spDevInfoData))
{
err = GetLastError();
goto cleanup_DeviceInfo;
}
}
else
{
free(PropertyBuffer);
}
}
cleanup_DeviceInfo :
SetupDiDestroyDeviceInfoList(hDevInfo);
return err;
}