现在的位置: 首页 > 综合 > 正文

通过注册表得到IP相关信息

2018年06月08日 ⁄ 综合 ⁄ 共 4766字 ⁄ 字号 评论关闭
#pragma comment(lib, "ws2_32.lib")
#include <iostream>
#include <cstdlib>
#include <string>
#include <vector>

#include <windows.h>

using namespace std;

typedef struct tagAdapterInfo
{
	string strName; // 适配器名称
	string strDriverDesc; // 适配器描述
	string strIP; // IP地址
	string strNetMask; // 子网掩码
	string strNetGate; // 网关
	string strBroadcastIp; // 广播地址
	string strS; // 测试用的
}ADAPTER_INFO;

BOOL GetAdapterInfo();
BOOL RegGetIP(ADAPTER_INFO *pAI, LPCTSTR lpszAdapterName, int nIndex = 0);

vector<ADAPTER_INFO*> AdapterInfoVector;

int main()
{
	GetAdapterInfo();
	int i;
	for (i = 0;i < AdapterInfoVector.size();i++)
	{
		cout << AdapterInfoVector[i]->strName << ":" << endl << endl;
		cout << " " << AdapterInfoVector[i]->strDriverDesc << endl;
		cout << " " << AdapterInfoVector[i]->strIP << endl;
		cout << " " << AdapterInfoVector[i]->strNetMask << endl;
		cout << " " << AdapterInfoVector[i]->strNetGate << endl;
		cout << " " << AdapterInfoVector[i]->strBroadcastIp << endl;
		cout << endl;
		
	}
	return 0;
}

//—————————————————————–
// 取得所有网卡信息
//—————————————————————–
BOOL GetAdapterInfo()
{
	// 这里的代码适合WINDOWS2000,对于NT需要读取HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards
	HKEY hKey, hSubKey, hNdiIntKey;
	
	if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,
		"System\\CurrentControlSet\\Control\\Class\\{4d36e972-e325-11ce-bfc1-08002be10318}",
		0,
		KEY_READ,
		&hKey) != ERROR_SUCCESS)
		return FALSE;
	
	DWORD dwIndex = 0;
	DWORD dwBufSize = 256;
	DWORD dwDataType;
	char szSubKey[256];
	unsigned char szData[256];
	
	while(RegEnumKeyEx(hKey, dwIndex++, szSubKey, &dwBufSize, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
	{
		if(RegOpenKeyEx(hKey, szSubKey, 0, KEY_READ, &hSubKey) == ERROR_SUCCESS)
		{ 
			if(RegOpenKeyEx(hSubKey, "Ndi\\Interfaces", 0, KEY_READ, &hNdiIntKey) == ERROR_SUCCESS)
			{
				dwBufSize = 256;
				if(RegQueryValueEx(hNdiIntKey, "LowerRange", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS)
				{
					if(strstr((char*)szData, "ethernet") != NULL)// 判断是不是以太网卡
					{
						dwBufSize = 256;
						if(RegQueryValueEx(hSubKey, "DriverDesc", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS)
						{
							ADAPTER_INFO *pAI = new ADAPTER_INFO;
							pAI->strDriverDesc = (LPCTSTR)szData;
							dwBufSize = 256;
							if(RegQueryValueEx(hSubKey, "NetCfgInstanceID", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS)
							{
								pAI->strS = (LPCTSTR)szData;
								RegGetIP(pAI, (LPCTSTR)szData);
							}
							AdapterInfoVector.push_back(pAI); // 加入到容器中
						}
					}
				}
				RegCloseKey(hNdiIntKey);
			}
			RegCloseKey(hSubKey);
		}
		
		dwBufSize = 256;
	} /* end of while */
	
	RegCloseKey(hKey);
	return true;
}

//—————————————————————–
// 得到注册表中的IP信息
// nIndex暂时未处理
//—————————————————————–

BOOL RegGetIP(ADAPTER_INFO *pAI, LPCTSTR lpszAdapterName, int nIndex/* =0 */)
{
	//ASSERT(pAI);
	HKEY hKey, hSubKey, hNdiIntKey;
	
	string strKeyName = "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces\\";
	strKeyName += lpszAdapterName;
	if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,
		strKeyName.c_str(),
		0,
		KEY_READ,
		&hKey) != ERROR_SUCCESS)
		return FALSE;
	
	unsigned char szData[256];
	DWORD dwDataType, dwBufSize;
	dwBufSize = 256;
	if(RegQueryValueEx(hKey, "DhcpIPAddress", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS)
		pAI->strIP = (LPCTSTR)szData;
	else{
		if(RegQueryValueEx(hKey, "IPAddress", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS)
			pAI->strIP = (LPCTSTR)szData;
	}
	
	
	
	dwBufSize = 256;
	if(RegQueryValueEx(hKey, "DhcpSubnetMask", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS)
		pAI->strNetMask = (LPCTSTR)szData;
	else
	{
		if(RegQueryValueEx(hKey, "SubnetMask", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS)
			pAI->strNetMask = (LPCTSTR)szData;
	}
	
	
	dwBufSize = 256;
	if(RegQueryValueEx(hKey, "DhcpDefaultGateway", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS)
		pAI->strNetGate = (LPCTSTR)szData;
	else
	{
		if(RegQueryValueEx(hKey, "DefaultGateway", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS)
			pAI->strNetGate = (LPCTSTR)szData;
	}
	
	
	RegCloseKey(hKey);
	
	strKeyName = "SYSTEM\\ControlSet001\\Control\\Network";
	if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,
		strKeyName.c_str(),
		0,
		KEY_READ,
		&hKey) != ERROR_SUCCESS)
		return FALSE;
	char szSubKey[256];
	char szSubKey_two[256];
	memset(szSubKey, 0, 256);
	DWORD dwIndex = 0;
	DWORD dwIndex_two = 0;
	dwBufSize = 256;
	DWORD dwBufSize_two = 256;
	pAI->strName = pAI->strDriverDesc;
	while(RegEnumKey(hKey, dwIndex++, szSubKey, dwBufSize) == ERROR_SUCCESS)
	{
		string strKeyName_two;
		strKeyName_two = strKeyName + "\\";
		strKeyName_two += szSubKey;
		if(RegOpenKeyEx(hKey, szSubKey, 0, KEY_READ, &hSubKey) == ERROR_SUCCESS)
		{
			while(RegEnumKey(hSubKey, dwIndex_two++, szSubKey_two, dwBufSize_two) == ERROR_SUCCESS)
			{
				if (strstr(szSubKey_two, lpszAdapterName) != NULL)
				{
					strcat(szSubKey_two, "\\Connection");
					if(RegOpenKeyEx(hSubKey, szSubKey_two, 0, KEY_READ, &hNdiIntKey) == ERROR_SUCCESS)
					{
						if(RegQueryValueEx(hNdiIntKey, "Name", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS)
						{
							pAI->strName = (LPCTSTR)szData;
							break;
						}
						
						RegCloseKey(hNdiIntKey);
					}
					
				}
			}
			RegCloseKey(hSubKey);
		}
	}
	RegCloseKey(hKey);
	
	/*
	算法: 
	1. 子网掩码与IP地址进行位与运算,得处网络地址
	2. 网络地址 | (~子网掩码),得出广播地址
	*/
	in_addr broadcast;
	broadcast.S_un.S_addr = (
		inet_addr(pAI->strIP.c_str()) 
		& inet_addr(pAI->strNetMask.c_str())
		)
		| (~inet_addr(pAI->strNetMask.c_str()));
	pAI->strBroadcastIp = inet_ntoa(broadcast);
	return TRUE;
}

http://blog.csdn.net/bweaglegao/article/details/8736900

http://blog.csdn.net/bweaglegao/article/details/8736900

抱歉!评论已关闭.