Solved

How to get MAC Address in Delphi

Posted on 2001-09-17
14
6,920 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
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!

 
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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi selector screen 2 92
how to update exe applicatio from internet ? 6 92
How to build JSON File in Delphi 6 3 70
Breakpoint doesn't stop in my variable 3 36
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

763 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