以编程方式禁用/启用网络接口 [英] Programmatically disable/enable network interface

查看:160
本文介绍了以编程方式禁用/启用网络接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试提出一种以编程方式启用/禁用网卡的解决方案-我进行了大量研究,在XP和Vista环境中似乎都没有可行的解决方案.我要说的是,如果您进入控制面板"的网络连接",右键单击一个,然后选择启用"或禁用".理想情况下,我想使用一个库,但是如果情况变得更糟,我以为我可以调出命令行应用程序,但这绝对是最糟糕的情况.到目前为止,这是我尝试过的事情以及失败的原因/原因:

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:

上一篇文章:

如何以编程方式启用/禁用网络接口? (Windows XP)

列出了几种方法-第一种是使用netsh,这似乎与使用IPHelper函数SetIfEntry()相同.问题在于,它将接口设置为管理上启用"或禁用",而不是正常启用/禁用,因此它实际上并未关闭NIC.

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.

提出的另一种解决方案是使用WMI,尤其是Win32_NetworkAdapter类,该类具有Enable和Disable方法:

Another solution proposed is using WMI and in particular Win32_NetworkAdapter class, which has an Enable and Disable method:

http://msdn.microsoft.com/zh-cn/library/aa394216(VS.85).aspx

对吧?在Vista中可以正常工作,而在普通的XP安装中不存在这些方法...

Great right? Works fine in Vista, those methods don't exist in a normal XP install...

另一个建议是使用DevCon,它实际上使用SetupAPI,尤其是带有DICS_ENABLE的SetupDiSetClassInstallParams().在花了无数个小时参加这个精彩的课程之后,并尝试在全局级别和特定配置级别(以及每种组合)上禁用/启用设备,但它也不能始终如一地工作-有时工作得很好,但有时在设备管理器"中禁用该设备,但仍保留该设备并在网络连接"中正​​常运行.

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.

然后我尝试使用INetConnection接口,特别是INetConnection-> Connect/Disconnect:

I then tried using the INetConnection interface, specifically INetConnection->Connect/Disconnect:

http://msdn.microsoft.com/zh-cn/library/aa365084(VS.85).aspx

但是我始终无法使它对我的Vista或XP测试箱上的连接产生任何影响.

But I was never able to get this to have any effect on the connections on either my Vista or XP test boxes.

最后,我找到了这个名为ToggleNic的C#脚本:

Finally, I found this C# script called ToggleNic:

http://channel9.msdn.com/playground/Sandbox/154712/

看起来它正在某种程度上通过外壳程序有效地导致右键单击行为.局限性(至少是此实现方式的限制)是,它在非英语系统上不起作用(未经修改),我需要与之配合使用.公平地讲,该解决方案看起来是最可行的,但是我对C#的熟悉程度很低,而且我无法确定它使用的API是否在C ++中可用.

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

我们将不胜感激任何帮助或见解-或有关如何完成C脚本语言的想法.谢谢!

Any help or insights would be greatly appreciated - or ideas on how to accomplish what the togglenic script does in C++. Thanks!

推荐答案

在更多平台和更多方法上进行测试之后,我基本上已经放弃了此功能(至少出于我的目的).对我来说,问题是我希望在90%以上的情况下都能使用某些东西,而现实是,我想出的一切都接近70%.具有讽刺意味的是,通过常规Windows方法,它实际上也很脆弱.对于那些仍然想走这条危险道路的人,这是我发现的东西:

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:

在上述API直接方法中,最一致的方法是使用SetupAPI(SetupDiSetClassInstallParams)-我遇到的最大问题是,有时它会处于需要重新启动并重新启动的状态在这种情况发生之前,任何更改都不会起作用.使用此功能时要注意的唯一另一件事是设备有两个配置文件,因此在某些情况下需要同时切换它们. DDK包含devcon工具的源代码,该工具确切地向您显示了如何执行所有操作.最终看起来好像是最接近右键单击的地方,但是它仍然表现出一些网络连接所没有的奇怪行为.这种方法似乎在大约70%的时间内都有效(在测试和测试系统中).

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

从全部黑客方法中,我发现最好的方法不是使用ToggleNIC的技术,而是使用IShellFolder的东西-这使您可以使用独立于语言的GetCommandString.问题在于,在XP下,GetCommandString不会返回任何内容(很高兴),但是"enable"和"disable"的菜单ID确实是一致的(分别为16和17),所以如果我对GetCommandString失败的话,我只是回到菜单ID.要进行切换,只需调用InvokeCommand即可,如果返回一个字符串,则调用该字符串;如果未返回,则使用菜单ID.问题是,就像普通的Windows方式一样,有时它不起作用,也不能给您任何提示,说明发生了什么或为什么失败.这种方法似乎也可以在大约70%的时间内起作用,但是很难判断出什么问题了,而且还会弹出普通的"Enabling interface ..."文本.

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!

这篇关于以编程方式禁用/启用网络接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆