我有这样一段代码,是取得本机网卡物理地址,但是每次取得都不一样,请帮我看看.
或者哪位有更好的办法,望告知.代码如下:
var
  GUID:TGUID;
  WCLID:array[0..127]of widechar;
  Start:Integer;
begin
  CoCreateGUID(GUID);
  StringFromGuid2(GUID,WCLID,Sizeof(WCLID)div 2);
  Start:=length(widechartostring(WCLID))-12;
  NetCardMacCode:=Copy(widechartostring(WCLID),Start,12);
  //ConvertValidateNetCard_MAC_Addr_RegCode(NetCode,pByteNumber,6);
end;还有一个办法也不行代码如下
var
  NCB:TNCB;
  ADAPTER:TADAPTERSTATUS;
  LANAENUM:TLANAENUM;
  IntIdx:integer;
  re:char;
  buf:string;
begin
  try
    //Reset adapter
    ZeroMemory(@NCB,Sizeof(NCB));
    NCB.ncb_command:=chr(NCBRESET);
    NCB.ncb_lana_num :=LANAENUM.lana[0];
    re:=NetBios(@NCB);
    if ord(re)<>0 then
      exit;
    //Get adapter address
    ZeroMemory(@NCB,Sizeof(NCB));
    NCB.ncb_command:=chr(NCBASTAT);
    NCB.ncb_lana_num :=LANAENUM.lana[0];
    StrpCopy(NCB.ncb_callname,'*');
    NCB.ncb_buffer :=@ADAPTER;
    NCB.ncb_length :=Sizeof(ADAPTER);
    re:=NETBios(@NCB);
    if Ord(re)<>0 then
      exit;
    buf:='';
    for IntIdx:=0 to 5 do
      buf:=buf+InttoHex(Integer(ADAPTER.adapter_address[intIdx]),2);
    //NetCardMacCodeByOtherWays:= copy(buf,0,length(buf)-1);
    NetCardMacCodeByOtherWays:=buf;
  finally
  end;
end;高手们,给个建议吧

解决方案 »

  1.   

    参考:
    unit MacAdress;interface
    uses SysUtils,NB30, Classes, Controls, StdCtrls,windows,messages,Dialogs;type
     TMACAdress  = array[0..5] of byte;
     ENCBError   = class(Exception);
    function GetMACAdress(AdapterNum:byte):TMACAdress;
    function MACAdressToStr(aMACAdr:TMACAdress):string;
    function IsMACAdress(aMACAdr:TMACAdress):boolean;var
     MacAdressFormat:string;
    implementationResourceString
     SNCBError='NetBios-Fehler %d';type TAStat=packed record
                 Adapter_Status:TAdapterStatus;
                 Name_Buffer:array[0..49] of TNameBuffer;
               end;function MACAdressToStr(aMACAdr:TMACAdress):string;
    begin
     Result:=Format( MacAdressFormat,
                     [aMACAdr[0],
                     aMACAdr[1],
                     aMACAdr[2],
                     aMACAdr[3],
                     aMACAdr[4],
                     aMACAdr[5]]
                   );
    end;procedure CheckResult(aReturnCode:char);
     begin
       if Byte(aReturnCode)<>NRC_GOODRET then
         raise ENCBError.Create(Format(SNCBError,[byte(aReturnCode)]));
     end;function IsMACAdress(aMACAdr:TMACAdress):boolean;
     var i:integer;
     begin
       for i:=0 to 5 do if aMACAdr[i]<>0 then begin Result:=True; exit end;
       Result:=False;
     end;function GetMACAdress(AdapterNum:byte):TMACAdress;
     var Adapter:TAStat;
         NCB:TNCB;
     begin
       FillChar(NCB,SizeOf(NCB),0);
       NCB.ncb_command:=char(NCBRESET);
       NCB.ncb_lana_num:=char(AdapterNum);
       CheckResult(NetBIOS(@NCB));   FillChar(NCB,SizeOf(NCB),0);
       NCB.ncb_command:=char(NCBASTAT);
       NCB.ncb_lana_num:=char(AdapterNum);   StrCopy(NCB.ncb_callname,'*               ');
       NCB.ncb_buffer:=@Adapter;
       NCB.ncb_length:=sizeof(Adapter);
       CheckResult(NetBIOS(@NCB));   Move(Adapter.Adapter_Status.adapter_address[0],Result[0],SizeOf(Result));
     end;
    initialization
     MacAdressFormat:='%2.2x-%2.2x-%2.2x-%2.2x-%2.2x-%2.2x';
    end.引用:
    uses MacAdress
    ...............
    begin
    showMessage(MACAdressToStr(GetMACAdress(0)));
    end;
      

  2.   

    在iphlpapi.dll里面有一个函数:GetAdaptersInfo() 
    好像是干这个用的。说明如下: 
    GetAdaptersInfo 
    The GetAdaptersInfo function retrieves adapter information for the local computer. DWORD GetAdaptersInfo( 
      PIP_ADAPTER_INFO pAdapterInfo,    // buffer to receive data 
      PULONG pOutBufLen                 // size of data returned 
    ); 
    Parameters 
    pAdapterInfo  
    [out] Pointer to a buffer that, , receives a linked list of IP_ADAPTER_INFO structures.  
    pOutBufLen  
    [in] Pointer to a ULONG variable that specifies the size of the buffer pointed to by the pAdapterInfo parameter. If this size is insufficient to hold the adapter information, GetAdaptersInfo fills in this variable with the required size, and returns an error code of ERROR_BUFFER_OVERFLOW.  
    Return Values 
    If the function succeeds, the return value is ERROR_SUCCESS. If the function fails, the return value is one of the following error codes. Value Meaning  
    ERROR_BUFFER_OVERFLOW The buffer size indicated by the pOutBufLen parameter is too small to hold the adapter information. The pOutBufLen parameter points to the required size.  
    ERROR_INVALID_PARAMETER The pOutBufLen parameter is NULL, or the calling process does not have read/write access to the memory pointed to by pOutBufLen, or the calling process does not have write access to the memory pointed to by the pAdapterInfo parameter.  
    ERROR_NO_DATA No adapter information exists for the local computer.  
    ERROR_NOT_SUPPORTED GetAdaptersInfo is not supported by the operating system running on the local computer.  
    Other If the function fails, use FormatMessage to obtain the message string for the returned error.  
    Requirements  
      Windows NT/2000: Requires Windows 2000. 
      Windows 95/98: Requires Windows 98. 
      Header: Declared in Iphlpapi.h.//没有 
      Library: Use Iphlpapi.lib.//没有 IP_ADAPTER_INFO 
    The IP_ADAPTER_INFO structure contains information about a particular network adapter on the local computer. typedef struct _IP_ADAPTER_INFO { 
      struct _IP_ADAPTER_INFO* Next; 
      DWORD ComboIndex; 
      char AdapterName[MAX_ADAPTER_NAME_LENGTH + 4]; 
      char Description[MAX_ADAPTER_DESCRIPTION_LENGTH + 4]; 
      UINT AddressLength; 
      BYTE Address[MAX_ADAPTER_ADDRESS_LENGTH]; 
      DWORD Index; 
      UINT Type; 
      UINT DhcpEnabled; 
      PIP_ADDR_STRING CurrentIpAddress; 
      IP_ADDR_STRING IpAddressList; 
      IP_ADDR_STRING GatewayList; 
      IP_ADDR_STRING DhcpServer; 
      BOOL HaveWins; 
      IP_ADDR_STRING PrimaryWinsServer; 
      IP_ADDR_STRING SecondaryWinsServer; 
      time_t LeaseObtained; 
      time_t LeaseExpires;  
    } IP_ADAPTER_INFO, *PIP_ADAPTER_INFO; 
    Members 
    Next  
    Pointer to the next adapter in the linked list of adapters.  
    ComboIndex  
    This member is unused.  
    AdapterName[MAX_ADAPTER_NAME_LENGTH + 4]  
    Specifies the name of the adapter.  
    Description[MAX_ADAPTER_DESCRIPTION_LENGTH + 4]  
    Specifies a description for the adapter.  
    AddressLength  
    Specifies the length of the hardware address for the adapter.  
    Address[MAX_ADAPTER_ADDRESS_LENGTH]  
    Specifies the hardware address for the adapter. //这个是不是你想要的? 
    Index  
    Specifies the adapter index.  
    Type  
    Specifies the adapter type.  
    DhcpEnabled  
    Specifies whether dynamic host configuration protocol (DHCP) is enabled for this adapter.  
    CurrentIpAddress  
    Specifies the current IP address for this adapter.  
    IpAddressList  
    Specifies the list of IP addresses associated with this adapter.  
    GatewayList  
    Specifies the IP address of the default gateway for this adapter.  
    DhcpServer  
    Specifies the IP address of the DHCP server for this adapter.  
    HaveWins  
    Specifies whether this adapter uses Windows Internet Name Service (WINS).  
    PrimaryWinsServer  
    Specifies the IP address of the primary WINS server.  
    SecondaryWinsServer  
    Specifies the IP address of the secondary WINS server.  
    LeaseObtained  
    Specifies the time when the current DHCP lease was obtained.  
    LeaseExpires  
    Specifies the time when the current DHCP lease will expire.  
    Requirements  
      Windows NT/2000: Requires Windows 2000. 
      Windows 95/98: Requires Windows 98. 
      Header: Declared in Iptypes.h.