以编程方式禁用/启用网络接口
我正在尝试提出一个以编程方式启用/禁用网卡的解决方案 - 我已经进行了大量研究,但在 XP 和 Vista 环境中似乎没有一个可行的解决方案。 我所说的是,如果您进入控制面板“网络连接”,右键单击其中一个并选择启用或禁用。 理想情况下,我想使用一个库,但如果情况变得更糟,我想我可以调用命令行应用程序,但这绝对是最坏的情况。 这是我到目前为止所尝试过的以及失败的原因/地点:
上一篇文章:
如何以编程方式启用/禁用网络接口? (Windows XP)
列出了几种方法 - 第一种是使用 netsh,它看起来与使用 IPHelper 函数 SetIfEntry() 相同。 这样做的问题是,它将接口设置为管理启用或禁用,而不是正常启用/禁用,因此它实际上并没有关闭 NIC。
建议的另一个解决方案是使用 WMI,特别是 Win32_NetworkAdapter 类,它具有启用和禁用方法:
http://msdn.microsoft.com/en-us/library/aa394216(VS.85).aspx
很好,对吗? 在 Vista 中工作正常,这些方法在正常的 XP 安装中不存在...
另一个建议是使用 DevCon,它实际上使用了 SetupAPI,特别是带有 DICS_ENABLE 的 SetupDiSetClassInstallParams()。 在花了无数个小时学习这个精彩的课程,并尝试在全局级别以及特定配置级别(以及每个组合)禁用/启用设备后,它也不能始终如一地工作 - 有时工作正常,但有时工作正常在设备管理器中禁用该设备,但仍使其在网络连接中保持运行状态。
然后我尝试使用 INetConnection 接口,特别是 INetConnection->Connect/Disconnect:
http://msdn.microsoft.com/en-us/library/aa365084(VS.85).aspx
但我永远无法让这对我的连接产生任何影响Vista 或 XP 测试盒。
最后,我找到了这个名为 ToggleNic 的 C# 脚本:
http://channel9.msdn.com/playground /Sandbox/154712/
看起来它正在通过外壳程序以某种方式有效地导致右键单击行为。 限制(至少是这个实现)是它不能在非英语系统上工作(未经修改),我需要我的系统才能使用。 公平地说,这个解决方案看起来是最可行的,但我对 C# 的熟悉程度很低,而且我找不到它使用的 API 是否在 C++ 中可用。
任何帮助或见解将不胜感激 - 或者有关如何完成切换脚本在 C++ 中执行的操作的想法。 谢谢!
I'm trying to come up with a solution to programmatically enable/disable the network card - I've done a ton of research and nothing seems to be a workable solution in both XP and Vista environments. What I'm talking about is if you went into the Control Panel 'Network Connections', right clicked on one and picked either enable or disable. Ideally I'd like to use a library, but if worse comes to worse I supposed I could call out to a commandline app, but that's absolute worst case. Here's what I've tried so far and where/why they failed:
This previous post:
How to programmatically enable/disable network interfaces? (Windows XP)
Lists a couple of methods - the first is using netsh, which appears to be the same as using the IPHelper function SetIfEntry(). The problem with this is that it sets the interface as Administratively enabled or disable, not the normal enabled/disabled so it doesn't actually shut down the NIC.
Another solution proposed is using WMI and in particular Win32_NetworkAdapter class, which has an Enable and Disable method:
http://msdn.microsoft.com/en-us/library/aa394216(VS.85).aspx
Great right? Works fine in Vista, those methods don't exist in a normal XP install...
Another suggestion is to use DevCon, which really uses the SetupAPI, in particular SetupDiSetClassInstallParams() with the DICS_ENABLE. After spending countless hours with this wonderful class, and trying to disable/enable the device both at the global level as well as the specific configuration level (and every combination), it doesn't consistently work either - sometimes working fine, but other times disabling the device in the Device Manager, but still leaving it up and operational in the Network Connections.
I then tried using the INetConnection interface, specifically INetConnection->Connect/Disconnect:
http://msdn.microsoft.com/en-us/library/aa365084(VS.85).aspx
But I was never able to get this to have any effect on the connections on either my Vista or XP test boxes.
Finally, I found this C# script called ToggleNic:
http://channel9.msdn.com/playground/Sandbox/154712/
Which looks like it's going through the Shell somehow to effectively cause the right-click behavior. The limitation (at least of this implementation) is that it doesn't work (without modification) on non-English systems, which I need mine to work with. To be fair, this solution looks like the most viable, but my familiarity with C# is low and I couldn't find if the API it's using is available in C++.
Any help or insights would be greatly appreciated - or ideas on how to accomplish what the togglenic script does in C++. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
在对更多平台和更多方法进行测试之后,我基本上放弃了此功能(至少对于我的目的而言)。 对我来说,问题是我想要一种能在 90% 以上的情况下工作的东西,而现实是,我能想到的一切,它都接近 70%。 具有讽刺意味的是,通过普通的 Windows 方法,它实际上同样不稳定。 对于那些仍然想走这条危险道路的人,这是我发现的:
在上述 API 直接方法中,最有效的方法是使用 SetupAPI (SetupDiSetClassInstallParams) - 我遇到的最大问题是有时它会进入需要重新启动的状态,并且在发生这种情况之前任何更改都不会起作用。 使用此功能时唯一需要注意的是,设备有两个配置文件,因此在某些情况下您需要切换它们。 DDK 包含 devcon 工具的源代码,它向您展示了如何执行所有操作。 这最终看起来像是最接近右键单击的,但它仍然表现出一些网络连接没有的奇怪行为。 这种方法似乎在大约 70% 的时间内有效(在测试和测试系统上)。
从整个黑客方法来看,我发现最好的方法不是使用 ToggleNIC 所做的技术,而是使用 IShellFolder 的东西 - 这允许您使用与语言无关的 GetCommandString。 问题在于,在 XP 下 GetCommandString 不会返回任何内容(哦,高兴),但“启用”和“禁用”的菜单 ID 确实是一致的(分别为 16 和 17),所以如果我无法 GetCommandString ,我只是回到了菜单 ID。 要进行切换,只需调用 InvokeCommand,如果返回了字符串,则使用字符串;如果没有返回,则使用菜单 ID。 这样做的问题是,就像正常的 Windows 方式一样,有时它不起作用,也不会向您提供任何有关发生了什么情况或失败原因的指示。 这种方法似乎在 70% 的情况下也有效,但更难判断是否出现问题,而且会弹出正常的“启用接口...”文本。
希望这对其他人有帮助 - 如果有人设法找到另一种适用于更多情况的方法,我很乐意听到!
After testing on more platforms and more approaches, I've basically given up on this functionality (at least for my purposes). The problem for me is that I want to have something that works in 90%+ of the situations, and the reality is that with everything I could come up with, it's closer to 70%. The ironic thing is that it's actually just as flaky through the normal Windows method. For those who still want to go down this perilous path, here's what I found:
Of the API direct methods described above, the one which worked the most consistently was using the SetupAPI (SetupDiSetClassInstallParams) - the biggest problem I ran into with this is that sometimes it would get in a state where it would require a reboot and no changes would work until that happened. The only other thing to be aware of when using this is that there are two profiles for devices, so you need to toggle them both in some cases. The DDK contains the source to the devcon tool, which shows you exactly how to do everything. This ultimately looked like it was the closest to right-clicking, but it still exhibited some strange behavior that Network Connections didn't. This approach seemed to work about 70% of the time (in both tests and on test systems).
From the total hack approach, the best I found was not using the technique that ToggleNIC did, but instead use the IShellFolder stuff - this allows you to use GetCommandString which is language-independent. The problem with this is that under XP GetCommandString doesn't return anything (oh joy), but it did appear that the menu ID's for 'enable' and 'disable' were consistent (16 and 17 respectively), so if I failed to GetCommandString, I just fell back to the menu ID's. To Toggle, just call InvokeCommand with either the string if it returned one, or the menu ID if it didn't. The problem with this was that just like the normal Windows way, sometimes it doesn't work, nor does it give you any indication of what's going on or why it failed. This approach seemed to work about 70% of the time as well, but was much harder to tell if something went wrong, plus the normal "Enabling interface..." text would pop up.
Hopefully this helps anyone else - and if anyone manages to find another way that works in more situations, I'd love to hear it!
此处尝试“Shell 网络接口”C++ 代码一个>。 这应该在 XP 及更高版本下工作。
Try the "Shell Network Interfaces" C++-code here. This should work under XP and higher.
经过大量搜索后,我开始深入研究 DEVCON。 在意识到这些方法必须是可调用的之后,我做了一些搜索并发现了这个:
http://www.codeproject.com/KB/cs/HardwareHelper.aspx
它至少帮助解决了我从 OpenVPN 启用和禁用 TAP 适配器的问题。
after much searching I started to dig into DEVCON. After realizing that those methods had to be pinvokable I did a little search and found this:
http://www.codeproject.com/KB/cs/HardwareHelper.aspx
It helped solve my problem of enabling and disabling the TAP adapter from OpenVPN at least.
ToggleNic 通过 COM 使用 Shell32。
在c中,这由标准库“SHLOBJ.H”“SHELLAPI.H”和类IShellDispatch使用。
ToggleNic uses Shell32 through COM.
In c this uses by standard libraries "SHLOBJ.H" "SHELLAPI.H" and class IShellDispatch.
找到了这个基本上是右键单击自动化的解决方案: http://www.wilderssecurity.com/ showthread.php?t=265836
适用于 Windows XP 和 Windows 7
还有一个由 Novell 编写的,它是 DevCon 之上的包装器:
http://www.novell.com/communities/node /2338/网络配置命令行控制
Found this solution that is basically automation of right click: http://www.wilderssecurity.com/showthread.php?t=265836
Works both on Windows XP and Windows 7
There is also one written by Novell, that is a wrapper above DevCon:
http://www.novell.com/communities/node/2338/network-configuration-command-line-control