擷取網卡的相關資訊

來源:互聯網
上載者:User

////////////////////////////////////////////////////////////////
//運行本程式需要安裝Microsoft Platform SDK .這個可以去MS下載
// http://www.microsoft.com/msdownload/platformsdk/sdkupdate/
//作者:安靜
//mail: aweiken@163.com
//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;
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.