欢迎来到代码驿站!

C代码

当前位置:首页 > 软件编程 > C代码

C++实现获取IP、子网掩码、网关、DNS等本机网络参数的方法

时间:2021-08-09 08:51:49|栏目:C代码|点击:

本文以一个完整实例形式介绍了C++实现获取IP、子网掩码、网关、DNS等本机网络参数的方法,供大家参考,具体的完整实例如下:

#pragma comment(lib,"Ws2_32.lib")
#include <Iphlpapi.h>
#pragma comment(lib, "Iphlpapi.lib")
using namespace std;
typedef struct tagNetworkCfg 
{
 char szIP[18];
 char szNetmask[18];
 char szGateway[18];
 char szDns1[18];
 char szDns2[18];
}NetworkCfg;
bool GetNetworkCfg(NetworkCfg *cfg)
{
 log_printf("Get network config");
 //获取网卡名称 网卡名称,网卡别名
 string strAdapterName,strAdapterAlias;
 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)
   {
    strAdapterName = (LPCTSTR)szData;
    dwBufSize = 256;
    if(RegQueryValueEx(hSubKey, "NetCfgInstanceID", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS)
    {
    strAdapterAlias = (LPCTSTR)szData;
    break;
    }
   }
   }
  }
  RegCloseKey(hNdiIntKey);
  }
  RegCloseKey(hSubKey);
 }

 dwBufSize = 256;
 }  /* end of while */

 RegCloseKey(hKey);
 if (strAdapterName.empty() || strAdapterAlias.empty())
 {
 log_printf("failed to get network config");
 return false;
 }
 string strKeyName = "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces\\";
 strKeyName += strAdapterAlias;
 if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,
 strKeyName.c_str(),
 0,
 KEY_READ,
 &hKey) != ERROR_SUCCESS)
 return FALSE;
 dwBufSize = 256;
 if(RegQueryValueEx(hKey, "DhcpIPAddress", 0,&dwDataType, szData, &dwBufSize) == ERROR_SUCCESS)
 strcpy(cfg->szIP,(LPCTSTR)szData);
 else{
 if(RegQueryValueEx(hKey, "IPAddress", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS)
 strcpy(cfg->szIP,(LPCTSTR)szData);
 }
  
 dwBufSize = 256;
 if(RegQueryValueEx(hKey, "DhcpSubnetMask", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS)
 strcpy(cfg->szNetmask,(LPCTSTR)szData);
 else
 {
 if(RegQueryValueEx(hKey, "SubnetMask", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS)
 trcpy(cfg->szNetmask,(LPCTSTR)szData);
 }
  
 dwBufSize = 256;
 if(RegQueryValueEx(hKey, "DhcpDefaultGateway", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS)
 strcpy(cfg->szGateway,(LPCTSTR)szData);
 else
 {
 if(RegQueryValueEx(hKey, "DefaultGateway", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS)
 strcpy(cfg->szGateway,(LPCSTR)szData);
 }
 RegCloseKey(hKey);

 //获取DNS服务器信息
 FIXED_INFO *fi = (FIXED_INFO *)GlobalAlloc(GPTR,sizeof( FIXED_INFO));
 ULONG ulOutBufLen = sizeof(FIXED_INFO);
 DWORD ret = ::GetNetworkParams(fi, &ulOutBufLen);
 if(ret != ERROR_SUCCESS) 
 { 
 GlobalFree(fi);
 fi = (FIXED_INFO *) GlobalAlloc( GPTR, ulOutBufLen );
 ret = ::GetNetworkParams(fi, &ulOutBufLen);
 if(ret != ERROR_SUCCESS) 
 {
  log_printf("Get Dns server failed"); 
  return false; 
 }
 }
 strcpy(cfg->szDns1,fi->DnsServerList.IpAddress.String);
 IP_ADDR_STRING *pIPAddr = fi->DnsServerList.Next;
 if(pIPAddr != NULL) 
 strcpy(cfg->szDns2, pIPAddr->IpAddress.String);
 return false;
}

上一篇:MFC实现全屏功能代码实例

栏    目:C代码

下一篇:Redis的新特性懒惰删除Lazy Free详解

本文标题:C++实现获取IP、子网掩码、网关、DNS等本机网络参数的方法

本文地址:http://www.codeinn.net/misctech/165536.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有