VC获取IP、子网掩码、网关、广播地址
来源:互联网 发布:淘宝店铺导航css模板 编辑:程序博客网 时间:2024/04/30 01:46
#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;}
0 0
- VC获取IP、子网掩码、网关、广播地址
- VC获取本机IP、子网掩码、网关、计算广播地址等信息代码(通过读注册表)
- VC获取本机IP、子网掩码、网关、计算广播地址等信息
- Linux查看网关、子网掩码、IP、广播地址
- shell 获取网卡IP地址 子网掩码 默认网关 广播地址 MAC地址
- IP地址、子网掩码、网关
- IP地址、子网掩码、网关
- ip地址 子网掩码 网关
- IP地址 网关 子网掩码
- MFC自动获取网络地址函数实现----广播地址,网关,子网掩码
- C#编程获取IP地址、子网掩码、网关地址
- C#编程获取IP地址、子网掩码、网关地址
- C#编程获取IP地址、子网掩码、网关地址
- C#编程获取IP地址、子网掩码、网关地址
- 默认网关 网关 子网掩码 广播地址
- 获取WIFI连接下的广播地址 子网掩码 IP地址
- 获取WIFI连接下的广播地址 子网掩码 IP地址
- IP地址,网关,子网掩码,网段
- 常用开源软件镜像
- PBP二次开发,设置表格根据内容自动设置行高
- 社团划分——Fast Unfolding算法
- <context:component-scan>使用说明
- GDAL Qt 开发
- VC获取IP、子网掩码、网关、广播地址
- 深入 HBase 架构解析(2)
- C++STL 常用 函数 用法
- SAP HANA 基础架构简介(二)
- 排序算法总结
- Unity - Camera
- 前端性能提升方法
- IntentFilter详解
- [转] A Brief Overview Of Vulkan API