问题描述
我试图想出一个解决方案来编程启用/禁用网卡 - 我做了大量的研究,似乎没有一个可行的解决方案,在XP和Vista环境。我在说的是,如果你进入控制面板的网络连接,右键单击一个,并选择启用或禁用。理想情况下,我想使用一个库,但如果更糟的是,我想我可以调用命令行应用程序,但这是绝对最坏的情况。这是我到目前为止所尝试的,以及他们失败的原因:这上一篇文章:
a href =http://stackoverflow.com/questions/83756/how-to-programmatically-enabledisable-network-interfaces-windows-xp> http://stackoverflow.com/questions/83756/how-to-programmatically -enabledisable-network-interfaces-windows-xp
列出了几种方法 - 第一种是使用netsh,这似乎与使用IPHelper函数SetIfEntry()。这个问题是它将接口设置为管理启用或禁用,而不是正常启用/禁用,因此它不会实际关闭NIC。
另一个解决方案建议使用WMI,特别是Win32_NetworkAdapter类,它具有启用和禁用方法:
好吧?在Vista中工作正常,这些方法不存在于正常的XP安装...
另一个建议是使用DevCon,它真正使用SetupAPI,特别是SetupDiSetClassInstallParams ()与DICS_ENABLE。在这个奇妙的类花了无数个小时,并试图在全局级别以及特定配置级别(和每个组合)禁用/启用设备后,它不能一直工作 - 有时工作正常,但其他时间
然后,我尝试使用INetConnection接口,特别是INetConnection-> Connect / Disconnect(连接/断开连接),然后在设备管理器中禁用该设备, :
但我从来没有得到这个对我的Vista或XP测试框上的连接有任何影响。
最后,我发现这个C#脚本叫ToggleNic:
p>
这看起来像是通过Shell有效地导致右键点击行为。 (至少对于这种实现)的限制是,它不工作(没有修改)非英语系统,我需要我的工作。公平地说,这个解决方案看起来是最可行的,但我对C#的熟悉程度很低,我找不到它使用的API是否在C ++中可用。
任何帮助或见解将非常感谢 - 或者如何完成什么C语言脚本在C ++中的想法。谢谢!
在更多平台和更多方法上测试后,我基本放弃了这项功能目的)。我的问题是,我想要有90%的情况下工作的东西,现实是,与我能够想出的一切,它接近70%。讽刺的是,它实际上正如通过正常的Windows方法片状。对于那些仍然想要走这条危险路径的人,这里是我发现的:
在上面描述的API直接方法中,最一致的方法是使用SetupAPI(SetupDiSetClassInstallParams) - 我遇到的最大的问题是,有时它会在一个状态,它将需要重新启动,没有更改将工作,直到发生。使用此功能时唯一需要注意的是,设备有两个配置文件,因此您需要在某些情况下切换它们。 DDK包含devcon工具的源,它显示了如何做所有事情。这最终看起来像是最接近右键单击,但它仍然表现出一些奇怪的行为,网络连接没有。这种方法似乎在大约70%的时间(在测试和测试系统)。
从总黑客方法,我发现最好的不是使用ToggleNIC做的技术,而是使用IShellFolder的东西 - 这允许你使用GetCommandString是语言无关的。问题是,在XP GetCommandString不返回任何东西(哦喜悦),但它似乎显示菜单ID的'enable'和'disable'是一致的(分别为16和17),所以如果我失败GetCommandString ,我刚刚回到菜单ID的。要切换,只需调用InvokeCommand与字符串,如果它返回一个,或菜单ID,如果没有。这个问题是,就像正常的Windows方式,有时它不工作,也不给你任何迹象表明发生了什么或为什么它失败。这种方法似乎在大约70%的时间工作,但是更难以告诉是否出了问题,加上正常的启用界面...文本会弹出。
希望这有助于任何人 - 如果任何人设法找到另一种方式,在更多的情况下,我很愿意听到!
I'm trying to come up with a solution to programatically 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:
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!
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!
这篇关于编程禁用/启用网络接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!