Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disabling/enabling network interfaces via WinAPI

Here is my attempt to disable/enable network adapters on Windows:

void EnableNetDevice(bool aState, int index)
{
  HDEVINFO NetPnPHandle;
  SP_PROPCHANGE_PARAMS PCHP;
  SP_DEVINFO_DATA DeviceData;
  NetPnPHandle = SetupDiGetClassDevs(&GUID_DEVCLASS_NET, 0, 0, DIGCF_PRESENT);

  if (NetPnPHandle == INVALID_HANDLE_VALUE)
  {
        return;
  }

  DeviceData.cbSize = sizeof(SP_DEVINFO_DATA);
  SetupDiEnumDeviceInfo(NetPnPHandle, index, &DeviceData);
  PCHP.ClassInstallHeader.cbSize = sizeof(SP_CLASSINSTALL_HEADER);

  if (SetupDiSetClassInstallParams(NetPnPHandle,&DeviceData,&PCHP.ClassInstallHeader,sizeof(SP_PROPCHANGE_PARAMS)))
  {
        PCHP.ClassInstallHeader.cbSize = sizeof(SP_CLASSINSTALL_HEADER);
        PCHP.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;
        PCHP.HwProfile = 0;
        PCHP.Scope = DICS_FLAG_CONFIGSPECIFIC;
        if (aState) PCHP.StateChange = DICS_ENABLE;
        else  PCHP.StateChange = DICS_DISABLE;
        SetupDiSetClassInstallParams(NetPnPHandle,&DeviceData,&PCHP.ClassInstallHeader,sizeof(SP_PROPCHANGE_PARAMS));
        SetupDiCallClassInstaller(DIF_PROPERTYCHANGE,NetPnPHandle,&DeviceData);
  }

  DeviceData.cbSize = sizeof(SP_DEVINFO_DATA);
  SetupDiDestroyDeviceInfoList(NetPnPHandle);
}

The problem is it works perfectly on Windows XP but doesn't work on Win 7 :(

Could you help me please find the bug?

Thank you very much!

like image 512
The_Immortal Avatar asked Jun 26 '13 22:06

The_Immortal


People also ask

How do I disable enable network interfaces on the command line in Windows?

Type netsh interface show interface and press Enter. Remember the interface name of the network adapter you want to disable or enable. To disable a network connection, use the netsh interface set interface “Wi-Fi 12” disable command (replace the one highlighted in red with the name of the network adapter interface).

How do I remove old network connections from my registry?

Right-click the network adapter interface name (the long alphanumeric string) in the Windows Registry and select Delete.


1 Answers

It's simply a requirement to call the function from a 64-bit process on x64 OS. See http://msdn.microsoft.com/en-us/library/windows/hardware/ff541255(v=vs.85).aspx

like image 190
White hawk Avatar answered Oct 21 '22 22:10

White hawk