Solved

MAC address

Posted on 2002-05-06
5
371 Views
Last Modified: 2010-04-04
Is there a better way to get MAC address from lan adapter.
This one require microsoft client for network to be installed.

Ex1….  

function CoCreateGuid(P:Pointer):Integer;stdcall;external 'ole32.dll';


function GetNicAddr:AnsiString;
var
  T:AnsiString;
  I:Integer;
  result2:String;
begin
  Result:='';
  Result2:='';
  SetLength(T,16);
  if CoCreateGUID(@T[1])=S_OK then begin
  for I:=11 to 15 do Result2:=Result2+IntToHex(Ord(T[I]),2);
  macout:=Result2+IntToHex(Ord(T[16]),2);
  ShowMessage(macout);
 end;
end;
0
Comment
Question by:Dark_King
  • 3
  • 2
5 Comments
 
LVL 8

Accepted Solution

by:
Cesario earned 300 total points
ID: 6991176
Hi Dark King,

first thanx for the point of your prev. Question ;-)


Try this:

{+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 + Unit.............MACADRESS.PAS
 + Description...... Retrive the Hardware-Adress of an ethernet adapther
 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
unit MacAdress;

interface

uses SysUtils,NB30;

type
  TMACAdress  = array[0..5] of byte;
  ENCBError   = class(Exception);

function GetMACAdress(AdapterNum:byte):TMACAdress;
function MACAdressToStr(aMACAdr:TMACAdress):string;
function IsMACAdress(aMACAdr:TMACAdress):boolean;

var
  MacAdressFormat:string;

implementation

ResourceString
  SNCBError='NetBios-Fehler %d';

type TAStat=packed record
              Adapter_Status:TAdapterStatus;
              Name_Buffer:array[0..49] of TNameBuffer;
            end;

function MACAdressToStr(aMACAdr:TMACAdress):string;
begin
  Result:=Format( MacAdressFormat,
                  [aMACAdr[0],
                  aMACAdr[1],
                  aMACAdr[2],
                  aMACAdr[3],
                  aMACAdr[4],
                  aMACAdr[5]]
                );
end;

procedure CheckResult(aReturnCode:char);
  begin
    if Byte(aReturnCode)<>NRC_GOODRET then
      raise ENCBError.Create(Format(SNCBError,[byte(aReturnCode)]));
  end;

function IsMACAdress(aMACAdr:TMACAdress):boolean;
  var i:integer;
  begin
    for i:=0 to 5 do if aMACAdr[i]<>0 then begin Result:=True; exit end;
    Result:=False;
  end;

function GetMACAdress(AdapterNum:byte):TMACAdress;
  var Adapter:TAStat;
      NCB:TNCB;
  begin
    FillChar(NCB,SizeOf(NCB),0);
    NCB.ncb_command:=char(NCBRESET);
    NCB.ncb_lana_num:=char(AdapterNum);
    CheckResult(NetBIOS(@NCB));

    FillChar(NCB,SizeOf(NCB),0);
    NCB.ncb_command:=char(NCBASTAT);
    NCB.ncb_lana_num:=char(AdapterNum);

    StrCopy(NCB.ncb_callname,'*               ');
    NCB.ncb_buffer:=@Adapter;
    NCB.ncb_length:=sizeof(Adapter);
    CheckResult(NetBIOS(@NCB));

    Move(Adapter.Adapter_Status.adapter_address[0],Result[0],SizeOf(Result));
  end;

initialization
  MacAdressFormat:='%2.2x-%2.2x-%2.2x-%2.2x-%2.2x-%2.2x';
end.
0
 
LVL 8

Expert Comment

by:Cesario
ID: 6991198
Exmaple:

procedure TForm1.Button1click(Sender: TObject);
begin
  showMessage ( MACAdressToStr(GetMACAdress(0)));
end;
0
 
LVL 6

Author Comment

by:Dark_King
ID: 6991228
I not 100% shore but I think you need Microsoft client as you call fore netbios funktion..

I have test a Nb30 before and it didn’t work on all computers,, best would be to use same ass
Microsoft use in winipcfg or ipconfig /all…  
0
 
LVL 6

Author Comment

by:Dark_King
ID: 6991288
I while test this one at home to night, cu to morrow..
0
 
LVL 6

Author Comment

by:Dark_King
ID: 6993121
Great  (**lol**)
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
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…

773 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