Solved

How to get MAC Address in Delphi

Posted on 2001-09-17
14
6,507 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
  • 3
  • 2
  • 2
  • +6
14 Comments
 
LVL 22

Expert Comment

by:mnasman
Comment Utility
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
Comment Utility
0
 
LVL 2

Expert Comment

by:tarang
Comment Utility
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
 
LVL 2

Expert Comment

by:gikam
Comment Utility
here you have sources and demos!

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

Author Comment

by:kittichai
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 22

Expert Comment

by:mnasman
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
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…
This video discusses moving either the default database or any database to a new volume.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now