Solved

How to get the MAC address?

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

856 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