教程集 www.jiaochengji.com
教程集 >  脚本编程  >  Asp.net  >  正文 C 获取本机网络参数(IP、子网掩码、网关、DNS)

C 获取本机网络参数(IP、子网掩码、网关、DNS)

发布时间:2016-12-02   编辑:jiaochengji.com
教程集为您提供C 获取本机网络参数(IP、子网掩码、网关、DNS)等资源,欢迎您收藏本站,我们将为您提供最新的C 获取本机网络参数(IP、子网掩码、网关、DNS)资源
网上查了一下资料,结合了一下自己的需要,封装的一个函数,只支持单网卡(项目只需要获取其中一个即可),只把关键代码贴出来
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy9628')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy9628>

#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,
  "SystemCurrentControlSetControlClass{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, "NdiInterfaces", 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 = "SYSTEMCurrentControlSetServicesTcpipParametersInterfaces";
 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)
   strcpy(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;
}

您可能感兴趣的文章:
FreeBSD下设置IP地址、网关、DNS的方法介绍
获取本地网卡IP mac地址 掩码 dns 外网IP的shell脚本
笔记本无法连接CMCC-EDU无线解决方法图解
solaris网络配置学习笔记
网络地址是什么
ubuntu配置IP地址的方法
php 实现dns域名查询的方法详解(图文)
一个检测网络连通性的shell脚本(图文)
solaris中有关 TCP/IP 配置的文件与命令
windows7 上如何设置开启虚拟 wifi 热点

[关闭]
~ ~