1. 程式人生 > >重啟網絡卡的幾種方法

重啟網絡卡的幾種方法

1.重啟windows網絡卡命令

rem 禁用網絡卡
netsh interface set interface 本地連線 disabled
rem 啟用網絡卡

netsh interface set interface 本地連線 enabled

2.根據相關資訊作出如下修改


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 <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;
				 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 <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);
				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 <windows.h>
#include <SetupAPI.h>
#include <stdio.h>
#include <tchar.h>
#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;
}