Mega Code Archive

 
Categories / Delphi / LAN Web TCP
 

Getting a Mac Address The JEDI way

Title: Getting a Mac Address - The JEDI way Question: 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.