程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> 獲取網卡的相關信息

獲取網卡的相關信息

編輯:關於C++

////////////////////////////////////////////////////////////////
//運行本程序需要安裝Microsoft Platform SDK .這個可以去MS下載
// http://www.microsoft.com/msdownload/platformsdk/sdkupdate/
//作者:安靜
//mail: [email protected]
//10:56 2002-1-31 start
//14:44 2002-2-2 fix some bug.
//////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include <iostream>
#include <string.h>
#include "Iphlpapi.h"
using namespace std;
PIP_ADAPTER_INFO pinfo=NULL;
unsigned long len=0;
CString macaddress;
CString  description;
CString  type;
CString  subnet;
CString  IpAddress;
CString  gateway;
CString  PrimaryWinsServer;
CString  dhcp;
void GetInfo();
void ParseData();
void OutPut(CString str);
void main()
{

   cout<<"------------------------網卡信息檢測---------------------\n";
   GetInfo();

}
void GetInfo()
{
   if (pinfo!=NULL)
     delete (pinfo);
   unsigned long nError;
   nError  =  GetAdaptersInfo(pinfo,&len); //1??üoˉêy
   if (nError==0)
   {
     ParseData();
   }
   if (nError==ERROR_NO_DATA)
   {
     OutPut("沒有網絡設備信息");
   }
   if (nError==ERROR_NOT_SUPPORTED)
   {
     OutPut("GetAdaptersInfo不支持本系統");
   }
   if (nError==ERROR_BUFFER_OVERFLOW)
   {
     pinfo= (PIP_ADAPTER_INFO)malloc(len);
     nError  =  GetAdaptersInfo(pinfo,&len);
     if (nError==0)
     {
       ParseData();
     }
   }

   return;
}
void ParseData()
{
   if (pinfo!=NULL)
   {
     macaddress.Format("%02X:%02X:%02X:%02X:%02X:%02X",pinfo->Address[0],pinfo->Address[1],pinfo->Address[2],pinfo->Address[3],pinfo->Address[4],pinfo->Address[5]);
     description  =  pinfo->Description;
     type.Format("%d",pinfo->Type);

      PIP_ADDR_STRING pAddressList = &(pinfo->IpAddressList);
      IpAddress  ="";
      do
      {
        IpAddress  +=  pAddressList->IpAddress.String;
        pAddressList = pAddressList->Next;
        if (pAddressList != NULL)
         IpAddress  +="\r\n";
      } while (pAddressList != NULL);
     subnet.Format("%s",pinfo->IpAddressList.IpMask.String);
     gateway.Format("%s",pinfo->GatewayList.IpAddress.String);
     if (pinfo->HaveWins)
       PrimaryWinsServer.Format("%s",pinfo->PrimaryWinsServer.IpAddress.String );
     else
       PrimaryWinsServer.Format("%s","N/A" );
     if (pinfo->DhcpEnabled )
       dhcp.Format("%s",pinfo->DhcpServer.IpAddress.String );
     else
       dhcp.Format("%s","N/A");
     pinfo = pinfo->Next;
   }
   OutPut("網絡設備為:\t"+description);
   OutPut("Mac地址為:\t"+macaddress);
   OutPut("網卡類型:\t"+type);
   OutPut("IP地址:\t"+IpAddress);
   OutPut("子網掩碼:\t"+subnet);
   OutPut("網關:\t"+gateway);
   OutPut("主Wins服務器:\t"+PrimaryWinsServer);
   OutPut("dhcp服務器:\t"+dhcp);
}
void OutPut(CString str)
{
   cout<<str.GetBuffer(str.GetLength()) <<endl;
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved