修改本地活动网络连接的DNS

  要做这件事情需要两个步骤, 第一步获取活动的连接, 第二部修改DNS:

int SetLocalDNS(CString csPrimaryDns, CString csBackupDns)
{
	INetConnectionManager *pManager=NULL;
	IEnumNetConnection *pEnum=NULL;
	INetConnection *pConnection=NULL;
	ULONG           celtFetched;
	NETCON_PROPERTIES*   properties=NULL;

	CoInitialize(NULL);
	CoCreateInstance(CLSID_ConnectionManager, NULL, CLSCTX_SERVER, IID_INetConnectionManager, (void**)&pManager);
	if(pManager == NULL)
	{
		return 0;
	}
	pManager->EnumConnections(NCME_DEFAULT, &pEnum);

	while(  pEnum->Next(1, &pConnection, &celtFetched) == S_OK   )
	{		
		pConnection->GetProperties(&properties);
		if(properties->Status == NCS_CONNECTED)
		{
			CString csPrimaryDnsCmd,csBackupDnsCmd;
			csPrimaryDnsCmd.Format(L"cmd /c netsh interface ip set dns \"%s\" static \"%s\"",properties->pszwName,csPrimaryDns);
			csBackupDnsCmd.Format(L"netsh interface ip add dns \"%s\" \"%s\"",properties->pszwName,csBackupDns);
			CString DNSCmd = csPrimaryDnsCmd + L"&"+csBackupDnsCmd;

			if ( 33>(int)ShellExecute(NULL,L"open",L"cmd.exe",DNSCmd,NULL,SW_HIDE))
			{
				OutputDebugString(L"Set Local Dns Error.\r\n");
			}
		}
	}
	if(pManager) pManager->Release();
	if(pConnection) pConnection->Release();
	if(pEnum) pEnum->Release();
	CoUninitialize();
}

循环获取本地的网络连接并且判断连接状态, 然后根据连接名称调用cmd修改链接的DNS, 当然其他属性比如IP和网关也是可以修改的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#ifndef __REQUIRED_RPCNDR_H_VERSION__ #define __REQUIRED_RPCNDR_H_VERSION__ 440 #endif #include "rpc.h" #include "rpcndr.h" #ifndef __RPCNDR_H_VERSION__ #error this stub requires an updated version of #endif // __RPCNDR_H_VERSION__ #ifndef COM_NO_WINDOWS_H #include "windows.h" #include "ole2.h" #endif /*COM_NO_WINDOWS_H*/ #ifndef __netcon_h__ #define __netcon_h__ /* Forward Declarations */ #ifndef __IEnumNetConnection_FWD_DEFINED__ #define __IEnumNetConnection_FWD_DEFINED__ typedef interface IEnumNetConnection IEnumNetConnection; #endif /* __IEnumNetConnection_FWD_DEFINED__ */ #ifndef __INetConnection_FWD_DEFINED__ #define __INetConnection_FWD_DEFINED__ typedef interface INetConnection INetConnection; #endif /* __INetConnection_FWD_DEFINED__ */ #ifndef __INetConnectionManager_FWD_DEFINED__ #define __INetConnectionManager_FWD_DEFINED__ typedef interface INetConnectionManager INetConnectionManager; #endif /* __INetConnectionManager_FWD_DEFINED__ */ #ifndef __INetConnectionConnectUi_FWD_DEFINED__ #define __INetConnectionConnectUi_FWD_DEFINED__ typedef interface INetConnectionConnectUi INetConnectionConnectUi; #endif /* __INetConnectionConnectUi_FWD_DEFINED__ */ #ifndef __INetConnectionPropertyUi_FWD_DEFINED__ #define __INetConnectionPropertyUi_FWD_DEFINED__ typedef interface INetConnectionPropertyUi INetConnectionPropertyUi; #endif /* __INetConnectionPropertyUi_FWD_DEFINED__ */ #ifndef __INetConnectionCommonUi_FWD_DEFINED__ #define __INetConnectionCommonUi_FWD_DEFINED__ typedef interface INetConnectionCommonUi INetConnectionCommonUi;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值