Ad:
 
0

Get MAC (Ethernet) Address of NIC

by Lord Soth on Mar 18th, 2006
In case there are mote than one NIC (Network Interface Card) the coe gives the MAC of the first adapter. You can change the 0 on the line qoted below to take the MAC of other NICs if present.

"Result := GetAdapterInfo(AdapterList.lana[0]) "
Pascal and Delphi Code Snippet (Toggle Plain Text)
  1. uses NB30;
  2.  
  3. function GetAdapterInfo(Lana: Char): String;
  4. var
  5. Adapter: TAdapterStatus;
  6. NCB: TNCB;
  7. begin
  8. FillChar(NCB, SizeOf(NCB), 0);
  9. NCB.ncb_command := Char(NCBRESET);
  10. NCB.ncb_lana_num := Lana;
  11. if Netbios(@NCB) <> Char(NRC_GOODRET) then
  12. begin
  13. Result := 'mac not found';
  14. Exit;
  15. end;
  16.  
  17. FillChar(NCB, SizeOf(NCB), 0);
  18. NCB.ncb_command := Char(NCBASTAT);
  19. NCB.ncb_lana_num := Lana;
  20. NCB.ncb_callname := '*';
  21.  
  22. FillChar(Adapter, SizeOf(Adapter), 0);
  23. NCB.ncb_buffer := @Adapter;
  24. NCB.ncb_length := SizeOf(Adapter);
  25. if Netbios(@NCB) <> Char(NRC_GOODRET) then
  26. begin
  27. Result := 'mac not found';
  28. Exit;
  29. end;
  30. Result :=
  31. IntToHex(Byte(Adapter.adapter_address[0]), 2) + '-' +
  32. IntToHex(Byte(Adapter.adapter_address[1]), 2) + '-' +
  33. IntToHex(Byte(Adapter.adapter_address[2]), 2) + '-' +
  34. IntToHex(Byte(Adapter.adapter_address[3]), 2) + '-' +
  35. IntToHex(Byte(Adapter.adapter_address[4]), 2) + '-' +
  36. IntToHex(Byte(Adapter.adapter_address[5]), 2);
  37. end;
  38.  
  39. function GetMACAddress: string;
  40. var
  41. AdapterList: TLanaEnum;
  42. NCB: TNCB;
  43. begin
  44. FillChar(NCB, SizeOf(NCB), 0);
  45. NCB.ncb_command := Char(NCBENUM);
  46. NCB.ncb_buffer := @AdapterList;
  47. NCB.ncb_length := SizeOf(AdapterList);
  48. Netbios(@NCB);
  49. if Byte(AdapterList.length) > 0 then
  50. Result := GetAdapterInfo(AdapterList.lana[0])
  51. else
  52. Result := 'mac not found';
  53. end;
  54.  
  55. // usage
  56. procedure TForm1.Button1Click(Sender: TObject);
  57. begin
  58. ShowMessage(GetMACAddress);
  59. end;
Comments on this Code Snippet
Message:
Previous Thread in Pascal and Delphi Forum Timeline: Disable font smoothing (Anti-aliased font)
Next Thread in Pascal and Delphi Forum Timeline: Check if Hibernation or Suspend is allowed





About Us | Contact Us | Advertise | Acceptable Use Policy
Build Custom RSS Feed


Follow us on Twitter


© 2010 DaniWeb® LLC