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!
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!