Newsgroups : Borland : borland.public.delphi.internet.winsock : 2007 Nov : Re: IP to MAC-address code?

www.cryer.info
Managed Newsgroup Archive

Re: IP to MAC-address code?

Subject:Re: IP to MAC-address code?
Posted by:"Jamie Dale" (jamie.da..@yahoo.com)
Date:Mon, 12 Nov 2007 21:31:13

I found 2 ways of doing this ages ago and subsequently made 2 demo progs for
my own use. I seem to remember both methods had advantages and disadvantages
over the other. I'll post the first entire unit here, and the other ones
unit in a seperate reply:

unit GetMacAddress_Source;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Button1: TButton;
    Label1: TLabel;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function GetMacAddress(const AServerName : string) : string;
type
     TNetTransportEnum = function(pszServer : PWideChar;
                                  Level : DWORD;
                                  var pbBuffer : pointer;
                                  PrefMaxLen : LongInt;
                                  var EntriesRead : DWORD;
                                  var TotalEntries : DWORD;
                                  var ResumeHandle : DWORD) : DWORD;
stdcall;

     TNetApiBufferFree = function(Buffer : pointer) : DWORD; stdcall;

     PTransportInfo = ^TTransportInfo;
     TTransportInfo = record
                       quality_of_service : DWORD;
                       number_of_vcs : DWORD;
                       transport_name : PWChar;
                       transport_address : PWChar;
                       wan_ish : boolean;
                     end;

var E,ResumeHandle,
    EntriesRead,
    TotalEntries : DWORD;
    FLibHandle : THandle;
    sMachineName,
    sMacAddr,
    Retvar : string;
    pBuffer : pointer;
    pInfo : PTransportInfo;
    FNetTransportEnum : TNetTransportEnum;
    FNetApiBufferFree : TNetApiBufferFree;
    pszServer : array[0..128] of WideChar;
    i,ii,iIdx : integer;
begin
  sMachineName := trim(AServerName);
  Retvar := '00-00-00-00-00-00';

  // Add leading \ if missing
  if (sMachineName <> '') and (length(sMachineName) >= 2) then begin
    if copy(sMachineName,1,2) <> '\' then
      sMachineName := '\' + sMachineName
  end;

  // Setup and load from DLL
  pBuffer := nil;
  ResumeHandle := 0;
  FLibHandle := LoadLibrary('NETAPI32.DLL');

  // Execute the external function
  if FLibHandle <> 0 then begin
    @FNetTransportEnum :=
GetProcAddress(FLibHandle,'NetWkstaTransportEnum');
    @FNetApiBufferFree := GetProcAddress(FLibHandle,'NetApiBufferFree');
    E := FNetTransportEnum(StringToWideChar(sMachineName,pszServer,129),0,
                           pBuffer,-1,EntriesRead,TotalEntries,Resumehandle);

    if E = 0 then begin
      pInfo := pBuffer;

      // Enumerate all protocols - look for TCPIP
      for i := 1 to EntriesRead do begin
        if pos('TCPIP',UpperCase(pInfo^.transport_name)) <> 0 then begin
          // Got It - now format result 'xx-xx-xx-xx-xx-xx'
          iIdx := 1;
          sMacAddr := pInfo^.transport_address;

          for ii := 1 to 12 do begin
            Retvar[iIdx] := sMacAddr[ii];
            inc(iIdx);
            if iIdx in [3,6,9,12,15] then inc(iIdx);
          end;
        end;

        inc(pInfo);
      end;
      if pBuffer <> nil then FNetApiBufferFree(pBuffer);
    end;

    try
      FreeLibrary(FLibHandle);
    except
      // Silent Error
    end;
  end;

  Result := Retvar;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage(GetMacAddress(Edit1.Text));
end;

end.

Replies:

none

In response to:

www.cryer.info
Managed Newsgroup Archive