重启网卡的几种方法

1.重启windows网卡命令

rem 禁用网卡
netsh interface set interface 本地连接 disabled
rem 启用网卡

netsh interface set interface 本地连接 enabled

2.根据相关信息作出如下修改

重启网卡的几种方法_第1张图片


rem 禁用网卡
netsh interface set interface 以太网 disabled
rem 启用网卡

netsh interface set interface 以太网 enabled


三、以下是网上找到的C++代码,

调试OK,需要管理员模式,requireAdministrator (/level='requireAdministrator'),不知道原文地址了,着时尴尬

方法一,VS2015 ANSI

// lan2.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

#include 
#include 
#include 
#include 
#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;
				 params.StateChange = DICS_DISABLE;
				//params.StateChange = DICS_ENABLE;
				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

// lan2.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

#include 
#include 
#include 
#include 
#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);
				SP_PROPCHANGE_PARAMS params = { sizeof(SP_CLASSINSTALL_HEADER) };
				params.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;
				params.Scope = DICS_FLAG_CONFIGSPECIFIC;
				 // params.StateChange = DICS_DISABLE;
				params.StateChange = DICS_ENABLE;
				params.HwProfile = 0;
				SetupDiSetClassInstallParams(hDevInfo, &DeviceInfoData, (SP_CLASSINSTALL_HEADER*)¶ms, sizeof(SP_PROPCHANGE_PARAMS));
				SetupDiChangeState(hDevInfo, &DeviceInfoData);
			}
		}
		CoTaskMemFree(guid);
	}
	SetupDiDestroyDeviceInfoList(hDevInfo);
	return 0;
}


方法二.VS2015 UNICODE

SetupDiCallClassInstaller函数需要编译64位程序运行64位系统

// LAN.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

#include 
#include 
#include 
#include 
#define ERROR_IN_WOW64 (APPLICATION_ERROR_MASK|ERROR_SEVERITY_ERROR|0x235)
#pragma comment(lib,"setupapi.lib")

DWORD CtrlNetcard(BOOL bStatus);

void _tmain(int arg, TCHAR** argv)
{
	BOOL b = FALSE;
	DWORD err = 0;
	if (2 == arg)
	{
		argv++;
		if (!_tcsicmp(TEXT("enable"), *argv))
		{
			b = TRUE;
			err = CtrlNetcard(TRUE);
		}
		else if (!_tcsicmp(TEXT("disable"), *argv))
		{
			b = FALSE;
			err = CtrlNetcard(FALSE);
		}
		else
		{
			goto main_end;
		}

		_tprintf(_T("\n%s NetAdapter %s !\n"),
			b ? TEXT("Enable") : TEXT("Disable"),
			err ? TEXT("fail") : TEXT("success"));
		return;

	}
main_end:
	_tprintf(TEXT("\nenable NetCard: enable"));
	_tprintf(TEXT("\ndisable NetCard: disable\n"));

}

DWORD CtrlNetcard(BOOL bStatus)
{
	LPCTSTR HardwareId = TEXT("{4D36E972-E325-11CE-BFC1-08002BE10318}");
	GUID g = { 0X4D36E972, 0XE325, 0X11CE,{ 0XBF, 0XC1, 0X08, 0X00, 0X2B, 0XE1, 0X03, 0X18 } };
	DWORD NewState = 0;
	//调用ddk函数,来禁用网卡    
	DWORD i = 0, err = 0;

	HDEVINFO hDevInfo = NULL;
	SP_DEVINFO_DATA spDevInfoData = { 0 };

	NewState = bStatus ? DICS_ENABLE : DICS_DISABLE;

	//访问系统的硬件库
	hDevInfo = SetupDiGetClassDevs(NULL, TEXT("PCI"), NULL, DIGCF_ALLCLASSES | DIGCF_PRESENT);
	if (INVALID_HANDLE_VALUE == hDevInfo)
	{
		_tprintf(TEXT("访问系统硬件出错!"));
		return GetLastError();
	}

	//枚举硬件,获得需要的接口
	spDevInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
	memcmp(&(spDevInfoData.ClassGuid), &g, sizeof(GUID));

	for (i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &spDevInfoData); i++)
	{

		PBYTE PropertyBuffer = NULL;
		DWORD PropertyBufferSize = 0;

		//获得硬件的属性值

		while (!SetupDiGetDeviceRegistryProperty(
			hDevInfo,
			&spDevInfoData,
			SPDRP_CLASSGUID,
			NULL,
			PropertyBuffer,
			PropertyBufferSize,
			&PropertyBufferSize))
		{
			if (ERROR_INSUFFICIENT_BUFFER == GetLastError())
			{
				if (PropertyBuffer)
				{
					free(PropertyBuffer);
				}
				PropertyBuffer = (PBYTE)malloc(PropertyBufferSize);
			}
			else
			{
				break;
			}
		}

		if (!PropertyBuffer)
		{
			continue;
		}
		 
		//MessageBox(NULL, (LPCTSTR)PropertyBuffer, L"提示", MB_OK);
		 
		if (!_tcsicmp(HardwareId, (LPTSTR)PropertyBuffer))
		{
			SP_PROPCHANGE_PARAMS spPropChangeParams;
			//printf("PropertyBuffer ok\n");
			free(PropertyBuffer);

			spPropChangeParams.ClassInstallHeader.cbSize = sizeof(SP_CLASSINSTALL_HEADER);
			spPropChangeParams.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;
			spPropChangeParams.Scope = DICS_FLAG_GLOBAL;
			spPropChangeParams.StateChange = NewState;

			if (!SetupDiSetClassInstallParams(hDevInfo, &spDevInfoData, (SP_CLASSINSTALL_HEADER*)&spPropChangeParams, sizeof(spPropChangeParams)))
			{
				err = GetLastError();
				printf("SetupDiSetClassInstallParams:%d\n", GetLastError());
				goto cleanup_DeviceInfo;
			}

			if (!SetupDiCallClassInstaller(DIF_PROPERTYCHANGE, hDevInfo, &spDevInfoData))
			{
				err = GetLastError();
				printf("SetupDiCallClassInstaller:%x\n", GetLastError());
			goto cleanup_DeviceInfo;
			}

		}
		else
		{
			//printf("PropertyBuffer error\n");
			free(PropertyBuffer);
		}

	}

cleanup_DeviceInfo:

	SetupDiDestroyDeviceInfoList(hDevInfo);

	return err;
}
 

方法三.网上相关:http://blog.csdn.net/buyicn/article/details/6438635

你可能感兴趣的:(windows系统,MFC/C++)