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
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
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!
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);
'==========================================================================
' 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"
Try the "Shell Network Interfaces" C++-code here. This should work under XP and higher.
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.