Solved

MAC address

Posted on 2002-05-06
5
368 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

743 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

11 Experts available now in Live!

Get 1:1 Help Now