Solved

How to get the MAC address?

Posted on 2001-06-13
4
294 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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 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 video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

760 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

20 Experts available now in Live!

Get 1:1 Help Now