 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Momchil Hristov Minchev Guest
|
Posted: Thu Nov 06, 2003 11:07 pm Post subject: GetMACAddress |
|
|
I need a ready component with source code with function GetMACAddress (or
other name but with same functionality). Please help!
Thanx in advance!
|
|
| Back to top |
|
 |
Cruxy Guest
|
Posted: Fri Nov 07, 2003 9:08 am Post subject: Re: GetMACAddress |
|
|
This should Work....
interface
const
MAXLEN_IFDESCR = 256;
{$EXTERNALSYM MAXLEN_IFDESCR}
MAXLEN_PHYSADDR = 8;
{$EXTERNALSYM MAXLEN_PHYSADDR}
MAX_INTERFACE_NAME_LEN = 256; // MPRAPI.H
{$EXTERNALSYM MAX_INTERFACE_NAME_LEN}
ANY_SIZE = 1;
{$EXTERNALSYM ANY_SIZE}
MIB_IF_TYPE_OTHER = 1;
{$EXTERNALSYM MIB_IF_TYPE_OTHER}
MIB_IF_TYPE_ETHERNET = 6;
{$EXTERNALSYM MIB_IF_TYPE_ETHERNET}
MIB_IF_TYPE_TOKENRING = 9;
{$EXTERNALSYM MIB_IF_TYPE_TOKENRING}
MIB_IF_TYPE_FDDI = 15;
{$EXTERNALSYM MIB_IF_TYPE_FDDI}
MIB_IF_TYPE_PPP = 23;
{$EXTERNALSYM MIB_IF_TYPE_PPP}
MIB_IF_TYPE_LOOPBACK = 24;
{$EXTERNALSYM MIB_IF_TYPE_LOOPBACK}
MIB_IF_TYPE_SLIP = 28;
{$EXTERNALSYM MIB_IF_TYPE_SLIP}
MIB_IF_ADMIN_STATUS_UP = 1;
{$EXTERNALSYM MIB_IF_ADMIN_STATUS_UP}
MIB_IF_ADMIN_STATUS_DOWN = 2;
{$EXTERNALSYM MIB_IF_ADMIN_STATUS_DOWN}
MIB_IF_ADMIN_STATUS_TESTING = 3;
{$EXTERNALSYM MIB_IF_ADMIN_STATUS_TESTING}
MIB_IF_OPER_STATUS_NON_OPERATIONAL = 0;
{$EXTERNALSYM MIB_IF_OPER_STATUS_NON_OPERATIONAL}
MIB_IF_OPER_STATUS_UNREACHABLE = 1;
{$EXTERNALSYM MIB_IF_OPER_STATUS_UNREACHABLE}
MIB_IF_OPER_STATUS_DISCONNECTED = 2;
{$EXTERNALSYM MIB_IF_OPER_STATUS_DISCONNECTED}
MIB_IF_OPER_STATUS_CONNECTING = 3;
{$EXTERNALSYM MIB_IF_OPER_STATUS_CONNECTING}
MIB_IF_OPER_STATUS_CONNECTED = 4;
{$EXTERNALSYM MIB_IF_OPER_STATUS_CONNECTED}
MIB_IF_OPER_STATUS_OPERATIONAL = 5;
{$EXTERNALSYM MIB_IF_OPER_STATUS_OPERATIONAL}
type
TMyByteArray = array of Byte;
DWORD = LongWord;
ULONG = Cardinal;
BOOL = LongBool;
PMIB_IFROW = ^MIB_IFROW;
{$EXTERNALSYM PMIB_IFROW}
_MIB_IFROW = record
wszName: array [0..MAX_INTERFACE_NAME_LEN - 1] of WideChar;
dwIndex: DWORD;
dwType: DWORD;
dwMtu: DWORD;
dwSpeed: DWORD;
dwPhysAddrLen: DWORD;
bPhysAddr: array [0..MAXLEN_PHYSADDR - 1] of BYTE;
dwAdminStatus: DWORD;
dwOperStatus: DWORD;
dwLastChange: DWORD;
dwInOctets: DWORD;
dwInUcastPkts: DWORD;
dwInNUcastPkts: DWORD;
dwInDiscards: DWORD;
dwInErrors: DWORD;
dwInUnknownProtos: DWORD;
dwOutOctets: DWORD;
dwOutUcastPkts: DWORD;
dwOutNUcastPkts: DWORD;
dwOutDiscards: DWORD;
dwOutErrors: DWORD;
dwOutQLen: DWORD;
dwDescrLen: DWORD;
bDescr: array[0..MAXLEN_IFDESCR - 1] of BYTE;
end;
{$EXTERNALSYM _MIB_IFROW}
MIB_IFROW = _MIB_IFROW;
{$EXTERNALSYM MIB_IFROW}
TMibIfRow = MIB_IFROW;
PMibIfRow = PMIB_IFROW;
PMIB_IFTABLE = ^MIB_IFTABLE;
{$EXTERNALSYM PMIB_IFTABLE}
_MIB_IFTABLE = record
dwNumEntries: DWORD;
table: array [0..ANY_SIZE - 1] of MIB_IFROW;
end;
{$EXTERNALSYM _MIB_IFTABLE}
MIB_IFTABLE = _MIB_IFTABLE;
{$EXTERNALSYM MIB_IFTABLE}
TMibIftable = MIB_IFTABLE;
PMibIftable = PMIB_IFTABLE;
function GetNetInterfaceData: pMIB_IFTABLE;
function GetNetIntMAC(IntNo: Integer): TMyByteArray;
function GetIfTable(pIfTable: PMIB_IFTABLE; var pdwSize: ULONG; bOrder:
BOOL): DWORD; stdcall;
{$EXTERNALSYM GetIfTable}
implementation
uses
SysUtils;
function GetIfTable; external 'iphlpapi.dll' name 'GetIfTable';
{This function retrieves General information from all Network-
Interfaces Present in the computer}
function GetNetInterfaceData: pMIB_IFTABLE;
var
NoOfInt: Cardinal;
BuffSiz: Cardinal;
data: TMyByteArray;
ret: Integer;
begin
BuffSiz:=0;
GetIfTable(Result,BuffSiz,False); //if a size of 0 is passed in, the
//needed Size of the buffer is
// returned in BuffSiz!
setlength(data,BuffSiz); //set the length (in byte) needed by the
//structure...
Result:=pMIB_IFTABLE(@data[0]);
ret:=GetIfTable(Result,BuffSiz,False);
if ret<>0 then raise Exception.Create('An Error occured...');
end;
{This function extracts the MAC Address of the Information retrieved
with the function above. The length of the MAC could be determined
using length()}
function GetNetIntMAC(IntNo: Integer): TMyByteArray;
var
data: pMIB_IFTABLE;
i: Integer;
begin
data:=GetNetInterfaceData;
if (intno<(data^.dwNumEntries-1)) and (intno>=0) then
begin
setlength(Result,data^.Table[IntNo].dwPhysAddrLen);
move(data^.Table[IntNo].bPhysAddr,Result[0],Length(Result));
end;
end;
end.
|
|
| Back to top |
|
 |
Ricardo Oliveira Guest
|
Posted: Fri Nov 07, 2003 5:02 pm Post subject: Re: GetMACAddress |
|
|
Hi
I've tested your code but i'm not sure how to use it.
Could you send me a simple example.
Thanks.
"Cruxy" <cruxyATcruxyDOTnet> escreveu na mensagem
news:3fab6f26$1 (AT) newsgroups (DOT) borland.com...
| Quote: | This should Work....
interface
const
MAXLEN_IFDESCR = 256;
{$EXTERNALSYM MAXLEN_IFDESCR}
MAXLEN_PHYSADDR = 8;
{$EXTERNALSYM MAXLEN_PHYSADDR}
MAX_INTERFACE_NAME_LEN = 256; // MPRAPI.H
{$EXTERNALSYM MAX_INTERFACE_NAME_LEN}
ANY_SIZE = 1;
{$EXTERNALSYM ANY_SIZE}
MIB_IF_TYPE_OTHER = 1;
{$EXTERNALSYM MIB_IF_TYPE_OTHER}
MIB_IF_TYPE_ETHERNET = 6;
{$EXTERNALSYM MIB_IF_TYPE_ETHERNET}
MIB_IF_TYPE_TOKENRING = 9;
{$EXTERNALSYM MIB_IF_TYPE_TOKENRING}
MIB_IF_TYPE_FDDI = 15;
{$EXTERNALSYM MIB_IF_TYPE_FDDI}
MIB_IF_TYPE_PPP = 23;
{$EXTERNALSYM MIB_IF_TYPE_PPP}
MIB_IF_TYPE_LOOPBACK = 24;
{$EXTERNALSYM MIB_IF_TYPE_LOOPBACK}
MIB_IF_TYPE_SLIP = 28;
{$EXTERNALSYM MIB_IF_TYPE_SLIP}
MIB_IF_ADMIN_STATUS_UP = 1;
{$EXTERNALSYM MIB_IF_ADMIN_STATUS_UP}
MIB_IF_ADMIN_STATUS_DOWN = 2;
{$EXTERNALSYM MIB_IF_ADMIN_STATUS_DOWN}
MIB_IF_ADMIN_STATUS_TESTING = 3;
{$EXTERNALSYM MIB_IF_ADMIN_STATUS_TESTING}
MIB_IF_OPER_STATUS_NON_OPERATIONAL = 0;
{$EXTERNALSYM MIB_IF_OPER_STATUS_NON_OPERATIONAL}
MIB_IF_OPER_STATUS_UNREACHABLE = 1;
{$EXTERNALSYM MIB_IF_OPER_STATUS_UNREACHABLE}
MIB_IF_OPER_STATUS_DISCONNECTED = 2;
{$EXTERNALSYM MIB_IF_OPER_STATUS_DISCONNECTED}
MIB_IF_OPER_STATUS_CONNECTING = 3;
{$EXTERNALSYM MIB_IF_OPER_STATUS_CONNECTING}
MIB_IF_OPER_STATUS_CONNECTED = 4;
{$EXTERNALSYM MIB_IF_OPER_STATUS_CONNECTED}
MIB_IF_OPER_STATUS_OPERATIONAL = 5;
{$EXTERNALSYM MIB_IF_OPER_STATUS_OPERATIONAL}
type
TMyByteArray = array of Byte;
DWORD = LongWord;
ULONG = Cardinal;
BOOL = LongBool;
PMIB_IFROW = ^MIB_IFROW;
{$EXTERNALSYM PMIB_IFROW}
_MIB_IFROW = record
wszName: array [0..MAX_INTERFACE_NAME_LEN - 1] of WideChar;
dwIndex: DWORD;
dwType: DWORD;
dwMtu: DWORD;
dwSpeed: DWORD;
dwPhysAddrLen: DWORD;
bPhysAddr: array [0..MAXLEN_PHYSADDR - 1] of BYTE;
dwAdminStatus: DWORD;
dwOperStatus: DWORD;
dwLastChange: DWORD;
dwInOctets: DWORD;
dwInUcastPkts: DWORD;
dwInNUcastPkts: DWORD;
dwInDiscards: DWORD;
dwInErrors: DWORD;
dwInUnknownProtos: DWORD;
dwOutOctets: DWORD;
dwOutUcastPkts: DWORD;
dwOutNUcastPkts: DWORD;
dwOutDiscards: DWORD;
dwOutErrors: DWORD;
dwOutQLen: DWORD;
dwDescrLen: DWORD;
bDescr: array[0..MAXLEN_IFDESCR - 1] of BYTE;
end;
{$EXTERNALSYM _MIB_IFROW}
MIB_IFROW = _MIB_IFROW;
{$EXTERNALSYM MIB_IFROW}
TMibIfRow = MIB_IFROW;
PMibIfRow = PMIB_IFROW;
PMIB_IFTABLE = ^MIB_IFTABLE;
{$EXTERNALSYM PMIB_IFTABLE}
_MIB_IFTABLE = record
dwNumEntries: DWORD;
table: array [0..ANY_SIZE - 1] of MIB_IFROW;
end;
{$EXTERNALSYM _MIB_IFTABLE}
MIB_IFTABLE = _MIB_IFTABLE;
{$EXTERNALSYM MIB_IFTABLE}
TMibIftable = MIB_IFTABLE;
PMibIftable = PMIB_IFTABLE;
function GetNetInterfaceData: pMIB_IFTABLE;
function GetNetIntMAC(IntNo: Integer): TMyByteArray;
function GetIfTable(pIfTable: PMIB_IFTABLE; var pdwSize: ULONG; bOrder:
BOOL): DWORD; stdcall;
{$EXTERNALSYM GetIfTable}
implementation
uses
SysUtils;
function GetIfTable; external 'iphlpapi.dll' name 'GetIfTable';
{This function retrieves General information from all Network-
Interfaces Present in the computer}
function GetNetInterfaceData: pMIB_IFTABLE;
var
NoOfInt: Cardinal;
BuffSiz: Cardinal;
data: TMyByteArray;
ret: Integer;
begin
BuffSiz:=0;
GetIfTable(Result,BuffSiz,False); //if a size of 0 is passed in, the
//needed Size of the buffer is
// returned in BuffSiz!
setlength(data,BuffSiz); //set the length (in byte) needed by the
//structure...
Result:=pMIB_IFTABLE(@data[0]);
ret:=GetIfTable(Result,BuffSiz,False);
if ret<>0 then raise Exception.Create('An Error occured...');
end;
{This function extracts the MAC Address of the Information retrieved
with the function above. The length of the MAC could be determined
using length()}
function GetNetIntMAC(IntNo: Integer): TMyByteArray;
var
data: pMIB_IFTABLE;
i: Integer;
begin
data:=GetNetInterfaceData;
if (intno<(data^.dwNumEntries-1)) and (intno>=0) then
begin
setlength(Result,data^.Table[IntNo].dwPhysAddrLen);
move(data^.Table[IntNo].bPhysAddr,Result[0],Length(Result));
end;
end;
end.
|
|
|
| Back to top |
|
 |
Cruxy Guest
|
Posted: Wed Nov 12, 2003 7:44 am Post subject: Re: GetMACAddress |
|
|
Copy and paste the code to a textfile -> put a "Unit ?;" before my code
and save it as "?.pas"
For information on the MAC address call GetNetIntMac(InterfaceNumber).
The result as an array of byte (the length is variable and could be
retrieved with "length()" )
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|