1. 使用类似开源项目TCPIPManager的实现,命令行调用netsh来实现
int NetworkProfile::ApplyNetworkProfile(TTrayIcon *TrayIcon){
String Command;
// Applying TCP/IP settings!
TrayIcon->BalloonHint="Currently applying IP settings!";
if (!IsCurrentOSWindowsVista7()) { // Different commands are required for pre-XP (XP included) and post-XP operating systems!
if (this->ObtainIPAddressAutomatically) {
Command="netsh interface ip set address name=\""+this->NetworkConnectionName+"\" dhcp";
} else {
Command="netsh interface ip set address name=\""+this->NetworkConnectionName+"\" static "+this->IPAddress+" "+this->SubnetMask+" "+this->DefaultGateway+" "+this->PreferredDNSServer;
}
QuietExecuteCommand(Command);
TrayIcon->BalloonHint="Currently applying DNS settings!";
if (this->ObtainDNSServerAddressAutomatically) {
Command="netsh interface ip set dns name=\""+this->NetworkConnectionName+"\" dhcp";
}
else {
if (this->AlternateDNSServer!="") {
Command="netsh interface ip set dns name=\""+this->NetworkConnectionName+"\" static "+this->AlternateDNSServer;
QuietExecuteCommand(Command);
}
Command="netsh interface ip add dns name=\""+this->NetworkConnectionName+"\" "+this->PreferredDNSServer+" index=1";
}
QuietExecuteCommand(Command);
}
else { // The changes are ip <-> ipv4 and dns <-> dnsserver!
if (this->ObtainIPAddressAutomatically) {
Command="netsh interface ipv4 set address name=\""+this->NetworkConnectionName+"\" dhcp";
} else {
Command="netsh interface ipv4 set address name=\""+this->NetworkConnectionName+"\" static "+this->IPAddress+" "+this->SubnetMask+" "+this->DefaultGateway;
}
QuietExecuteCommand(Command);
TrayIcon->BalloonHint="Currently applying DNS settings!";
if (this->ObtainDNSServerAddressAutomatically) { // On Windows Vista using "dnsservers" fails while on Windows 7 it works. To suit both versions I use "dnsserver" which works on both operating systems!
Command="netsh interface ipv4 set dnsserver name=\""+this->NetworkConnectionName+"\" dhcp";
} else {
if (this->AlternateDNSServer!="") {
Command="netsh interface ipv4 set dnsserver name=\""+this->NetworkConnectionName+"\" static "+this->AlternateDNSServer;
QuietExecuteCommand(Command);
}
Command="netsh interface ipv4 add dnsserver name=\""+this->NetworkConnectionName+"\" "+this->PreferredDNSServer+" index=1";
}
QuietExecuteCommand(Command);
}
// Applying proxy settings!
TrayIcon->BalloonHint="Currently applying proxy settings!";
String RegistryPath="\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings";
TRegistry *RegistryKey=new TRegistry;
RegistryKey->RootKey=HKEY_CURRENT_USER;
RegistryKey->OpenKey(RegistryPath,true);
if (this->UseProxyServer) {
RegistryKey->WriteInteger("ProxyEnable",1);
RegistryKey->WriteString("ProxyServer",this->ProxyServerAddress+":"+this->ProxyServerPort);
if (this->BypassProxyServerForLocalAddresses) {
RegistryKey->WriteString("ProxyOverride","<local>");
} else {
RegistryKey->DeleteValue("ProxyOverride");
}
} else {
RegistryKey->WriteInteger("ProxyEnable",0);
RegistryKey->DeleteValue("ProxyServer");
RegistryKey->DeleteValue("ProxyOverride");
}
RegistryKey->CloseKey();
free(RegistryKey);
return 0;
}
2. 使用SetupDiCallClassInstaller函数实现,详见此帖。需要注意的地方是中文里面的一个Bug 讲解 中文 KB888609
- /*****************************************************************************
- 演示如何编程实现启用禁用网卡
- Mady By ZwelL
- 2004.7.29
- zwell@sohu.com
- *****************************************************************************/
- #include <windows.h>
- #include <setupapi.h>
- #include <tchar.h>
- #include <stdio.h>
- #pragma comment(lib,"ws2_32.lib")
- #pragma comment(lib,"setupapi.lib")
- BOOL DisableNetInterface(bool bStatus)
- {
- IN LPTSTR HardwareId ;
- //硬件ComponentId,注册表地址:system\currentcontrolset\class\{4D36E972-E325-11CE-BFC1-08002BE10318}\0000
- HardwareId="PCI\\VEN_10EC&DEV_8139&SUBSYS_813910EC" ;
- DWORD NewState ;
- if(bStatus)
- {
- NewState=DICS_DISABLE ;
- //禁用
- }
- else
- {
- NewState=DICS_ENABLE ;
- //启用
- }
- //调用ddk函数,来禁用网卡
- DWORD i,err ;
- BOOL Found=false ;
- HDEVINFO hDevInfo ;
- SP_DEVINFO_DATA spDevInfoData ;
- //访问系统的硬件库
- hDevInfo=SetupDiGetClassDevs(NULL,"PCI",NULL,DIGCF_ALLCLASSES|DIGCF_PRESENT);
- if(hDevInfo==INVALID_HANDLE_VALUE)
- {
- printf("访问系统硬件出错!");
- return false ;
- }
- //枚举硬件,获得需要的接口
- spDevInfoData.cbSize=sizeof(SP_DEVINFO_DATA);
- for(i=0;SetupDiEnumDeviceInfo(hDevInfo,i,&spDevInfoData);i++)
- {
- DWORD DataT ;
- LPTSTR p,buffer=NULL ;
- DWORD buffersize=0 ;
- //获得硬件的属性值
- while(!SetupDiGetDeviceRegistryProperty(
- hDevInfo,
- &spDevInfoData,
- SPDRP_HARDWAREID,
- &DataT,
- (PBYTE)buffer,
- buffersize,
- &buffersize))
- {
- if(GetLastError()==ERROR_INVALID_DATA)
- {
- //不存在HardwareID. Continue.
- break ;
- }
- else if(GetLastError()==ERROR_INSUFFICIENT_BUFFER)
- {
- //buffer size不对.
- if(buffer)
- LocalFree(buffer);
- buffer=(char*)LocalAlloc(LPTR,buffersize);
- }
- else
- {
- //未知错误
- goto cleanup_DeviceInfo ;
- }
- }
- if(GetLastError()==ERROR_INVALID_DATA)
- continue ;
- //比较,找到和网卡ID相同的项
- for(p=buffer;*p&&(p<&buffer[buffersize]);p+=lstrlen(p)+sizeof(TCHAR))
- {
- if(!_tcscmp(HardwareId,p))
- {
- //找到网卡
- Found=TRUE ;
- break ;
- }
- }
- if(buffer)
- LocalFree(buffer);
- //如果相等
- if(Found)
- {
- //禁用该设备
- SP_PROPCHANGE_PARAMS spPropChangeParams ;
- spPropChangeParams.ClassInstallHeader.cbSize=sizeof(SP_CLASSINSTALL_HEADER);
- spPropChangeParams.ClassInstallHeader.InstallFunction=DIF_PROPERTYCHANGE ;
- spPropChangeParams.Scope=DICS_FLAG_GLOBAL ;
- spPropChangeParams.StateChange=NewState ;
- //禁用:DICS_DISABLE,DICS_ENABLE启用
- //
- if(!SetupDiSetClassInstallParams(hDevInfo,&spDevInfoData,(SP_CLASSINSTALL_HEADER*)&spPropChangeParams,sizeof(spPropChangeParams)))
- {
- DWORD errorcode=GetLastError();
- }
- if(!SetupDiCallClassInstaller(DIF_PROPERTYCHANGE,hDevInfo,&spDevInfoData))
- {
- DWORD errorcode=GetLastError();
- }
- switch(NewState)
- {
- case DICS_DISABLE :
- printf("成功禁用网络!");
- break ;
- case DICS_ENABLE :
- printf("成功启用网络!");
- break ;
- }
- break ;
- }
- }
- //退出时,清理工作环境
- cleanup_DeviceInfo :
- err=GetLastError();
- SetupDiDestroyDeviceInfoList(hDevInfo);
- SetLastError(err);
- return true ;
- }
- void usage(char *exefile)
- {
- printf("Usage:%s [-e|-d]\r\n", exefile);
- printf("\t-e: Enable the network card.\r\n");
- printf("\t-d: Disable the network card.\r\n");
- exit(0);
- }
- int main(int argc,char**argv)
- {
- if(argc<2)
- usage(argv[0]);
- if(!DisableNetInterface((strstr(argv[1],"-d")>0?TRUE:FALSE)))
- printf("对网卡操作失败!");
- return 0;
- }
微信扫描下方的二维码阅读本文