VC写的代码xp下可以禁用、启动网卡,win7下不行
BOOL StateChange(DWORD NewState, DWORD SelectedItem,HDEVINFO hDevInfo){
LPTSTR buffer=NULL ;
DWORD buffersize=0 ;
SP_PROPCHANGE_PARAMS PropChangeParams = {sizeof(SP_CLASSINSTALL_HEADER)};
SP_DEVINFO_DATA DeviceInfoData = {sizeof(SP_DEVINFO_DATA)};
HCURSOR hCursor;
//
// This may take a while :^(
//
hCursor = SetCursor(LoadCursor(NULL, IDC_WAIT));
//
// Get a handle to the Selected Item.
//
if (!SetupDiEnumDeviceInfo(hDevInfo,SelectedItem,&DeviceInfoData))
{
printf("EnumDeviceInfo");
return FALSE;
}
//{4D36E972-E325-11CE-BFC1-08002BE10318}
// 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)))
{
printf("SetClassInstallParams");
SetCursor(hCursor);
return FALSE;
}
//
// Call the ClassInstaller and perform the change.
//
if (!SetupDiCallClassInstaller(DIF_PROPERTYCHANGE,
hDevInfo,
&DeviceInfoData))
{
printf("SetClassInstallParams");
SetCursor(hCursor);
return TRUE;
}
return TRUE;
}
通过改注册表来实现对电脑IP的更改,最后关键一步是对网卡重启,使IP更改生效,上面的函数在xp下用的好好的,win7x下就不行了 linux下可以执行ifconfig down 然后 up。win也有类似的吧 好像用net命令就可以,还要注意权限 试了一下,ipconfig 是用的管理员权限在win7下运行的么? tongdayusu 发表于 2016-7-31 10:59
是用的管理员权限在win7下运行的么?
是的 messagebox 提示操作成功其实没有 mcu5i51 发表于 2016-7-31 08:34
好像用net命令就可以,还要注意权限
xp win7系统都试过netsh指令可以重启网卡 但是通过vc代码调用时只能打开cmd.exe具体指令没有被执行 通过命令直接改ip和设置成dhcp
页:
[1]