问题
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!
回答1:
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!
回答2:
Try the "Shell Network Interfaces" C++-code here. This should work under XP and higher.
回答3:
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.
回答4:
'==========================================================================
' NAME: 1unconnectedNIC.vbs
'
' COMMENT: This file will disable and rename the unconnected NIC on the
' local server.
'
' Usage:
' cscript 1unconnectedNIC.vbs
' Alex John
'==========================================================================
strComputer = "."
Set objShell = CreateObject("WScript.Shell")
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colAdapters = objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapter where NetConnectionStatus = '7'" )
'For every adapter that is disconnected (has a red X by it)
For Each objAdapter in colAdapters
Wscript.Echo "Net Connection ID: " & objAdapter.NetConnectionID
Wscript.Echo "....Disabling NIC"
'Disable the NIC
objShell.Run "netsh interface set interface name=" & chr(34) & objAdapter.NetConnectionID & chr(34) & " admin=disabled", 0, true
Wscript.Echo "....NIC Disabled"
WScript.Sleep 10000
'Rename the NIC
Wscript.Echo "....Renaming NIC to 'Not Used'"
objShell.Run "netsh interface set interface name=" & chr(34) & objAdapter.NetConnectionID & chr(34) & " newname=" & chr(34) & "Not Used" & n & chr(34), 0, true
Next
Wscript.Echo "Finished"
回答5:
ManagementClass managementClass = new ManagementClass("Win32_NetworkAdapter");
ManagementObjectCollection mgmtObjectColl = managementClass.GetInstances();
ManagementObject myObject = null;
foreach (ManagementObject mgmtObject in mgmtObjectColl)
{
if (mgmtObject["NetConnectionID"] != null && mgmtObject["NetConnectionID"].Equals("Local Area Connection"))
{
Console.WriteLine("found");
myObject = mgmtObject;
object result = mgmtObject.InvokeMethod("Disable", null);
}
//Console.WriteLine("{0}, {1}", mgmtObject["Name"], mgmtObject["NetConnectionID"]);
}
object result3 = myObject.InvokeMethod("Enable", null);
回答6:
ToggleNic uses Shell32 through COM. In c this uses by standard libraries "SHLOBJ.H" "SHELLAPI.H" and class IShellDispatch.
回答7:
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
来源:https://stackoverflow.com/questions/860673/programmatically-disable-enable-network-interface