当前位置:文档之家› VC获取本机IP、子网掩码、网关、计算广播地址等信息代码(通过读注册表)

VC获取本机IP、子网掩码、网关、计算广播地址等信息代码(通过读注册表)

VC获取本机IP、子网掩码、网关、计算广播地址等信息代码(通过读注册表)
//cody by xiujie , https://www.doczj.com/doc/5313168757.html,
#pragma comment(lib, "ws2_32.lib")
#include
#include
#include
#include

#include

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 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 << " " << AdapterInfoVector[i]->strS << 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, "IPAddress", 0, dwDataType, szData, &dwBufSize) == ERROR_SUCCESS)
pAI->strIP = (LPCTSTR)szData;
if(RegQueryValueEx(hKey, "DhcpIPAddress", 0, dwDataType, szData, &dwBufSize) == ERROR_SUCCESS)
pAI->strIP = (LPCTSTR)szData;

dwBufSize = 256;
if(RegQueryValueEx(hKey, "SubnetMask", 0, dwDataType, szData, &dwBufSize) == ERROR_SUCCESS)
pA

I->strNetMask = (LPCTSTR)szData;
if(RegQueryValueEx(hKey, "DhcpSubnetMask", 0, dwDataType, szData, &dwBufSize) == ERROR_SUCCESS)
pAI->strNetMask = (LPCTSTR)szData;

dwBufSize = 256;
if(RegQueryValueEx(hKey, "DefaultGateway", 0, dwDataType, szData, &dwBufSize) == ERROR_SUCCESS)
pAI->strNetGate = (LPCTSTR)szData;
if(RegQueryValueEx(hKey, "DhcpDefaultGateway", 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;
}


修复了一下,我一开始的在Windows 7 下测试是没有问题的,在XP下重新测试就出问题了。
这个问题是Windows 7 下的注册表优化了,如果是自动获取IP的话就没有IPAddress项了。
下面是修复版的,已经

通过XP系统测试了。我这没有2000的平台还不行的话,请联系偶.


// code by xiujie , https://www.doczj.com/doc/5313168757.html,
#pragma comment(lib, "ws2_32.lib")
#include
#include
#include
#include

#include

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 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;

}
::Sleep(10000);
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;
}

相关主题
文本预览
相关文档 最新文档