Programmatically Changing NameServer in Windows TCP/IP
I am building a VPN application where I need to set DNS server programmatically. I am changing NameServer
parameter in the registry (in HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces
) to pre开发者_运维知识库pend a name server to the list of existing entries. Once I do this, if I try to look up a host name using nslookup
, it works fine, but ping
is not able to resolve the name - for 15 minutes. And after 15 mins ping is also able to resolve the name. I've tried my own little TCP/IP app, and that has the same issue. I looked at the traffic in wireshark, and I see that nslookup is sending dns lookup requests, while ping isn't.
Is there anyway I can kick windows to notice the name server earlier? Or is there a better way to set name servers in windows?
Thanks in advance for your help!
Don't change that registry key directly. As you've found, it doesn't notify DNS clients that the nameserver has changed.
The easiest solution is probably %SystemRoot%\system32\netsh.exe
.
netsh interface ip set dns name="Local Area Connection" source=static addr=...
精彩评论