通过WinAPI禁用/启用网络接口

时间:2013-06-26 22:16:47

标签: c++ winapi networking

以下是我在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);
}

问题是在Windows XP上完美运行在Win 7上不起作用 :(

你能帮我找一下这个错误吗?

非常感谢!

1 个答案:

答案 0 :(得分:4)

只需要在x64 OS上从64位进程调用该函数。 见http://msdn.microsoft.com/en-us/library/windows/hardware/ff541255(v=vs.85).aspx