robballi
asked on
get mac address from C++ builder
Please a code sample to get the PC mac address from C++ builder.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
best regards...
My Way:
//kidding...Khalid Shaikh Way:
I executed under V.S. 2008 and OK!
//kidding...Khalid Shaikh Way:
I executed under V.S. 2008 and OK!
// GetMACUuid.cpp : Defines the entry point for the console application.
//
// Author: Khalid Shaikh [Shake@ShakeNet.com]
// Date: April 5th, 2002
//
// This program fetches the MAC address of the localhost by creating a UUID
// and obtaining the IP address through that
//
// Prewindows 2000 one should replace the function below
// UuidCreateSequential with UuidCreate
// Microsoft decided that it was not wise to put the MAC address in the UUID
// hence changed the algorithm in Windows 2000 and later to not include it.
//
// Supported in Windows NT/2000/XP
// Supported in Windows 95/98/Me
//
// Supports single NIC card.
#include "stdafx.h"
#include <Windows.h>
#include <rpc.h>
#include <rpcdce.h>
#pragma comment(lib, "rpcrt4.lib")
// Prints the MAC address stored in a 6 byte array to stdout
static void PrintMACaddress(unsigned char MACData[])
{
printf("MAC Address: %02X-%02X-%02X-%02X-%02X-%02X\n",
MACData[0], MACData[1], MACData[2], MACData[3], MACData[4], MACData[5]);
}
// Fetches the MAC address and prints it
static void GetMACaddress(void)
{
unsigned char MACData[6];
UUID uuid;
UuidCreateSequential( &uuid ); // Ask OS to create UUID
for (int i=2; i<8; i++) // Bytes 2 through 7 inclusive are MAC address
MACData[i - 2] = uuid.Data4[i];
PrintMACaddress(MACData); // Print MAC address
}
int _tmain(int argc, _TCHAR* argv[])
{
GetMACaddress(); // Obtain MAC address of adapters
return 0;
}
MyWay.jpg
sad but i have no C++ builder by the way...
ASKER
Excelent i had to do some hacking to put it to work under windows but it works!!
Thanks a lot
Thanks a lot
<<Excelent i had to do some hacking to put it to work under windows but it works!!
<<Thanks a lot
Hey, could you send your modified code?
<<Thanks a lot
Hey, could you send your modified code?
ASKER
Sorry jazzIIIlove:
I had some bussy time here.
I just made a class like:
//------------------------ ---------- ---------- ---------- ---------- ---------- -
#ifndef MacH
#define MacH
#include <sstream.h>
//------------------------ ---------- ---------- ---------- ---------- ---------- -
class Mac {
public:
string mac();
};
#endif
And modified your code to get the string, as i have only one mac address i used a string:
#include "Mac.h"
#include <windows.h>
#include <wincon.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
typedef struct _ASTAT_ {
ADAPTER_STATUS adapt;
NAME_BUFFER NameBuff [30];
} ASTAT, * PASTAT;
ASTAT Adapter;
string Mac::mac() {
NCB Ncb;
UCHAR uRetCode;
char NetName[50];
LANA_ENUM lenum;
int i;
char mac_addr[12];
string macy;
string mac_address;
memset( &Ncb, 0, sizeof(Ncb) );
Ncb.ncb_command = NCBENUM;
Ncb.ncb_buffer = (UCHAR *)&lenum;
Ncb.ncb_length = sizeof(lenum);
uRetCode = Netbios( &Ncb );
printf( "The NCBENUM return code is: 0x%x \n", uRetCode );
int n = lenum.length;
for(i=0; i < n ;i++)
{
memset( &Ncb, 0, sizeof(Ncb) );
Ncb.ncb_command = NCBRESET;
Ncb.ncb_lana_num = lenum.lana[i];
uRetCode = Netbios( &Ncb );
printf( "The NCBRESET on LANA %d return code is: 0x%x \n",
lenum.lana[i], uRetCode );
memset( &Ncb, 0, sizeof (Ncb) );
Ncb.ncb_command = NCBASTAT;
Ncb.ncb_lana_num = lenum.lana[i];
strcpy( Ncb.ncb_callname, "* " );
Ncb.ncb_buffer = (char *) &Adapter;
Ncb.ncb_length = sizeof(Adapter);
uRetCode = Netbios( &Ncb );
printf( "The NCBASTAT on LANA %d return code is: 0x%x \n",
lenum.lana[i], uRetCode );
if ( uRetCode == 0 ) {
sprintf( mac_addr,"%02x%02x%02x%02x %02x%02x",
Adapter.adapt.adapter_addr ess[0],
Adapter.adapt.adapter_addr ess[1],
Adapter.adapt.adapter_addr ess[2],
Adapter.adapt.adapter_addr ess[3],
Adapter.adapt.adapter_addr ess[4],
Adapter.adapt.adapter_addr ess[5] );
}
macy = mac_addr;
}
return macy;
}
then you just instanciate the object on a standard window form to get the string value from the class just described!!
like
string macaddress;
Mac mac;
macaddress = mac.mac();
Thanks for your help!
I had some bussy time here.
I just made a class like:
//------------------------
#ifndef MacH
#define MacH
#include <sstream.h>
//------------------------
class Mac {
public:
string mac();
};
#endif
And modified your code to get the string, as i have only one mac address i used a string:
#include "Mac.h"
#include <windows.h>
#include <wincon.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
typedef struct _ASTAT_ {
ADAPTER_STATUS adapt;
NAME_BUFFER NameBuff [30];
} ASTAT, * PASTAT;
ASTAT Adapter;
string Mac::mac() {
NCB Ncb;
UCHAR uRetCode;
char NetName[50];
LANA_ENUM lenum;
int i;
char mac_addr[12];
string macy;
string mac_address;
memset( &Ncb, 0, sizeof(Ncb) );
Ncb.ncb_command = NCBENUM;
Ncb.ncb_buffer = (UCHAR *)&lenum;
Ncb.ncb_length = sizeof(lenum);
uRetCode = Netbios( &Ncb );
printf( "The NCBENUM return code is: 0x%x \n", uRetCode );
int n = lenum.length;
for(i=0; i < n ;i++)
{
memset( &Ncb, 0, sizeof(Ncb) );
Ncb.ncb_command = NCBRESET;
Ncb.ncb_lana_num = lenum.lana[i];
uRetCode = Netbios( &Ncb );
printf( "The NCBRESET on LANA %d return code is: 0x%x \n",
lenum.lana[i], uRetCode );
memset( &Ncb, 0, sizeof (Ncb) );
Ncb.ncb_command = NCBASTAT;
Ncb.ncb_lana_num = lenum.lana[i];
strcpy( Ncb.ncb_callname, "* " );
Ncb.ncb_buffer = (char *) &Adapter;
Ncb.ncb_length = sizeof(Adapter);
uRetCode = Netbios( &Ncb );
printf( "The NCBASTAT on LANA %d return code is: 0x%x \n",
lenum.lana[i], uRetCode );
if ( uRetCode == 0 ) {
sprintf( mac_addr,"%02x%02x%02x%02x
Adapter.adapt.adapter_addr
Adapter.adapt.adapter_addr
Adapter.adapt.adapter_addr
Adapter.adapt.adapter_addr
Adapter.adapt.adapter_addr
Adapter.adapt.adapter_addr
}
macy = mac_addr;
}
return macy;
}
then you just instanciate the object on a standard window form to get the string value from the class just described!!
like
string macaddress;
Mac mac;
macaddress = mac.mac();
Thanks for your help!
Open in new window