程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> C++完成獲得IP、子網掩碼、網關、DNS等本機收集參數的辦法

C++完成獲得IP、子網掩碼、網關、DNS等本機收集參數的辦法

編輯:關於C++

C++完成獲得IP、子網掩碼、網關、DNS等本機收集參數的辦法。本站提示廣大學習愛好者:(C++完成獲得IP、子網掩碼、網關、DNS等本機收集參數的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C++完成獲得IP、子網掩碼、網關、DNS等本機收集參數的辦法正文


本文以一個完全實例情勢引見了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;
}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved