Programmatically Changing NameServer in Windows TCP/IP

杀马特。学长 韩版系。学妹 提交于 2019-12-22 09:48:38

问题


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 prepend 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!


回答1:


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=...


来源:https://stackoverflow.com/questions/1677154/programmatically-changing-nameserver-in-windows-tcp-ip

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!