Solved

How to get MAC Address in Delphi

Posted on 2001-09-17
14
6,743 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: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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Sending Gmail through Delphi 3 108
Working with hours 3 58
Dynamically Created Query 3 55
Delphi and Access based Enumeration 9 64
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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

778 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