Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
468 Users Online NOW
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi







Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (0)


Getting a Mac Address - The JEDI wayFormat this article printer-friendly!Bookmark function is only available for registered users!
Product:
Delphi 5.x (or higher)
Category:
Communication
Skill Level:
Scoring:
Last Update:
12/11/2002
Search Keys:
delphi delphi3000 article borland vcl code-snippet Mac-Address NetBios SNMP JEDI
Times Scored:
9
Visits:
10654
Uploader: Michael Brooks
Company:
Reference: N/A
 
Question/Problem/Abstract:
How to get the Mac Address for the local Windows PC (and possibly any other computer on the LAN) without writing/translating a lot of code or relying completely on NetBios support.
Answer:



The Jedi Project ( http://www.delphi-jedi.org/ ) has a library called Jedi Code Library ( JCL ) that includes a function ( inside the JclSysInfo unit ) called GetMacAddresses which returns a TStrings.  It takes a Lan "Computer Name" parameter and attempts to use NetBios to get the Mac address.  If NetBios fails and the "Computer Name" parameter is blank it uses SNMP to get the Mac address.  

When your local PC is not signed on to a network or is connected to the Internet via Cable Modem or DSL the NetBios call will likely fail and SNMP will (assuming a blank "Computer Name" was provided as a parameter) try to make up for it.  Unfortunately, the way the JCL is currently coded... it will probably get a 0 Mac Address (i.e. Result = 0) from the NetBios call and not call the SNMP code.  Well... Thank God for Open Source... To fix this change (you might choose something different) the JCL's JclSysInfo.GetMacAddresses code to look like this:

begin
  Result := -1;
  Addresses.Clear;
  GetMacAddressesNetBios();
  if (Result = 0)
    then Result := -1;
  if (Result = -1) and
     (Machine = '')
    then GetMacAddressesSnmp();
  if (Result = 0)
    then Result := -1;
end;

The JclSysInfo file is read only so you'll have to change it's rights before you try modifying the code.  This code (with the modification) will probably work better for you than NetBios calls alone (it did for me on W2K connected via Cable Modem where NetBios didn't).

Now...

To get a Mac Address for the local computer (once you've modified the code as mentioned above), within a project of your choice, do the following:

1) Under Project Options, add the JEDI Source directory to your project's "Search Path".
2) Include the "JclSysInfo" unit in your project's "uses" clause.
3) Include the following code in one of your project's methods:

  var
    MacAddressStrings: TStringList;
    MacAddress: String;

  begin

    MacAddressStrings := TStringList.Create();
    JclSysInfo.GetMacAddresses('', MacAddressStrings);
    MacAddress:= MacAddressStrings[0];
    MacAddressStrings.Free();

  end;

With a few simple modifications (e.g. the Computer Name parameter and the reference to the first element of the TStringList) you could also use this code to get multiple local PC MAC addresses or Mac Addresses for remote LAN computers.

As mentioned... this code works better than the NetBios only solution on my W2K + CableModem PC.  I haven't tried this yet on Win95 (which I have as a dual boot and will try soon but... NetBios ONLY worked there anyway) or Win98 (which I don't have) or WNT 4.0 (which I don't have anymore).  So... I'd appreciate any feedback on whether this works on different version of Windows or even Linux (assuming the JCL works with Kylix).

Good Luck,

Michael.





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment













 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
M. Kleiner
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)