rem 禁用网卡
netsh interface set interface 以太网 disabled
rem 启用网卡
netsh interface set interface 以太网 enabled
三、以下是网上找到的C++代码,调试OK,需要管理员模式,requireAdministrator (/level=‘requireAdministrator‘),不知道原文地址了,着时尴尬
方法一,VS2015 ANSI
[cpp]
// lan2.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <stdio.h>
#include <windows.h>
#include <Setupapi.h>
#include <objbase.h>
#pragma comment(lib, "Setupapi.lib")
void EnumDevices(HDEVINFO hDevInfo)
{
SP_DEVINFO_DATA DeviceInfoData = { sizeof(SP_DEVINFO_DATA) };
LPOLESTR guid;
char devName[128];
char instanceId[128];
for (int i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData); i++)
{
StringFromCLSID(DeviceInfoData.ClassGuid, &guid);
SetupDiClassNameFromGuid(&DeviceInfoData.ClassGuid, devName, 128, NULL);
//printf("%s\n", devName);
; if (!strcmp(devName, "Net"))
{
SetupDiGetDeviceInstanceId(hDevInfo, &DeviceInfoData, instanceId, 128, NULL);
//printf("instanceId1:%s\n", instanceId);
if (!strncmp(instanceId, "PCI", 3))
{
//wprintf(L"guid:%s\n", guid);
// printf("devName:%s\n", devName);
// printf("instanceId2:%s\n", instanceId);
SP_PROPCHANGE_PARAMS params = { sizeof(SP_CLASSINSTALL_HEADER) };
params.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;
params.Scope = DICS_FLAG_CONFIGSPECIFIC;
<span style="color:#ff0000;"> params.StateChange = DICS_DISABLE;
</span><span style="color:#33ff33;"> //params.StateChange = DICS_ENABLE;
</span> params.HwProfile = 0;
SetupDiSetClassInstallParams(hDevInfo, &DeviceInfoData, (SP_CLASSINSTALL_HEADER*)¶ms, sizeof(SP_PROPCHANGE_PARAMS));
SetupDiChangeState(hDevInfo, &DeviceInfoData);
}
}
CoTaskMemFree(guid);
}
}
int main(int argc, char* argv[])
{
HDEVINFO hDevInfo = INVALID_HANDLE_VALUE;
hDevInfo = SetupDiGetClassDevs(NULL, NULL, NULL, DIGCF_PRESENT | DIGCF_ALLCLASSES);
if (INVALID_HANDLE_VALUE == hDevInfo)
return GetLastError();
EnumDevices(hDevInfo);
SetupDiDestroyDeviceInfoList(hDevInfo);
return 0;
}
UNICODE mode
[cpp]
// lan2.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <stdio.h>
#include <windows.h>
#include <Setupapi.h>
#include <objbase.h>
#pragma comment(lib, "Setupapi.lib")
int main(int argc, char* argv[])
{
HDEVINFO hDevInfo = INVALID_HANDLE_VALUE;
hDevInfo = SetupDiGetClassDevs(NULL, NULL, NULL, DIGCF_PRESENT | DIGCF_ALLCLASSES);
if (INVALID_HANDLE_VALUE == hDevInfo)
return GetLastError();
//EnumDevices(hDevInfo);
SP_DEVINFO_DATA DeviceInfoData = { sizeof(SP_DEVINFO_DATA) };
LPOLESTR guid;
wchar_t devName[128];
wchar_t instanceId[128];
for (int i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData); i++)
{
StringFromCLSID(DeviceInfoData.ClassGuid, &guid);
SetupDiClassNameFromGuid(&DeviceInfoData.ClassGuid, devName, 128, NULL);
// printf("%s\n", devName);
if (!wcscmp(devName, _T("Net")))
{
SetupDiGetDeviceInstanceId(hDevInfo, &DeviceInfoData, instanceId, 128, NULL);
//printf("instanceId1:%s\n", instanceId);
if (!wcsncmp(instanceId, _T("PCI"), 3))
{
//wprintf(L"guid:%s\n", guid);
// printf("devName:%s\n", devName);
// printf("instanceId2:%s\n", instanceId);