根据MAC地址查找网卡并修改IP地址 | 马犇-技术博客

根据MAC地址查找网卡并修改IP地址

来源:本站原创 C/C++, 原创 超过6,145 views围观 3条评论
#include <winsock2.h>
#include <iphlpapi.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include "setupapi.h"
#include <devguid.h>
#pragma comment(lib, "IPHLPAPI.lib")
#pragma comment(lib, "setupapi.lib")

#define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x))
#define FREE(x) HeapFree(GetProcessHeap(), 0, (x))

/* Note: could also use malloc() and free() */

BOOL GetAdapterInfo(LPCTSTR lpszAdapterName, PIP_ADAPTER_INFO pAdapterInfo);
BOOL SetIP(LPCTSTR lpszAdapterName, int nIndex, LPCTSTR pIPAddress, LPCTSTR pNetMask, LPCTSTR pNetGate);
BOOL RestartNetwork(LPCTSTR lpszAdapterName);

int __cdecl main()
{
	LPCTSTR lpszMAC = "DE-AD-DE-ED-9A-05";
	LPCTSTR lpszIP = "10.3.3.145";
	LPCTSTR lpszMask = "255.255.252.0";
	LPCTSTR lpszGateway = "10.3.0.5";
	IP_ADAPTER_INFO AdapterInfo;
	BOOL bResult = GetAdapterInfo(lpszMAC, &AdapterInfo);
	if (bResult) {
		printf("Get adapter information successfuly!\nAdapterName:%s\nnDescription:%s\nIndex:%d\n", AdapterInfo.AdapterName, AdapterInfo.Description, AdapterInfo.Index);
		BOOL bResult = SetIP(AdapterInfo.AdapterName, AdapterInfo.Index, lpszIP, lpszMask, lpszGateway);
		printf("result:%d", bResult);
	}
	return 0;
}

BOOL GetAdapterInfo(LPCTSTR lpszMAC, PIP_ADAPTER_INFO pRetAdapterInfo)
{
	PIP_ADAPTER_INFO pAdapterInfo;
	PIP_ADAPTER_INFO pAdapter = NULL;
	DWORD dwRetVal = 0;
	UINT i;

	ULONG ulOutBufLen = sizeof (IP_ADAPTER_INFO);
	pAdapterInfo = (IP_ADAPTER_INFO *)MALLOC(sizeof (IP_ADAPTER_INFO));
	if (pAdapterInfo == NULL) {
		printf("Error allocating memory needed to call GetAdaptersinfo\n");
		return 1;
	}
	// Make an initial call to GetAdaptersInfo to get
	// the necessary size into the ulOutBufLen variable
	if (GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW) {
		FREE(pAdapterInfo);
		pAdapterInfo = (IP_ADAPTER_INFO *)MALLOC(ulOutBufLen);
		if (pAdapterInfo == NULL) {
			printf("Error allocating memory needed to call GetAdaptersinfo\n");
			return 1;
		}
	}

	if ((dwRetVal = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen)) == NO_ERROR) {
		pAdapter = pAdapterInfo;
		while (pAdapter) {
			CHAR szMAC[18] = { 0 };
			for (i = 0; i < pAdapter->AddressLength; i++) {
				if (i == (pAdapter->AddressLength - 1)) 
					sprintf(szMAC + i * 3, "%.2X", (int)pAdapter->Address[i]);
				else 
					sprintf(szMAC + i * 3, "%.2X-", (int)pAdapter->Address[i]);
			}
			if (strcmp(szMAC, lpszMAC) == 0) {
				memcpy(pRetAdapterInfo, pAdapter, sizeof(IP_ADAPTER_INFO));
				if (pAdapterInfo)
					FREE(pAdapterInfo);
				return TRUE;
			}
			pAdapter = pAdapter->Next;
		}
	}
	else {
		printf("GetAdaptersInfo failed with error: %d\n", dwRetVal);
		return FALSE;
	}
	if (pAdapterInfo)
		FREE(pAdapterInfo);
	return FALSE;
}



BOOL RestartNetwork(LPCTSTR lpszAdapterName)
{
	LPCTSTR lpszNetworkName = "本地连接";
	// 获取网络连接名称用于netsh命令
	HKEY hKey;
	std::string strKeyName = "SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}\\";
	strKeyName += lpszAdapterName;
	strKeyName += "\\Connection";
	if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,
		strKeyName.c_str(),
		0,
		KEY_QUERY_VALUE,
		&hKey) != ERROR_SUCCESS)
		return FALSE;
	CHAR szNetworkName[32] = { 0 };
	DWORD dwSize = 32;
	DWORD dwResult = RegQueryValueEx(hKey, "Name", NULL, NULL, (LPBYTE)szNetworkName, &dwSize);
	printf("Name:%s", szNetworkName);
	CHAR szCommand[128] = { 0 };
	sprintf(szCommand, "netsh interface set interface name=\"%s\" admin=DISABLED", lpszNetworkName);
	system(szCommand);

	sprintf(szCommand, "netsh interface set interface name=\"%s\" admin=ENABLED", lpszNetworkName);
	system(szCommand);
	RegCloseKey(hKey);
	return TRUE;
}

#if 1
BOOL SetIP(LPCTSTR lpszAdapterName, int nIndex, LPCTSTR pIPAddress, LPCTSTR pNetMask, LPCTSTR pNetGate)
{
	HKEY hKey;
	std::string strKeyName = "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces\\";
	strKeyName += lpszAdapterName;
	if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,
		strKeyName.c_str(),
		0,
		KEY_WRITE,
		&hKey) != ERROR_SUCCESS)
		return FALSE;

	char mszIPAddress[100];
	char mszNetMask[100];
	char mszNetGate[100];

	strncpy(mszIPAddress, pIPAddress, 98);
	strncpy(mszNetMask, pNetMask, 98);
	strncpy(mszNetGate, pNetGate, 98);

	int nIP, nMask, nGate;

	nIP = strlen(mszIPAddress);
	nMask = strlen(mszNetMask);
	nGate = strlen(mszNetGate);

	*(mszIPAddress + nIP + 1) = 0x00;
	nIP += 2;

	*(mszNetMask + nMask + 1) = 0x00;
	nMask += 2;

	*(mszNetGate + nGate + 1) = 0x00;
	nGate += 2;

	DWORD dwLastError = 0;
	RegSetValueEx(hKey, "EnableDHCP", 0, REG_DWORD, (LPBYTE)&dwLastError, sizeof(DWORD));
	RegSetValueEx(hKey, "IPAddress", 0, REG_MULTI_SZ, (unsigned char*)mszIPAddress, nIP);
	RegSetValueEx(hKey, "SubnetMask", 0, REG_MULTI_SZ, (unsigned char*)mszNetMask, nMask);
	RegSetValueEx(hKey, "DefaultGateway", 0, REG_MULTI_SZ, (unsigned char*)mszNetGate, nGate);

	RegCloseKey(hKey);
	RestartNetwork(lpszAdapterName);
	return TRUE;
}
#endif