Parsi Coders
سورس کد بدست اوردن مک ادرس - نسخه قابل چاپ

+- Parsi Coders (http://parsicoders.com)
+-- انجمن: Software Development Programming (http://parsicoders.com/forumdisplay.php?fid=37)
+--- انجمن: Pascal/Delphi (http://parsicoders.com/forumdisplay.php?fid=45)
+---- انجمن: Delphi (http://parsicoders.com/forumdisplay.php?fid=69)
+---- موضوع: سورس کد بدست اوردن مک ادرس (/showthread.php?tid=3187)



سورس کد بدست اوردن مک ادرس - Amin_Mansouri - 05-22-2013

با سورس زیر در دلفی میتونید مک ادرس کارت شبکه رو بدست بیارید.
سورس زیر نیاز به استفاده از یونیتهای SysUtils, Windows, Nb30. میباشد.
English:
English:GetMacAddressReturns the MAC address of the first ethernet adapter on the computer or the empty string if there is no adapter or if the function fails.Source Code Get Mac Address Delphi 
Source Code Get Mac Address Delphi 
کد:
function GetMacAddress: string;
type
  // This type is defined in MSDN sample code, but tests have found this is
  // not needed (on XP Pro) and Adapter can be of type TAdapterStatus. This
  // method use the type in case other OSs require it
  TAStat = packed record
    Adapt: Nb30.TAdapterStatus;
    NameBuff: array[0..29] of Nb30.TNameBuffer;
  end;
var
  Adapter: TAStat;              // info about a network adapter
  AdapterList: Nb30.TLanaEnum;  // numbers for current LAN adapters
  Ncb: Nb30.TNCB;               // network control block descriptor
  I: Integer;                   // loops thru all adapters in list
  // ---------------------------------------------------------------------------
  function NetBiosSucceeded(const RetCode: AnsiChar): Boolean;
  begin
    // Check RetCode is good NetBios function return value
    Result := Windows.UCHAR(RetCode) = Nb30.NRC_GOODRET;
  end;
  // ---------------------------------------------------------------------------
begin
  // Assume not adapter
  Result := '';
  // Get list of adapters
  FillChar(Ncb, SizeOf(Ncb), 0);
  Ncb.ncb_command := AnsiChar(Nb30.NCBENUM);
  Ncb.ncb_buffer := PAnsiChar(@AdapterList);
  Ncb.ncb_length := SizeOf(AdapterList);
  if not NetBiosSucceeded(Nb30.Netbios(@Ncb)) then
    Exit;
  // Get status of each adapter, exiting when first valid one reached
  // MSDN cautions us not to assume lana[0] is valid
  for I := 0 to Pred(Integer(AdapterList.length)) do
  begin
    // reset the adapter
    FillChar(Ncb, SizeOf(Ncb), 0);
    Ncb.ncb_command := AnsiChar(Nb30.NCBRESET);
    Ncb.ncb_lana_num := AdapterList.lana[I];
    if not NetBiosSucceeded(Nb30.Netbios(@Ncb)) then
      Exit;
    // get status of adapter
    FillChar(Ncb, SizeOf(Ncb), 0);
    Ncb.ncb_command := AnsiChar(Nb30.NCBASTAT);
    Ncb.ncb_lana_num := AdapterList.lana[I];
    Ncb.ncb_callname := '*               ';
    Ncb.ncb_buffer := PAnsiChar(@Adapter);
    Ncb.ncb_length := SizeOf(Adapter);
    if NetBiosSucceeded(Nb30.Netbios(@Ncb)) then
    begin
      // we have a MAC address: return it
      with Adapter.Adapt do
        Result := SysUtils.Format(
          '%.2x-%.2x-%.2x-%.2x-%.2x-%.2x',
          [
            Ord(adapter_address[0]),
            Ord(adapter_address[1]),
            Ord(adapter_address[2]),
            Ord(adapter_address[3]),
            Ord(adapter_address[4]),
            Ord(adapter_address[5])
          ]
        );
      Exit;
    end;
  end;
end;
 
[i] [/i]