C++ Add DNS entry into network adapter
I have to programatically add DNS server addre开发者_如何转开发ss in network adapter settings on windows. Programming language is C++.
You can take a look to the IP Helper API
You can find how to use it There
Setting DNS using iphelp and register at CodeProject.
bool RegSetDNS(LPCTSTR lpszAdapterName, LPCTSTR pDNS)
{
HKEY hKey;
string strKeyName = "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces\\";
strKeyName += lpszAdapterName;
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,
strKeyName.c_str(),
0,
KEY_WRITE,
&hKey) != ERROR_SUCCESS)
return false;
char mszDNS[100];
strncpy(mszDNS, pDNS, 98);
int nDNS;
nDNS = strlen(mszDNS);
*(mszDNS + nDNS + 1) = 0x00; // REG_MULTI_SZ need add one more 0
nDNS += 2;
RegSetValueEx(hKey, "NameServer", 0, REG_SZ, (unsigned char*)mszDNS, nDNS);
RegCloseKey(hKey);
return true;
}
精彩评论