VC獲取本機IP、子網掩碼、閘道器、計算廣播地址等資訊程式碼(通過讀登錄檔)
阿新 • • 發佈:2019-01-28
// code by xiujie , bbs.xiujie.cn
#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;
}
::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;
}
#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;
}
::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;
}