Return to Snippet

Revision: 22095
at December 31, 2009 23:22 by richardw


Updated Code
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------

#include <windows.h>
#include <stdio.h>
#include <time.h>

#define MAX_ADAPTER_DESCRIPTION_LENGTH  128 // arb.
#define MAX_ADAPTER_NAME_LENGTH         256 // arb.
#define MAX_ADAPTER_ADDRESS_LENGTH      8   // arb.

//
// IP_ADDRESS_STRING - store an IP address as a dotted decimal string
//

typedef struct
{
    char String[4 * 4];
} IP_ADDRESS_STRING, *PIP_ADDRESS_STRING, IP_MASK_STRING, *PIP_MASK_STRING;

//
// IP_ADDR_STRING - store an IP address with its corresponding subnet mask,
// both as dotted decimal strings
//

typedef struct _IP_ADDR_STRING
{
    struct _IP_ADDR_STRING* Next;
    IP_ADDRESS_STRING IpAddress;
    IP_MASK_STRING IpMask;
    DWORD Context;
} IP_ADDR_STRING, *PIP_ADDR_STRING;

//
// ADAPTER_INFO - per-adapter information. All IP addresses are stored as strings
//

typedef struct _IP_ADAPTER_INFO
{
    struct _IP_ADAPTER_INFO* Next;
    DWORD ComboIndex;
    char AdapterName[MAX_ADAPTER_NAME_LENGTH + 4];
    char Description[MAX_ADAPTER_DESCRIPTION_LENGTH + 4];
    UINT AddressLength;
    BYTE Address[MAX_ADAPTER_ADDRESS_LENGTH];
    DWORD Index;
    UINT Type;
    UINT DhcpEnabled;
    PIP_ADDR_STRING CurrentIpAddress;
    IP_ADDR_STRING IpAddressList;
    IP_ADDR_STRING GatewayList;
    IP_ADDR_STRING DhcpServer;
    BOOL HaveWins;
    IP_ADDR_STRING PrimaryWinsServer;
    IP_ADDR_STRING SecondaryWinsServer;
    time_t LeaseObtained;
    time_t LeaseExpires;
} IP_ADAPTER_INFO, *PIP_ADAPTER_INFO;

typedef DWORD (WINAPI* GetAdaptersInfoFunc)(
    PIP_ADAPTER_INFO pAdapterInfo,  // buffer to receive data
    PULONG pOutBufLen               // size of data returned
);
///////////////////////////////////////////////////////////////////////////////

void MacAddressToString(const LPBYTE Address, LPSTR lpsz, int nAddressLength = 6)
{
	int n;
	LPSTR p = lpsz;
	for (n = 0; n < nAddressLength; n++)
	{
		p += sprintf(p, n ? "-%02x" : "%02x", Address[n]);
	}
}
//---------------------------------------------------------------------------
      
AnsiString __fastcall TForm1::GetMac(void)
{
    //TODO: Add your source code here
    HMODULE hLib;
    GetAdaptersInfoFunc GetAdaptersInfo = NULL;

    PIP_ADAPTER_INFO pai = NULL;
    DWORD dwSize = 0;
    CHAR szMac[64];

    hLib = LoadLibrary("Iphlpapi.dll");
    if (!hLib)
    {
        //puts("Failed to load Iphlpapi.dll");
        exit(1);
    }

    GetAdaptersInfo = (GetAdaptersInfoFunc)GetProcAddress(hLib, "GetAdaptersInfo");
    if (GetAdaptersInfo == NULL)
    {
        //puts("Failed to load GetAdaptersInfo in Iphlpapi.dll");
    }

    // Get size of buffer needed:
    GetAdaptersInfo(NULL, &dwSize);

    pai = (PIP_ADAPTER_INFO)GlobalAlloc(GPTR, dwSize);
    GetAdaptersInfo(pai, &dwSize);

    PIP_ADAPTER_INFO p = pai;

    MacAddressToString(p->Address, szMac, p->AddressLength);

    GlobalFree(pai);
    FreeLibrary(hLib);
    return ((AnsiString)szMac).UpperCase();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::btn1Click(TObject *Sender)
{
    lbl1->Caption = GetMac();
}
//---------------------------------------------------------------------------

Revision: 22094
at December 31, 2009 23:19 by richardw


Initial Code
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------

#include <windows.h>
#include <stdio.h>
#include <time.h>

#define MAX_ADAPTER_DESCRIPTION_LENGTH  128 // arb.
#define MAX_ADAPTER_NAME_LENGTH         256 // arb.
#define MAX_ADAPTER_ADDRESS_LENGTH      8   // arb.

//
// IP_ADDRESS_STRING - store an IP address as a dotted decimal string
//

typedef struct
{
    char String[4 * 4];
} IP_ADDRESS_STRING, *PIP_ADDRESS_STRING, IP_MASK_STRING, *PIP_MASK_STRING;

//
// IP_ADDR_STRING - store an IP address with its corresponding subnet mask,
// both as dotted decimal strings
//

typedef struct _IP_ADDR_STRING
{
    struct _IP_ADDR_STRING* Next;
    IP_ADDRESS_STRING IpAddress;
    IP_MASK_STRING IpMask;
    DWORD Context;
} IP_ADDR_STRING, *PIP_ADDR_STRING;

//
// ADAPTER_INFO - per-adapter information. All IP addresses are stored as strings
//

typedef struct _IP_ADAPTER_INFO
{
    struct _IP_ADAPTER_INFO* Next;
    DWORD ComboIndex;
    char AdapterName[MAX_ADAPTER_NAME_LENGTH + 4];
    char Description[MAX_ADAPTER_DESCRIPTION_LENGTH + 4];
    UINT AddressLength;
    BYTE Address[MAX_ADAPTER_ADDRESS_LENGTH];
    DWORD Index;
    UINT Type;
    UINT DhcpEnabled;
    PIP_ADDR_STRING CurrentIpAddress;
    IP_ADDR_STRING IpAddressList;
    IP_ADDR_STRING GatewayList;
    IP_ADDR_STRING DhcpServer;
    BOOL HaveWins;
    IP_ADDR_STRING PrimaryWinsServer;
    IP_ADDR_STRING SecondaryWinsServer;
    time_t LeaseObtained;
    time_t LeaseExpires;
} IP_ADAPTER_INFO, *PIP_ADAPTER_INFO;

typedef DWORD (WINAPI* GetAdaptersInfoFunc)(
    PIP_ADAPTER_INFO pAdapterInfo,  // buffer to receive data
    PULONG pOutBufLen               // size of data returned
);
///////////////////////////////////////////////////////////////////////////////

void MacAddressToString(const LPBYTE Address, LPSTR lpsz, int nAddressLength = 6)
{
	int n;
	LPSTR p = lpsz;
	for (n = 0; n < nAddressLength; n++)
	{
		p += sprintf(p, n ? "-%02x" : "%02x", Address[n]);
	}
}
//---------------------------------------------------------------------------
      
AnsiString __fastcall TForm1::GetMac(void)
{
    //TODO: Add your source code here
    HMODULE hLib;
	GetAdaptersInfoFunc GetAdaptersInfo = NULL;

	PIP_ADAPTER_INFO pai = NULL;
	DWORD dwSize = 0;
	CHAR szMac[64];
	
	hLib = LoadLibrary("Iphlpapi.dll");
	if (!hLib)
	{
		//puts("Failed to load Iphlpapi.dll");
		exit(1);
	}

	GetAdaptersInfo = (GetAdaptersInfoFunc)GetProcAddress(hLib, "GetAdaptersInfo");
	if (GetAdaptersInfo == NULL)
	{
		//puts("Failed to load GetAdaptersInfo in Iphlpapi.dll");
	}

	// Get size of buffer needed:
	GetAdaptersInfo(NULL, &dwSize);

	pai = (PIP_ADAPTER_INFO)GlobalAlloc(GPTR, dwSize);
	GetAdaptersInfo(pai, &dwSize);

	PIP_ADAPTER_INFO p = pai;

    MacAddressToString(p->Address, szMac, p->AddressLength);

	GlobalFree(pai);
	FreeLibrary(hLib);
    return ((AnsiString)szMac).UpperCase();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::btn1Click(TObject *Sender)
{
    lbl1->Caption = GetMac();    
}
//---------------------------------------------------------------------------

Initial URL

                                

Initial Description
获取MAC地址

Initial Title
获取MAC地址

Initial Tags
mac

Initial Language
C++