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!
See Question&Answers more detail:
os