I’m writing a small tray app in C++ for Win XP/7, with the objective that as long as the app is running, http://*.dev will point to http://localhost.
This is part of a mod for XAMPP/WAMP to permit directory-named virtual domains on localhost (so, for example, http://test.dev/index.php will map to G:\xampp\virtual\test\index.php)
Part of this is running posadis as a DNS shim on all adapters. I have the tray icon working, as well as the basic functionality thereof (start posadis on startup, kill on exit, monitor the posadis process and close if it dies, give a cursory “about” screen), but I still have to manually set and unset the DNS server on my net adapters.
“Manually” means the following dos commands
netsh interface ip set dnsservers name=”{adapter name}” source=static address=127.0.0.1 primary
…
netsh interface ip set dnsservers name=”{adapter name}” source=dhcp
This is bad, of course; what good is it to run a DNS shim if you can’t get it in the door jamb, so to speak?
Solutions I’m looking into:
1) I could just have the tray app run a command in cscript that would get the list of adapters from “netsh interface ip show interfaces”, then run the registration commands over the list. This seems inelegant.
2) Figure out where I should be looking to do this in C++; specifically, I’ll need a way to enumerate the system’s adapters, to record their configuration at program start, to direct their DNS to 127.0.0.1, and to later restore their configuration at program end.
An arrow in the right direction would be helpful. An example snippet, moreso. Thanks in advance!
You can use WMI to configure DNS on each interface. You can do this by using the EnableDNS or SetDNSServerSearchOrder method of the Win32_NetworkAdapterConfiguration class. You can get the current setting using the
DNSServerSearchOrderproperty. Below are a couple of resources.http://www.activexperts.com/activmonitor/windowsmanagement/scripts/networking/client/configuring/#EDNSANAdapters.htm
http://msdn.microsoft.com/en-us/library/aa394558(v=VS.85).aspx
http://technet.microsoft.com/en-us/library/ee692588.aspx
NOTE:
If you decide to take this approach be mindful of a known issue with Windows XP. It is described in a KB article.