Skip to content

Instantly share code, notes, and snippets.

@kurotych
Last active April 14, 2019 22:56
Show Gist options
  • Save kurotych/fdac790665950cf4345673ec0a455e6c to your computer and use it in GitHub Desktop.
Save kurotych/fdac790665950cf4345673ec0a455e6c to your computer and use it in GitHub Desktop.
set dns addr in Windows
// example adapterName : {54BB6B17-91C0-4F84-83B2-DE82C525425C}
bool setDNS(QString sAdapterName, QString sDNS)
{
HKEY hKey;
QString strKeyName = "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces\\" + sAdapterName;
if( RegOpenKeyEx(HKEY_LOCAL_MACHINE,
strKeyName.toStdWString().c_str(),
0,
KEY_WRITE,
&hKey) != ERROR_SUCCESS )
return false;
TCHAR Buffer[20];
size_t convertedChars = 0;
mbstowcs_s(&convertedChars, Buffer, sDNS.length() + 1, sDNS.toLatin1().data(), _TRUNCATE);
convertedChars *= sizeof(TCHAR);
RegSetValueEx(hKey, L"NameServer", 0, REG_SZ, reinterpret_cast<LPBYTE>(Buffer), convertedChars);
RegCloseKey(hKey);
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment