Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to get MAC Address in Delphi

Posted on 2001-09-17
14
Medium Priority
?
7,378 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 800 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

670 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