title: Programmatically disable/enable network interface
body: <p>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:</p>
  
  <p>This previous post:</p>
  
  <p><a href="https://stackoverflow.com/questions/83756/how-to-programmatically-enabledisable-network-interfaces-windows-xp">How to programmatically enable/disable network interfaces? (Windows XP)</a></p>
  
  <p>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.</p>
  
  <p>Another solution proposed is using WMI and in particular Win32_NetworkAdapter class, which has an Enable and Disable method:</p>
  
  <p><a href="http://msdn.microsoft.com/en-us/library/aa394216(VS.85).aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/aa394216(VS.85).aspx</a></p>
  
  <p>Great right?  Works fine in Vista, those methods don't exist in a normal XP install...</p>
  
  <p>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.</p>
  
  <p>I then tried using the INetConnection interface, specifically INetConnection->Connect/Disconnect:</p>
  
  <p><a href="http://msdn.microsoft.com/en-us/library/aa365084(VS.85).aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/aa365084(VS.85).aspx</a></p>
  
  <p>But I was never able to get this to have any effect on the connections on either my Vista or XP test boxes.</p>
  
  <p>Finally, I found this C# script called ToggleNic:</p>
  
  <p><a href="http://channel9.msdn.com/playground/Sandbox/154712/" rel="nofollow noreferrer">http://channel9.msdn.com/playground/Sandbox/154712/</a></p>
  
  <p>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++.</p>
  
  <p>Any help or insights would be greatly appreciated - or ideas on how to accomplish what the togglenic script does in C++.  Thanks!</p>