Click here to Skip to main content
11,481,843 members (64,750 online)
Click here to Skip to main content

A Class for Getting NetCard Adapter Information

, 10 Sep 2002 94.6K 1.7K 34
Rate this:
Please Sign up or sign in to vote.
This class can be used to get netcard adapter information such as MAC,IP,DHCP
<!-- Article image -->

Sample Image - NetcardInfo.jpg

Introduction

I provide you a class CxNetCardInfo which can be used to get the local machine Netcard Adapter information such as IP address, MAC, DHCP, Subnet mask, WINS etc. For more detailed information, let's go to the next section.

How to use CxNetCardInfo

Usage of CxNetCardInfo is easy! For example:

void CNetCardInfoDlg::OnButtonGetinfo() 
{
  // TODO: Add your control notification handler code here
  CString tmp( _T("") );

  CxNetCardInfo NCI;

  tmp += _T("NetCard type:\t") + NCI.GetNetCardType() + _T("\r\n");
  tmp += _T("IP Address:\t") + NCI.GetNetCardIPAddress() + _T("\r\n" );
  tmp += _T("Subnet Mask:\t") + NCI.GetNetCardSubnetMask() + _T("\r\n");
  tmp += _T("Gateway:\t") + NCI.GetNetCardGateWay() + _T("\r\n");
  tmp += _T("DHCP Server:\t") + NCI.GetDHCPServer() + _T("\r\n");
  tmp += _T("MAC Address:\t") + NCI.GetNetCardMACAddress() + _T("\r\n");
  tmp += _T("Device name:\t") + NCI.GetNetCardDeviceName() + _T("\r\n");
  tmp += _T("Wins Server:\t") + NCI.GetNetCardWINS() + ("\r\n");
  tmp += NCI.GetErrorMsg() + _T("\r\n");

  CEdit *pEdit = (CEdit *)GetDlgItem(IDC_EDIT_INFO);
  pEdit->SetWindowText(tmp);
}

The CxNetCardInfo class

Header file

// xNetCardInfo.h: interface for the CxNetCardInfo class.
//
//////////////////////////////////////////////////////////////////////

#if 
 !defined(AFX_XNETCARDINFO_H__FC408BC6_3516_4481_AF7E_0B875C0E2B2C__INCLUDED_)
#define AFX_XNETCARDINFO_H__FC408BC6_3516_4481_AF7E_0B875C0E2B2C__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

class CxNetCardInfo  
{
public:
  CxNetCardInfo();
  virtual ~CxNetCardInfo();

private:
  void ParseData();
  void GetInfo();

public:
  CString GetErrorMsg();
  CString GetNetCardWINS();
  CString GetNetCardDeviceName();
  CString GetNetCardMACAddress();
  CString GetDHCPServer();
  CString GetNetCardGateWay();
  CString GetNetCardSubnetMask();
  CString GetNetCardIPAddress();
  CString GetNetCardType();

private:
  BYTE m_data[4096];
  CString ErrMsg;
  CString macaddress;
  CString description;
  CString type;
  CString subnet;
  CString IpAddress;
  CString gateway;
  CString PrimaryWinsServer;
  CString dhcp;

  unsigned long len;
  PIP_ADAPTER_INFO pinfo;
};

#endif 
//!defined(AFX_XNETCARDINFO_H__FC408BC6_3516_4481_AF7E_0B875C0E2B2C__INCLUDED_)

Implementation file

// xNetCardInfo.cpp: implementation of the CxNetCardInfo class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "NetCardInfo.h"
#include "xNetCardInfo.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CxNetCardInfo::CxNetCardInfo()
{
  ErrMsg = _T( "" );

  macaddress = _T( "" );
  description = _T( "" );
  type = _T( "" );
  subnet = _T( "" );
  IpAddress = _T( "" );
  gateway = _T( "" );
  PrimaryWinsServer = _T( "" );
  dhcp = _T( "" );

  ZeroMemory( m_data,4096 );
  len = 0;
  pinfo = ( PIP_ADAPTER_INFO )m_data;

  GetInfo();
}

CxNetCardInfo::~CxNetCardInfo()
{

}

void CxNetCardInfo::GetInfo()
{
  ErrMsg = _T( "Success!" );

  unsigned long nError;

  nError = GetAdaptersInfo( pinfo,&len );

  switch( nError ) { // Not all return value processed here!
    case 0:
      ParseData();
      break;
    case ERROR_NO_DATA:
      ErrMsg = _T( "No net device information!" );
      break;
    case ERROR_NOT_SUPPORTED:
      ErrMsg = _T( "The system not support GetAdaptersInfo API function!" );
      break;
    case ERROR_BUFFER_OVERFLOW:
      nError = GetAdaptersInfo( pinfo,&len );
      if( nError == 0 ) ParseData();
      else ErrMsg = _T("Unknow error!");
      break;
  }
}

void CxNetCardInfo::ParseData()
{
  macaddress.Format( _T("%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(_T("%d"),pinfo->Type);

  PIP_ADDR_STRING pAddressList = &(pinfo->IpAddressList);
  IpAddress = _T("");
  do {
    IpAddress += pAddressList->IpAddress.String;
    pAddressList = pAddressList->Next;
    if( pAddressList != NULL ) IpAddress += _T( "\r\n" );
  }while( pAddressList != NULL );

  subnet.Format( _T("%s"),pinfo->IpAddressList.IpMask.String );
  gateway.Format( _T("%s"),pinfo->GatewayList.IpAddress.String );
  if( pinfo->HaveWins )
    PrimaryWinsServer.Format( _T("%s"),
      pinfo->PrimaryWinsServer.IpAddress.String );
  else
    PrimaryWinsServer.Format( _T("%s"),_T("N/A") );
  if( pinfo->DhcpEnabled )
    dhcp.Format( _T("%s"),pinfo->DhcpServer.IpAddress.String );
  else
    dhcp.Format( _T("%s"),_T("N/A") );
  pinfo = pinfo->Next;
}

CString CxNetCardInfo::GetNetCardType()
{
  return type;
}

CString CxNetCardInfo::GetNetCardIPAddress()
{
  return IpAddress;
}

CString CxNetCardInfo::GetNetCardSubnetMask()
{
  return subnet;
}

CString CxNetCardInfo::GetNetCardGateWay()
{
  return gateway;
}

CString CxNetCardInfo::GetDHCPServer()
{
  return dhcp;
}

CString CxNetCardInfo::GetNetCardMACAddress()
{
  return macaddress;
}

CString CxNetCardInfo::GetNetCardDeviceName()
{
  return description;
}

CString CxNetCardInfo::GetNetCardWINS()
{
  return PrimaryWinsServer;
}

CString CxNetCardInfo::GetErrorMsg()
{
  return ErrMsg;
}

Notes

The CxNetCardInfo is not strong enough for serious usage, but someone can modify it and make it better hopefully!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

YangTze
Web Developer
China China
Hi,I'm YangTze!

Comments and Discussions

 
QuestionCan't get the disable netcard adapter. Pin
ly751926-Jul-13 0:08
memberly751926-Jul-13 0:08 
Generalwell done! i am chinese too. Pin
redice16-Jun-08 22:40
memberredice16-Jun-08 22:40 
QuestionHow to distinguish Wireless Adapter from other Adapers? Pin
iprowq6-Apr-08 17:09
memberiprowq6-Apr-08 17:09 
GeneralClass Rewrite Pin
Blake Miller26-Feb-04 11:55
memberBlake Miller26-Feb-04 11:55 
GeneralRe: Class Rewrite Pin
olegxxx9-Dec-04 3:38
memberolegxxx9-Dec-04 3:38 
GeneralMAC address length Pin
DavidCrow5-Sep-03 9:07
memberDavidCrow5-Sep-03 9:07 
QuestionWhy different Mac address with two methods? Pin
catchang4-Aug-03 22:11
membercatchang4-Aug-03 22:11 
QuestionHow to Get the real MacAddress? Pin
Anonymous1-Jul-03 23:01
sussAnonymous1-Jul-03 23:01 
Generalconsult Pin
hailiang13-Apr-03 18:26
memberhailiang13-Apr-03 18:26 
GeneralGateway IP under Win NT Pin
Koep17-Feb-03 5:54
memberKoep17-Feb-03 5:54 
GeneralRe: Gateway IP under Win NT Pin
geo_m17-Feb-03 6:15
membergeo_m17-Feb-03 6:15 
GeneralRe: Gateway IP under Win NT Pin
Koep17-Feb-03 6:22
memberKoep17-Feb-03 6:22 
GeneralRe: Gateway IP under Win NT Pin
geo_m17-Feb-03 6:30
membergeo_m17-Feb-03 6:30 
Generalabout pOutBufLen Pin
Panr11-Dec-02 21:11
memberPanr11-Dec-02 21:11 
GeneralRe: about pOutBufLen Pin
Panr11-Dec-02 21:14
memberPanr11-Dec-02 21:14 
Generalµ¥Íø¿¨ Pin
dzyd759-Dec-02 15:21
memberdzyd759-Dec-02 15:21 
GeneralNetCard Disabler Pin
drCool28-Nov-02 7:24
sussdrCool28-Nov-02 7:24 
GeneralAND ... Pin
Germi Massimo12-Sep-02 9:42
memberGermi Massimo12-Sep-02 9:42 
GeneralRe: AND ... Pin
tstih12-Sep-02 22:27
membertstih12-Sep-02 22:27 
GeneralRe: AND ... Pin
adrianw23-Nov-03 18:58
memberadrianw23-Nov-03 18:58 
General&#35831;&#25945; Pin
jngxx11-Sep-02 18:12
memberjngxx11-Sep-02 18:12 
GeneralRe: Çë½Ì Pin
YangTze11-Sep-02 19:44
memberYangTze11-Sep-02 19:44 
GeneralRe: Çë½Ì Pin
jngxx12-Sep-02 0:01
memberjngxx12-Sep-02 0:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150520.1 | Last Updated 11 Sep 2002
Article Copyright 2002 by YangTze
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid