需要一种 C++ 方法设置所有接口上的 DNS 服务器

发布于 2024-09-27 16:44:37 字数 938 浏览 4 评论 0原文

我正在用 C++ 为 Win XP/7 编写一个小托盘应用程序,其目标是只要该应用程序正在运行,http://*.dev 就会指向 http://localhost

这是 XAMPP/WAMP 模组的一部分,允许本地主机上以目录命名的虚拟域(例如 http: //test.dev/index.php 将映射到 G:\xampp\virtual\test\index.php)

其中一部分是在所有适配器上将 Posadis 作为 DNS 填充程序运行。我有托盘图标工作,以及其基本功能(启动时启动 posadis,退出时终止,监视 posadis 进程并在其终止时关闭,给出一个粗略的“关于”屏幕),但我仍然需要手动设置并取消设置我的网络适配器上的 DNS 服务器。

“手动”是指以下dos命令 netsh 接口 ip set dnsservers name="{适配器名称}" 源=静态地址=127.0.0.1 主 ... netsh interface ip set dnsservers name="{adapter name}" source=dhcp

当然,这很糟糕;可以这么说,如果您无法将 DNS 填充程序放入门框,那么运行 DNS 填充程序有什么用呢?

我正在研究的解决方案: 1)我可以让托盘应用程序在 cscript 中运行一个命令,该命令将从“netsh interface ip show faces”获取适配器列表,然后在该列表上运行注册命令。这看起来很不优雅。

2)找出我应该在C++中寻找什么地方来做这件事;具体来说,我需要一种方法来枚举系统的适配器,在程序启动时记录它们的配置,将它们的 DNS 定向到 127.0.0.1,并在程序结束时恢复它们的配置。

正确方向的箭头会很有帮助。还有一个示例片段。提前致谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若有似无的小暗淡 2024-10-04 16:44:37

您可以使用 WMI 配置 DNS在每个界面上。您可以使用 EnableDNSSetDNSServerSearchOrder 方法 Win32_NetworkAdapterConfiguration 类。您可以使用 DNSServerSearchOrder 属性获取当前设置。以下是一些资源。

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

注意:

如果当您决定采用此方法时,请注意 Windows XP 的一个已知问题。 知识库文章中对此进行了描述。

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 DNSServerSearchOrder property. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文