#include <Windows.h>
#include <SetupAPI.h>
#include <devguid.h>
#include <Cfgmgr32.h>
#pragma comment(lib,"Setupapi.lib")
ULONG NetCardIndex[16];
//参数为FALSE可以将活动网卡禁用,而后用参数TRUE调用
//可以将之前被禁用的网卡启动,并且不会启动非本函数禁用的网卡。
//该方法不支持WOW64,在64位系统中必须以64位代码执行。
ULONG ChangeNetCardStatus(BOOL Enable)
{
HDEVINFO DeviceInfoSet;
SP_DEVINFO_DATA DeviceInfoData;
ULONG DeviceIndex;
CHAR Buffer[64];
DWORD Size;
ULONG Status;
ULONG ProblemNumber;
SP_PROPCHANGE_PARAMS PropchangeParams;
DWORD ChangedCount;
ChangedCount = 0;
DeviceInfoSet = SetupDiGetClassDevsA(
&GUID_DEVCLASS_NET,
NULL,
NULL,
DIGCF_PRESENT);
SecureZeroMemory(&DeviceInfoData, sizeof(SP_DEVINFO_DATA));
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
DeviceIndex = 0;
while (SetupDiEnumDeviceInfo(
DeviceInfoSet,
DeviceIndex,
&DeviceInfoData) && DeviceIndex < 16)
{
DeviceIndex++;
if (Enable && NetCardIndex[DeviceIndex - 1] == 0)
continue;
if (!SetupDiGetDeviceRegistryPropertyA(
DeviceInfoSet,
&DeviceInfoData,
SPDRP_LOCATION_INFORMATION,
NULL,
(PBYTE)&Buffer,
sizeof(Buffer),
&Size))
{
continue;
}
if (CR_SUCCESS != CM_Get_DevNode_Status(&Status, &ProblemNumber, DeviceInfoData.DevInst, 0))
continue;
//if (Status & DN_WILL_BE_REMOVED)
// continue;
if (Enable == FALSE && Status & DN_HAS_PROBLEM && ProblemNumber == CM_PROB_DISABLED)
continue;
PropchangeParams.ClassInstallHeader.cbSize = sizeof(SP_CLASSINSTALL_HEADER);
PropchangeParams.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;
PropchangeParams.Scope = DICS_FLAG_GLOBAL;
PropchangeParams.StateChange = Enable ? DICS_ENABLE : DICS_DISABLE;
if (FALSE == SetupDiSetClassInstallParamsA(DeviceInfoSet, &DeviceInfoData, (PSP_CLASSINSTALL_HEADER)&PropchangeParams, sizeof(PropchangeParams)))
{
printf("SetupDiSetClassInstallParamsA: %08X\n", GetLastError());
continue;
}
if (FALSE == SetupDiCallClassInstaller(DIF_PROPERTYCHANGE, DeviceInfoSet, &DeviceInfoData))
{
printf("SetupDiCallClassInstaller: %08X\n", GetLastError());
}
NetCardIndex[DeviceIndex - 1] = 1;
ChangedCount++;
}
if (DeviceInfoSet) {
SetupDiDestroyDeviceInfoList(DeviceInfoSet);
}
return ChangedCount;
}
int main()
{
SecureZeroMemory(&NetCardIndex, sizeof(NetCardIndex));
ChangeNetCardStatus(FALSE);
SleepEx(10 * 1000, TRUE);
ChangeNetCardStatus(TRUE);
return 0;
}
禁用所有活动网卡(支持还原)
由
·
发表回复