windows从注册表获取ip配置信息

DNS的获取方式有多种:

这里提供一种方式是从注册表获取的:

在HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\Tcpip\Parameters\Adapters可以获取网卡的SettingID,

//HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\Tcpip\Parameters\Adapters
bool GetAdaptersKeyName(std::list *listAdapters)
{
	OutputDebugStringA("Get network config");
	//获取网卡名称 网卡名称,网卡别名
	string strAdapterName,strAdapterAlias;
	HKEY hKey, hSubKey;
	if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,
		_T("SYSTEM\\CurrentControlSet\\services\\Tcpip\\Parameters\\Adapters"),
		0,
		KEY_READ,
		&hKey) != ERROR_SUCCESS)
		return FALSE;

	DWORD dwIndex = 0;
	DWORD dwBufSize = 256;
	TCHAR szSubKey[256];
	listAdapters->clear();

	while(RegEnumKeyEx(hKey, dwIndex++, szSubKey, &dwBufSize, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
	{
		if(RegOpenKeyEx(hKey, szSubKey, 0, KEY_READ, &hSubKey) == ERROR_SUCCESS)
		{
			listAdapters->push_back(CString(szSubKey));
			RegCloseKey(hSubKey);
		}
		dwBufSize = 256;
	}    /* end of while */

	RegCloseKey(hKey);
	return true;
}

根据SettingID,再逐个查询获取到ip配置包含DNS的信息:

这里要查询 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\Tcpip\Parameters\Interfaces的SettingID下的 内容

typedef struct tagNetworkCfg 
{
	TCHAR szIP[18];
	TCHAR szNetmask[18];
	TCHAR szGateway[18];
	TCHAR szDns1[256];
	TCHAR szDns2[18];
}NetworkCfg;


bool GetNetworkCfg(NetworkCfg *cfg, CString strAdapterAlias)
{
	HKEY hKey;
	BYTE lpData[256];
	CString strKeyName = _T("SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces\\");
	strKeyName += strAdapterAlias;
	if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,
		strKeyName,
		0,
		KEY_READ,
		&hKey) != ERROR_SUCCESS)
		return FALSE;
	DWORD dwBufSize = 256;
	DWORD dwDataType;

	if(RegQueryValueEx(hKey, _T("DhcpIPAddress"), 0,&dwDataType, lpData, &dwBufSize) == ERROR_SUCCESS)
		wcscpy(cfg->szIP,(LPCTSTR)lpData);
	else
	{
		if(RegQueryValueEx(hKey, _T("IPAddress"), 0, &dwDataType, lpData, &dwBufSize) == ERROR_SUCCESS)
			wcscpy(cfg->szIP,(LPCTSTR)lpData);
	}

	dwBufSize = 256;
	if(RegQueryValueEx(hKey, _T("DhcpSubnetMask"), 0, &dwDataType, lpData, &dwBufSize) == ERROR_SUCCESS)
		wcscpy(cfg->szNetmask,(LPCTSTR)lpData);
	else
	{
		if(RegQueryValueEx(hKey, _T("SubnetMask"), 0, &dwDataType, lpData, &dwBufSize) == ERROR_SUCCESS)
			wcscpy(cfg->szNetmask,(LPCTSTR)lpData);
	}

	dwBufSize = 256;
	if(RegQueryValueEx(hKey, _T("DhcpDefaultGateway"), 0, &dwDataType, lpData, &dwBufSize) == ERROR_SUCCESS)
		wcscpy(cfg->szGateway,(LPCTSTR)lpData);
	else
	{
		if(RegQueryValueEx(hKey, _T("DefaultGateway"), 0, &dwDataType, lpData, &dwBufSize) == ERROR_SUCCESS)
			wcscpy(cfg->szGateway,(LPCTSTR)lpData);
	}

	dwBufSize = 256;
	if(RegQueryValueEx(hKey, _T("DhcpNameServer"), 0, &dwDataType, lpData, &dwBufSize) == ERROR_SUCCESS)
		wcscpy(cfg->szDns1,(LPCTSTR)lpData);
	else
	{
		if(RegQueryValueEx(hKey, _T("NameServer"), 0, &dwDataType, lpData, &dwBufSize) == ERROR_SUCCESS)
			wcscpy(cfg->szDns1,(LPCTSTR)lpData);
	}

	RegCloseKey(hKey);
       return true;

}
引用头内容有:
	std::list listAdapters;
	GetAdaptersKeyName(&listAdapters);

	std::list networkCfgList;
	for (list::iterator iter = listAdapters.begin(); iter!=listAdapters.end();iter++)
	{
		CString strTemp = *iter;

		NetworkCfg cfg;
		GetNetworkCfg(&cfg, strTemp);
	}

#include 
#include 

#include 
#pragma comment(lib,"Ws2_32.lib")

#include 
#pragma comment(lib, "Iphlpapi.lib")

using namespace std;


你可能感兴趣的:([Windows]网络编程)