Solved

How to get MAC Address in Delphi

Posted on 2001-09-17
14
7,015 Views
Last Modified: 2010-05-18
I want to retrieve the MAC Address of installed LAN card on local machine using Delphi Pascal. Anyone has code?
0
Comment
Question by:kittichai
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +6
14 Comments
 
LVL 22

Expert Comment

by:Mohammed Nasman
ID: 6489466
Hello

 you can Download a freeware Delphi-unit for retrieving the MAC-Address of LAN/WAN-adapters (getmacad.zip - 3Kb)
from

http://vladshar.ufanet.ru/
http://www.delphi32.com/vcl/2239/

Best regards
Mohammed Nasman

0
 
LVL 8

Expert Comment

by:TOndrej
ID: 6489532
0
 
LVL 2

Expert Comment

by:tarang
ID: 6489687
Sorry this is C code but you should be able to translate it quite easily into Pascal.  BTW you have to link with Netapi32.lib.

#include <nb30.h>


typedef struct
{
    ADAPTER_STATUS  AdapterStatus;
    NAME_BUFFER     NameBuffer [ 30 ];
} ASTAT;



void MacAddress()
{

    NCB                                 Ncb;
    ASTAT                               Adapter;

    memset ( &Ncb, 0, sizeof ( Ncb ) );
    Ncb.ncb_command = NCBRESET;
    Netbios ( &Ncb );
    Ncb.ncb_command = NCBASTAT;
    strcpy ( Ncb.ncb_callname, "*               " );
    Ncb.ncb_buffer = ( char* ) &Adapter;
    Ncb.ncb_length = sizeof ( Adapter );
    Netbios ( &Ncb );
    // At this point the addapter address is in
    // Adapter.AdapterStatus.adapter_address [ 0 ] through
    // Adapter.AdapterStatus.adapter_address [ 5 ],
}
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:gikam
ID: 6489823
here you have sources and demos!

http://www.delphipages.com/edit/count.cfm?ID=1720
0
 

Author Comment

by:kittichai
ID: 6489995
I have used all above solutions. But the MAC Address I received is not the same as what Windows System information told me. I did copy their code and work on, and the result are 3 MAC Address. I compare it with report from Windows System Inforation, the last of those 3 is correct. But how could I know which one? Do I alway receive 3 MAC Address? Is the correct MAC Address always the last one? Or the third one? What happen if I run the program on machine with multiple Network Adapter?
0
 
LVL 2

Expert Comment

by:gikam
ID: 6490250
if u receive more than one addr. then u perhaps have one or more NICs or one NIC and 2 dialup connections (modems), which are considered also adapters. the NCB structure contains a field which tells whish adapter's status to return (0,1,2.....) u wont know which is which
0
 
LVL 22

Expert Comment

by:Mohammed Nasman
ID: 6490272
Hello

  The link I gave to you http://vladshar.ufanet.ru/
 contian the lib and the project, and it's worked fine and gave the right Mac address

  did u try it?
0
 

Author Comment

by:kittichai
ID: 6490765
For mnasman,
  I already try that library. It requires LANA parameter before returning MAC Address. They also list all available LANA parameter, but I only want the MAC Address of LAN Card. Yes, one of it was the correct MAC Address I willing to know. But how can I know the right LANA when I run it on different machine? In my situation, I can't go to every machine to check the correct LANA. How could I detect that which one is LAN CARD?
0
 

Accepted Solution

by:
ssjjll earned 200 total points
ID: 6492179
uses winsock,...

...

Function sendarp(ipaddr:ulong;
                 temp:dword;
                 ulmacaddr:pointer;
                 ulmacaddrleng:pointer) : DWord; StdCall;


implementation

{$R *.dfm}

Function sendarp; External 'Iphlpapi.dll' Name 'SendARP';


procedure TForm1.Button1Click(Sender: TObject);
var
  myip:ulong;
  mymac:array[0..5] of byte;
  mymaclength:ulong;
  r:integer;
begin
  myip:=inet_addr(PChar('192.168.1.1'));
  mymaclength:=length(mymac);
  r:=sendarp(myip,0,@mymac,@mymaclength);
  label1.caption:='errorcode:'+inttostr(r);
  label2.caption:=format('%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x',[mymac[0],mymac[1],mymac[2],mymac[3],mymac[4],mymac[5]]);
end;
0
 
LVL 1

Expert Comment

by:Oli2
ID: 6493251
Note that if you use Win95 and you set it to NOT use NetBios (in Win95 this was possible), all functions using Netbios will not get the correct result.

Oli
0
 
LVL 2

Expert Comment

by:alexstewart@beta
ID: 6493497
I like the arp solution,
Thats the way IP protocols do it.
It needs a GetMy_IP function though, otherwise it only works if the machine has that IP (192.168.1.1)

nice one ssjjll.
0
 

Author Comment

by:kittichai
ID: 6495659
Thank you everyone for the answers. Many network device such as LAN Card or Modem can generated MAC Address. But ssjjll 's solution seems to be the best since I can get the Ethernet MAC Address, which is what I want.
0
 
LVL 17

Expert Comment

by:geobul
ID: 9308517
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

accept ssjjll's comment as answer

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Thanks,

geobul
EE Cleanup Volunteer
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question