Solved

How to get the MAC address?

Posted on 2001-06-13
4
299 Views
Last Modified: 2010-04-04
I need to get the MAC address in Delphi. There's an example which uses CoCreateGuid, but this is not the solution because as of Windows 2000 Microsoft doesn't use the MAC address any more (or encrypts it in the GUID, don't know) because of privacy.

Please post a working solution, for that I give 250 points. Thanks.

By the way (maybe this is another question), it really would be nice if the same program works on Kylix, too.
0
Comment
Question by:dnoelpp
  • 2
  • 2
4 Comments
 
LVL 3

Author Comment

by:dnoelpp
ID: 6185203
Please don't post answers, I found the answer just now, 5 minutes ago and will delete this question. thanks!
0
 
LVL 8

Accepted Solution

by:
Cesario earned 250 total points
ID: 6185205
From Microsoft:
http://support.microsoft.com/support/kb/articles/Q118/6/23.asp


The Delphi Translation

uses
  NB30;

function GetAdapterInfo(Lana: Char): String;
var
  Adapter: TAdapterStatus;
  NCB: TNCB;
begin
  FillChar(NCB, SizeOf(NCB), 0);
  NCB.ncb_command := Char(NCBRESET);
  NCB.ncb_lana_num := Lana;
  if Netbios(@NCB) <> Char(NRC_GOODRET) then
  begin
    Result := 'mac not found';
    Exit;
  end;

  FillChar(NCB, SizeOf(NCB), 0);
  NCB.ncb_command := Char(NCBASTAT);
  NCB.ncb_lana_num := Lana;
  NCB.ncb_callname := '*';

  FillChar(Adapter, SizeOf(Adapter), 0);
  NCB.ncb_buffer := @Adapter;
  NCB.ncb_length := SizeOf(Adapter);
  if Netbios(@NCB) <> Char(NRC_GOODRET) then
  begin
    Result := 'mac not found';
    Exit;
  end;
  Result :=
    IntToHex(Byte(Adapter.adapter_address[0]), 2) + '-' +
    IntToHex(Byte(Adapter.adapter_address[1]), 2) + '-' +
    IntToHex(Byte(Adapter.adapter_address[2]), 2) + '-' +
    IntToHex(Byte(Adapter.adapter_address[3]), 2) + '-' +
    IntToHex(Byte(Adapter.adapter_address[4]), 2) + '-' +
    IntToHex(Byte(Adapter.adapter_address[5]), 2);
end;

function GetMACAddress: string;
var
  AdapterList: TLanaEnum;
  NCB: TNCB;
begin
  FillChar(NCB, SizeOf(NCB), 0);
  NCB.ncb_command := Char(NCBENUM);
  NCB.ncb_buffer := @AdapterList;
  NCB.ncb_length := SizeOf(AdapterList);
  Netbios(@NCB);
  if Byte(AdapterList.length) > 0 then
    Result := GetAdapterInfo(AdapterList.lana[0])
  else
    Result := 'mac not found';
end;

Best Regards

Cesario
0
 
LVL 3

Author Comment

by:dnoelpp
ID: 6185292
Hey Cesario

The other solution I found was not so good as yours. So I decided to cancel the deletion and give you all 250 points.

I realized that you did really good work here!!! Please tell me a little more about how you translated the vb code to Delphi! I didn't know about the NB30 unit, but this was exactly the missing bit I needed. How did you know about it?

Thanks very much and please forgive me for raising a hell of confusion with deleting the question etc.
0
 
LVL 8

Expert Comment

by:Cesario
ID: 6185318
Hello dnoelpp,

the NB3 Unit is in your delphi Folder:

..\Delph4\Source\RTL\Win...

I get the translation from Knowledge Base in ower Company.
Some one put it in the Database. I think i get it from Delphi3000.com

Best Regards

Cesario
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

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…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

770 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