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

  1. /*****************************************************************************
  2. 演示如何编程实现启用禁用网卡
  3. Mady By ZwelL
  4. 2004.7.29
  5. zwell@sohu.com
  6. *****************************************************************************/
  7. #include <windows.h>
  8. #include <setupapi.h>
  9. #include <tchar.h>
  10. #include <stdio.h>
  11. #pragma comment(lib,”ws2_32.lib”)
  12. #pragma comment(lib,”setupapi.lib”)
  13. BOOL DisableNetInterface(bool bStatus)
  14. {
  15.     IN LPTSTR HardwareId ;
  16.     //硬件ComponentId,注册表地址:system\currentcontrolset\class\{4D36E972-E325-11CE-BFC1-08002BE10318}\0000
  17.     HardwareId=”PCI\\VEN_10EC&DEV_8139&SUBSYS_813910EC” ;
  18.     DWORD NewState ;
  19.     if(bStatus)
  20.     {
  21.         NewState=DICS_DISABLE ;
  22.         //禁用
  23.     }
  24.     else
  25.     {
  26.         NewState=DICS_ENABLE ;
  27.         //启用
  28.     }
  29.     //调用ddk函数,来禁用网卡
  30.     DWORD i,err ;
  31.     BOOL Found=false ;
  32.     HDEVINFO hDevInfo ;
  33.     SP_DEVINFO_DATA spDevInfoData ;
  34.     //访问系统的硬件库
  35.     hDevInfo=SetupDiGetClassDevs(NULL,”PCI”,NULL,DIGCF_ALLCLASSES|DIGCF_PRESENT);
  36.     if(hDevInfo==INVALID_HANDLE_VALUE)
  37.     {
  38.         printf(“访问系统硬件出错!”);
  39.         return false ;
  40.     }
  41.     //枚举硬件,获得需要的接口
  42.     spDevInfoData.cbSize=sizeof(SP_DEVINFO_DATA);
  43.     for(i=0;SetupDiEnumDeviceInfo(hDevInfo,i,&spDevInfoData);i++)
  44.     {
  45.         DWORD DataT ;
  46.         LPTSTR p,buffer=NULL ;
  47.         DWORD buffersize=0 ;
  48.         //获得硬件的属性值
  49.         while(!SetupDiGetDeviceRegistryProperty(
  50.     hDevInfo,
  51.     &spDevInfoData,
  52.     SPDRP_HARDWAREID,
  53.     &DataT,
  54.     (PBYTE)buffer,
  55.     buffersize,
  56.     &buffersize))
  57.         {
  58.             if(GetLastError()==ERROR_INVALID_DATA)
  59.             {
  60.                 //不存在HardwareID. Continue.
  61.                 break ;
  62.             }
  63.             else if(GetLastError()==ERROR_INSUFFICIENT_BUFFER)
  64.             {
  65.                 //buffer size不对.
  66.                 if(buffer)
  67.                 LocalFree(buffer);
  68.                 buffer=(char*)LocalAlloc(LPTR,buffersize);
  69.             }
  70.             else
  71.             {
  72.                 //未知错误
  73.                 goto cleanup_DeviceInfo ;
  74.             }
  75.         }
  76.         if(GetLastError()==ERROR_INVALID_DATA)
  77.         continue ;
  78.         //比较,找到和网卡ID相同的项
  79.         for(p=buffer;*p&&(p<&buffer[buffersize]);p+=lstrlen(p)+sizeof(TCHAR))
  80.         {
  81.             if(!_tcscmp(HardwareId,p))
  82.             {
  83.                 //找到网卡
  84.                 Found=TRUE ;
  85.                 break ;
  86.             }
  87.         }
  88.         if(buffer)
  89.         LocalFree(buffer);
  90.         //如果相等
  91.         if(Found)
  92.         {
  93.             //禁用该设备
  94.             SP_PROPCHANGE_PARAMS spPropChangeParams ;
  95.             spPropChangeParams.ClassInstallHeader.cbSize=sizeof(SP_CLASSINSTALL_HEADER);
  96.             spPropChangeParams.ClassInstallHeader.InstallFunction=DIF_PROPERTYCHANGE ;
  97.             spPropChangeParams.Scope=DICS_FLAG_GLOBAL ;
  98.             spPropChangeParams.StateChange=NewState ;
  99.             //禁用:DICS_DISABLE,DICS_ENABLE启用
  100.             //
  101.             if(!SetupDiSetClassInstallParams(hDevInfo,&spDevInfoData,(SP_CLASSINSTALL_HEADER*)&spPropChangeParams,sizeof(spPropChangeParams)))
  102.             {
  103.                 DWORD errorcode=GetLastError();
  104.             }
  105.             if(!SetupDiCallClassInstaller(DIF_PROPERTYCHANGE,hDevInfo,&spDevInfoData))
  106.             {
  107.                 DWORD errorcode=GetLastError();
  108.             }
  109.             switch(NewState)
  110.             {
  111.                 case DICS_DISABLE :
  112.      printf(“成功禁用网络!”);
  113.                 break ;
  114.                 case DICS_ENABLE :
  115.      printf(“成功启用网络!”);
  116.                 break ;
  117.             }
  118.             break ;
  119.         }
  120.     }
  121.     //退出时,清理工作环境
  122.     cleanup_DeviceInfo :
  123.     err=GetLastError();
  124.     SetupDiDestroyDeviceInfoList(hDevInfo);
  125.     SetLastError(err);
  126.     return true ;
  127. }
  128. void usage(char *exefile)
  129. {
  130. printf(“Usage:%s [-e|-d]\r\n”, exefile);
  131. printf(“\t-e: Enable the network card.\r\n”);
  132. printf(“\t-d: Disable the network card.\r\n”);
  133. exit(0);
  134. }
  135. int main(int argc,char**argv)
  136. {
  137. if(argc<2)
  138.   usage(argv[0]);
  139. if(!DisableNetInterface((strstr(argv[1],”-d”)>0?TRUE:FALSE)))
  140.   printf(“对网卡操作失败!”);
  141.     return 0;
  142. }

 

Loading



微信扫描下方的二维码阅读本文

作者 arcko

新长征路上的码农下班买菜中

发表回复

此站点使用 Akismet 来减少垃圾评论。了解我们如何处理您的评论数据