delphi获取mac地址有几种方法?
哪种在不连接网线的情况下能获取到?
注册表的mac地址是不是没有手动分配就不能取到?

解决方案 »

  1.   


    //Use Nb30function GetNetBIOSAddress : string;
    var   ncb   : TNCB;
       status   : TAdapterStatus;
       lanenum : TLanaEnum;
         procedure ResetAdapter (num : char);
         begin
           fillchar(ncb,sizeof(ncb),0);
           ncb.ncb_command:=char(NCBRESET);
           ncb.ncb_lana_num:=num;
           Netbios(@ncb);
         end;
    var
       i:integer;
       lanNum   : char;
       address : record
                  part1 : Longint;
                  part2 : Word;
                 end absolute status;
    begin
       Result:='';
       fillchar(ncb,sizeof(ncb),0);
         ncb.ncb_command:=char(NCBENUM);
         ncb.ncb_buffer:=@lanenum;
         ncb.ncb_length:=sizeof(lanenum);
       Netbios(@ncb);
       if lanenum.length=#0 then exit;
       lanNum:=lanenum.lana[0];
       ResetAdapter(lanNum);
       fillchar(ncb,sizeof(ncb),0);
         ncb.ncb_command:=char(NCBASTAT);
         ncb.ncb_lana_num:=lanNum;
         ncb.ncb_callname[0]:='*';
         ncb.ncb_buffer:=@status;
         ncb.ncb_length:=sizeof(status);
       Netbios(@ncb);
       ResetAdapter(lanNum);
       for i:=0 to 5 do
       begin
         result:=result+inttoHex(integer(Status.adapter_address[i]),2);
         if (i<5) then
         result:=result+'-';
       end;
    end;
      

  2.   


    uses 
    NB30; function GetAdapterInfo(Lana: Char): String; 
    var 
    Adapter: TAdapterStatus; 
    NCB: TNCB; 
    begin 
    FillChar(NCB, SizeOf(NCB), 0); 
    NCB.ncb_command := Char(NCBRESET); 
    NCB.ncb_lana_num := Lana; 
    if Netbios(@NCB) <> Char(NRC_GOODRET) then 
    begin 
    Result := 'mac not found'; 
    Exit; 
    end; FillChar(NCB, SizeOf(NCB), 0); 
    NCB.ncb_command := Char(NCBASTAT); 
    NCB.ncb_lana_num := Lana; 
    NCB.ncb_callname := '*'; FillChar(Adapter, SizeOf(Adapter), 0); 
    NCB.ncb_buffer := @Adapter; 
    NCB.ncb_length := SizeOf(Adapter); 
    if Netbios(@NCB) <> Char(NRC_GOODRET) then 
    begin 
    Result := 'mac not found'; 
    Exit; 
    end; 
    Result := 
    IntToHex(Byte(Adapter.adapter_address[0]), 2) + '-' + 
    IntToHex(Byte(Adapter.adapter_address[1]), 2) + '-' + 
    IntToHex(Byte(Adapter.adapter_address[2]), 2) + '-' + 
    IntToHex(Byte(Adapter.adapter_address[3]), 2) + '-' + 
    IntToHex(Byte(Adapter.adapter_address[4]), 2) + '-' + 
    IntToHex(Byte(Adapter.adapter_address[5]), 2); 
    end; function GetMACAddress: string; 
    var 
    AdapterList: TLanaEnum; 
    NCB: TNCB; 
    begin 
    FillChar(NCB, SizeOf(NCB), 0); 
    NCB.ncb_command := Char(NCBENUM); 
    NCB.ncb_buffer := @AdapterList; 
    NCB.ncb_length := SizeOf(AdapterList); 
    Netbios(@NCB); 
    if Byte(AdapterList.length) > 0 then 
    Result := GetAdapterInfo(AdapterList.lana[0]) 
    else 
    Result := 'mac not found'; 
    end; 
      

  3.   

    program GetMAC;uses
      Dialogs, SysUtils, nb30;function GetMACAddress(PCName: string) : string;
    type
      TASTAT = packed record
        adapt: nb30.TADAPTERSTATUS;
        NameBuff: array [0..30] of TNAMEBUFFER;
      end;
      var
      NCB: TNCB;
      Tmp: String;
      pASTAT: Pointer;
      AST: TASTAT;
    begin
      // The IBM NetBIOS 3.0 specifications defines four basic
      // NetBIOS environments under the NCBRESET command. Win32
      // follows the OS/2 Dynamic Link Routine (DLR) environment.
      // This means that the first NCB issued by an application
      // must be a NCBRESET, with the exception of NCBENUM.
      // The Windows NT implementation differs from the IBM
      // NetBIOS 3.0 specifications in the NCB_CALLNAME field.
      FillChar(NCB, SizeOf(NCB), 0);
      NCB.ncb_command := Chr(NCBRESET);
      NetBios(@NCB);  // To get the Media Access Control (MAC) address for an
      // ethernet adapter programmatically, use the Netbios()
      // NCBASTAT command and provide a "*" as the name in the
      // NCB.ncb_CallName field (in a 16-chr string).
      // NCB.ncb_callname = "* "
      FillChar(NCB, SizeOf(NCB), 0);
      FillChar(NCB.ncb_callname[0], 16, ' ');
      Move(PCName[1], NCB.ncb_callname[0], Length(PCName));
      NCB.ncb_command := Chr(NCBASTAT);  // For machines with multiple network adapters you need to
      // enumerate the LANA numbers and perform the NCBASTAT
      // command on each. Even when you have a single network
      // adapter, it is a good idea to enumerate valid LANA numbers
      // first and perform the NCBASTAT on one of the valid LANA
      // numbers. It is considered bad programming to hardcode the
      // LANA number to 0 (see the comments section below).
      NCB.ncb_lana_num := #0;
      NCB.ncb_length := SizeOf(AST);  GetMem(pASTAT, NCB.ncb_length);  if pASTAT=nil then
      begin
        result := 'memory allocation failed!';
        exit;
      end;
      NCB.ncb_buffer := pASTAT;
      NetBios(@NCB);  Move(NCB.ncb_buffer, AST, SizeOf(AST));  with AST.adapt do
        Tmp := 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])]);  FreeMem(pASTAT);
      Result := Tmp;
    end;begin
      ShowMessage(GetMACAddress('*'));
    end.
      

  4.   


    function MacAddress: string;               
    var
     Lib: Cardinal;
     Func : function(GUID: PGUID): Longint; stdcall; 
     GUID1, GUID2: TGUID;
    begin
     Result := '';
     Lib := LoadLibrary('rpcrt4.dll'); 
     if Lib <> 0 then
     begin
       if Win32Platform <>VER_PLATFORM_WIN32_NT then
         @Func := GetProcAddress(Lib, 'UuidCreate')
         else @Func := GetProcAddress(Lib, 'UuidCreateSequential');
       if Assigned(Func) then
       begin
         if (Func(@GUID1) = 0) and
           (Func(@GUID2) = 0) and
           (GUID1.D4[2] = GUID2.D4[2]) and
           (GUID1.D4[3] = GUID2.D4[3]) and
           (GUID1.D4[4] = GUID2.D4[4]) and
           (GUID1.D4[5] = GUID2.D4[5]) and
           (GUID1.D4[6] = GUID2.D4[6]) and
           (GUID1.D4[7] = GUID2.D4[7]) then
         begin
           Result :=
            IntToHex(GUID1.D4[2], 2) + '-' +
            IntToHex(GUID1.D4[3], 2) + '-' +
            IntToHex(GUID1.D4[4], 2) + '-' +
            IntToHex(GUID1.D4[5], 2) + '-' +
            IntToHex(GUID1.D4[6], 2) + '-' +
            IntToHex(GUID1.D4[7], 2);
         end;
       end;
       FreeLibrary(Lib);
     end;
    end;
      

  5.   

    上面的这几个代码,我在拔掉网线的情况下都不能取到mac地址,最后用getadaoterinfo的方法才能取到