您的位置:程序门 -> delphi -> 网络通信/分布式开发



求一个正确的,可以运行的 delphi 读取 mac地址 例子, 多谢


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


求一个正确的,可以运行的 delphi 读取 mac地址 例子, 多谢
发表于:2007-01-19 14:36:56 楼主
求一个正确的,可以运行的   delphi   读取   mac地址   例子,   多谢

在下愚钝,   网上找了很多代码都未成正确运行。
发表于:2007-01-19 17:28:031楼 得分:0
const
    snetbioserror   =   'netbios错误%d ';

type
    tmacaddress   =   packed   array[0..5]   of   byte;
    enetbioserror   =   class(exception);
    tastat   =   record   adapt:   tadapterstatus;
        namebuff:   array[0..30]   of   tnamebuffer;
    end;

function   getmacaddress(adapternum:   integer):   tmacaddress;
var
    ncb:   tncb;
    uretcode:   char;
    j:   integer;
    adapter:   tastat;
begin
    fillchar(ncb,   sizeof(ncb),   0);
    with   ncb   do
    begin
        ncb_command   :=   char(ncbreset);
        ncb_lana_num   :=   char(adapternum);
    end;
    uretcode   :=   netbios(@ncb);
    if   uretcode   <>   #0   then   raise   exception.createfmt(snetbioserror,   [ord(uretcode)]);
    fillchar(ncb,   sizeof(ncb),   0);
    with   ncb   do
    begin
        ncb_command   :=   char(ncbastat);
        ncb_lana_num   :=   char(adapternum);
        strcopy(ncb_callname,   '* ');
        ncb_buffer   :=   @adapter;
        ncb_length   :=   sizeof(adapter);
    end;
    uretcode   :=   netbios(@ncb);
    if   uretcode   <>   #0   then   raise   exception.createfmt(snetbioserror,   [ord(uretcode)]);
    for   j   :=   0   to   5   do
        result[j]   :=   ord(adapter.adapt.adapter_address[j]);
end;


方法2:

uses   nb30;

function   nbgetadapteraddress(a:   integer):   string;
var
    ncb:   tncb;                                                                                                 //   netbios   control   block   //netbios控制块
    adapter:   tadapterstatus;                                                                     //   netbios   adapter   status//取网卡状态
    lanaenum:   tlanaenum;                                                                             //   netbios   lana
    intidx:   integer;                                                                                     //   temporary   work   value//临时变量
    crc:   char;                                                                                                 //   netbios   return   code//netbios返回值
    strtemp:   string;                                                                                     //   temporary   string//临时变量
begin
    result   :=   ' ';

    try
        zeromemory(@ncb,   sizeof(ncb));                                                     //   zero   control   blocl

        ncb.ncb_command   :=   chr(ncbenum);                                                 //   issue   enum   command
        crc   :=   netbios(@ncb);

        ncb.ncb_buffer   :=   @lanaenum;                                                         //   reissue   enum   command
        ncb.ncb_length   :=   sizeof(lanaenum);
        crc   :=   netbios(@ncb);
        if   ord(crc)   <>   0   then
            exit;

        zeromemory(@ncb,   sizeof(ncb));                                                     //   reset   adapter
        ncb.ncb_command   :=   chr(ncbreset);
        ncb.ncb_lana_num   :=   lanaenum.lana[a];
        crc   :=   netbios(@ncb);
        if   ord(crc)   <>   0   then
            exit;


        zeromemory(@ncb,   sizeof(ncb));                                                     //   get   adapter   address
        ncb.ncb_command   :=   chr(ncbastat);
        ncb.ncb_lana_num   :=   lanaenum.lana[a];
        strpcopy(ncb.ncb_callname,   '* ');
        ncb.ncb_buffer   :=   @adapter;
        ncb.ncb_length   :=   sizeof(adapter);
        crc   :=   netbios(@ncb);

        strtemp   :=   ' ';                                                                                     //   convert   it   to   string
        for   intidx   :=   0   to   5   do
            strtemp   :=   strtemp   +   inttohex(integer(adapter.adapter_address[intidx]),   2);
        result   :=   strtemp;
    finally
    end;
end;
发表于:2007-01-22 14:49:102楼 得分:0
//netbios函数获取mac地址有的系统里会取不到。

const
    max_adapter_name_length   =   256;
    max_adapter_description_length   =   128;
    max_adapter_address_length   =   8;

type
    tip_address_string   =   record
          ipstring:   array   [0..15]   of   char;
    end;
    pip_address_string   =   ^tip_address_string;
    tip_mask_string   =   tip_address_string;
    pip_mask_string   =   ^tip_mask_string;

    pip_addr_string   =   ^tip_addr_string;
    tip_addr_string   =   record
        next:   pip_addr_string;
        ipaddress:   tip_address_string;     //ip地址字符串
        ipmask:   tip_mask_string;     //子网掩码字符串
        context:   dword;   //netword   table   entry
    end;
    pip_adapter_info   =   ^tip_adapter_info;
    tip_adapter_info   =   packed   record
          next:   pip_adapter_info;
          comboindex:   dword;
          adaptername:   array   [0..max_adapter_name_length   +   4-1]   of   char;
          description:   array   [0..max_adapter_description_length   +   4-1]   of   char;
          addresslength:   uint;
          address:   array   [0..max_adapter_address_length-1]   of   byte;
          index:   dword;
          dwtype:   uint;
          dhcpenabled:   uint;
          currentipaddress:   pip_addr_string;
          ipaddresslist:   tip_addr_string;
          gatewaylist:   tip_addr_string;
          dhcpserver:   tip_addr_string   ;
          havewins:   bool;
          primarywinsserver:   tip_addr_string;
          secondarywinsserver:   tip_addr_string;
    end;

function   getadaptersinfo(padapterinfo:   pip_adapter_info;
                poutbuflen:   pdword):   dword;   stdcall;
                external   'iphlpapi.dll '   name   'getadaptersinfo ';

implementation


procedure   showmacs();
var
    pbuf:   pip_adapter_info;
    buflen:   dword;
    i:   integer;
    mac:   string;
begin
    buflen   :=   0;
    if   getadaptersinfo(pbuf,   @buflen)   =   error_buffer_overflow   then
    begin
        pbuf   :=   allocmem(buflen);
        if   getadaptersinfo(pbuf,   @buflen)   =   error_success   then
        while   pbuf   <>   nil   do
        begin
            mac   :=   ' ';
            for   i   :=   0   to   5   do
                mac   :=   mac   +   inttohex(pbuf.address[i],   2);
            showmessage(mac);
            pbuf   :=   pbuf.next;
        end;
        freemem(pbuf);
    end;
end;
发表于:2007-01-22 15:37:333楼 得分:0
good!
发表于:2007-01-22 16:22:324楼 得分:0
好东东
发表于:2007-01-22 20:45:035楼 得分:0
mark
发表于:2007-01-27 11:59:076楼 得分:0
mark
发表于:2007-01-27 13:52:437楼 得分:0
我以前也问过这个问题

可是我出了100分,(100分也不多!)
最近d版好象越来越便宜,放眼望去,都是20分,30分的.
更要鄙视的是,就这20分还tmd不结贴.
发表于:2007-01-27 15:14:568楼 得分:0
tmd   还是三个裤衩的,真的怀疑这些裤衩是怎么来的。

lj


快速检索

最新资讯
热门点击