您的位置:程序门 -> delphi -> windows sdk/api



如何获取机器码?


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


如何获取机器码?[已结贴,结贴人:jbzj]
发表于:2007-02-27 17:27:52 楼主
如何获取电脑的机器码呀?

怎样获取网卡的物理地址?
发表于:2007-02-27 18:06:591楼 得分:6
function   lancardid:   string;//获取网卡物理地址
var   guid:   tguid;
        i:   integer;
begin
      result   :=   ' ';
      cocreateguid(guid);
      for   i   :=   low(guid.d4)+2   to   high(guid.d4)   do
      begin
            result   :=   result   +   inttohex(guid.d4[i],2);
      end;
end;
发表于:2007-02-28 14:59:482楼 得分:1
学习!
发表于:2007-03-02 10:03:263楼 得分:3
*             这个模块是用来获取cpu、硬盘序列号,cpu的*
*                                                                                             *
*       速率、显示器的刷新率网卡的mac地址等信息         *

unit   hardinfo;

interface

uses  
      windows,sysutils,nb30;

const
      id_bit   =   $200000;   //   eflags   id   bit

type  
      tcpuid   =   array[1..4]   of   longint;
      tvendor   =   array   [0..11]   of   char;
     
      function   iscpuid_available   :   boolean;   register;               //判断cpu序列号是否可用函数
      function   getcpuid:   tcpuid;   assembler;   register;               //获取cpu序列号函数
      function   getcpuvendor:   tvendor;   assembler;   register;     //获取cpu生产厂家函数
      function   getcpuinfo:   string;                                                     //cpu序列号(格式化成字符串)
      function   getcpuspeed:   double;                                                   //获取cpu速度函数
      function   getdisplayfrequency:   integer;                                 //获取显示器刷新率
      function   getideserialnumber:   pchar;                                       //获取ide硬盘序列号函数
      function   monthmaxday(year,month:integer):integer;           //获取某年某月的最大天数
      function   getadaptermac(ano:integer):string;

implementation  

    function   iscpuid_available   :   boolean;   register;
      asm
        pushfd   {direct   access   to   flags   no   possible,   only   via   stack}
        pop   eax   {flags   to   eax}
        mov   edx,eax   {save   current   flags}
        xor   eax,id_bit   {not   id   bit}
        push   eax   {onto   stack}
        popfd   {from   stack   to   flags,   with   not   id   bit}
        pushfd   {back   to   stack}
        pop   eax   {get   back   to   eax}
        xor   eax,edx   {check   if   id   bit   affected}
        jz   @exit   {no,   cpuid   not   availavle}
        mov   al,true   {result=true}
        @exit:
    end;


    function   getcpuid:   tcpuid;   assembler;   register;
        asm
        push         ebx                   {save   affected   register}
        push         edi
        mov           edi,eax           {@resukt}
        mov           eax,1
        dw             $a20f               {cpuid   command}
        stosd                                 {cpuid[1]}
        mov           eax,ebx
        stosd                               {cpuid[2]}
        mov           eax,ecx
        stosd                               {cpuid[3]}
        mov           eax,edx
        stosd                               {cpuid[4]}
        pop           edi                   {restore   registers}
        pop           ebx
        end;  

    function   getcpuvendor   :   tvendor;   assembler;   register;
    //获取cpu生产厂家函数
    //调用方法:edit.text:= 'current   cpu   vendor: '+getcpuvendor;
        asm
            push   ebx   {save   affected   register}
            push   edi
            mov   edi,eax   {@result   (tvendor)}
            mov   eax,0
            dw   $a20f   {cpuid   command}
            mov   eax,ebx
            xchg   ebx,ecx   {save   ecx   result}
            mov   ecx,4
            @1:
            stosb
            shr   eax,8
            loop   @1
            mov   eax,edx
            mov   ecx,4
            @2:
            stosb
            shr   eax,8
            loop   @2
            mov   eax,ebx
            mov   ecx,4
            @3:
            stosb
            shr   eax,8
            loop   @3
            pop   edi   {restore   registers}
            pop   ebx
        end;

    function   getcpuinfo:   string;
    var
        cpuid:   tcpuid;
                i:   integer;
                s:   tvendor;
    begin
        for   i:=low(cpuid)   to   high(cpuid)   do   cpuid[i]:=-1;
        if   iscpuid_available   then
            begin
                  cpuid:=   getcpuid;
                  s:=getcpuvendor;
                  result:=   inttohex(cpuid[1],   8)
                                  + '- '+   inttohex(cpuid[2],   8)
                                  + '- '+   inttohex(cpuid[3],   8)
                                  + '- '+   inttohex(cpuid[4],   8);
            end
        else   result:= 'cpuid   not   available ';
    end;

   
  function   getcpuspeed:   double;
  //获取cpu速率函数
  //调用方法:edit.text:= 'current   cpu   speed: '+floattostr(getcpuspeed)+ 'mhz ';
  const
    delaytime   =   500;   //   时间单位是毫秒
  var
    timerhi,   timerlo:   dword;
    priorityclass,   priority:   integer;
  begin
          priorityclass   :=   getpriorityclass(getcurrentprocess);
          priority   :=   getthreadpriority(getcurrentthread);
          setpriorityclass(getcurrentprocess,   realtime_priority_class);
          setthreadpriority(getcurrentthread,   thread_priority_time_critical);
          sleep(10);
          asm
                dw   310fh   //   rdtsc
                mov   timerlo,   eax
                mov   timerhi,   edx
          end;
          sleep(delaytime);
          asm
                dw   310fh   //   rdtsc  
                sub   eax,   timerlo  
                sbb   edx,   timerhi
                mov   timerlo,   eax
                mov   timerhi,   edx
          end;

          setthreadpriority(getcurrentthread,   priority);
          setpriorityclass(getcurrentprocess,   priorityclass);
          result   :=   timerlo   /   (1000.0   *   delaytime);
  end;

  function   getdisplayfrequency:   integer;
  //   这个函数返回的显示刷新率是以hz为单位的
  //调用方法:edit.text:= 'current   displayfrequency: '+inttostr(getdisplayfrequency)+ '   hz ';
  var
        devicemode:   tdevicemode;
  begin
      enumdisplaysettings(nil,   cardinal(-1),   devicemode);
      result   :=   devicemode.dmdisplayfrequency;
  end;

发表于:2007-03-02 10:05:474楼 得分:10

    function   getideserialnumber   :   pchar;
    //获取第一个ide硬盘的序列号
    //调用方法:edit.text:= 'harddriver   serialnumber: '+strpas(getideserialnumber);
    const   identify_buffer_size   =   512;
    type
      tideregs   =   packed   record
        bfeaturesreg           :   byte;   //   used   for   specifying   smart   "commands ".
        bsectorcountreg     :   byte;   //   ide   sector   count   register
        bsectornumberreg   :   byte;   //   ide   sector   number   register
        bcyllowreg               :   byte;   //   ide   low   order   cylinder   value
        bcylhighreg             :   byte;   //   ide   high   order   cylinder   value
        bdriveheadreg         :   byte;   //   ide   drive/head   register
        bcommandreg             :   byte;   //   actual   ide   command.
        breserved                 :   byte;   //   reserved   for   future   use.     must   be   zero.
    end;
    tsendcmdinparams   =   packed   record
        //   buffer   size   in   bytes
        cbuffersize     :   dword;
        //   structure   with   drive   register   values.
        irdriveregs     :   tideregs;
        //   physical   drive   number   to   send   command   to   (0,1,2,3).
        bdrivenumber   :   byte;
        breserved         :   array[0..2]   of   byte;
        dwreserved       :   array[0..3]   of   dword;
        bbuffer             :   array[0..0]   of   byte;     //   input   buffer.
    end;
    tidsector   =   packed   record
        wgenconfig                                   :   word;
        wnumcyls                                       :   word;
        wreserved                                     :   word;
        wnumheads                                     :   word;
        wbytespertrack                           :   word;
        wbytespersector                         :   word;
        wsectorspertrack                       :   word;
        wvendorunique                             :   array[0..2]   of   word;
        sserialnumber                             :   array[0..19]   of   char;
        wbuffertype                                 :   word;
        wbuffersize                                 :   word;
        weccsize                                       :   word;
        sfirmwarerev                               :   array[0..7]   of   char;
        smodelnumber                               :   array[0..39]   of   char;
        wmorevendorunique                     :   word;
        wdoublewordio                             :   word;
        wcapabilities                             :   word;
        wreserved1                                   :   word;
        wpiotiming                                   :   word;
        wdmatiming                                   :   word;
        wbs                                                 :   word;
        wnumcurrentcyls                         :   word;
        wnumcurrentheads                       :   word;
        wnumcurrentsectorspertrack   :   word;
        ulcurrentsectorcapacity         :   dword;
        wmultsectorstuff                       :   word;
        ultotaladdressablesectors     :   dword;
        wsingleworddma                           :   word;
        wmultiworddma                             :   word;
        breserved                                     :   array[0..127]   of   byte;
    end;
    pidsector   =   ^tidsector;
    tdriverstatus   =   packed   record
        //   驱动器返回的错误代码,无错则返回0
        bdrivererror   :   byte;
        //   ide出错寄存器的内容,只有当bdrivererror   为   smart_ide_error   时有效
        bidestatus       :   byte;
        breserved         :   array[0..1]   of   byte;
        dwreserved       :   array[0..1]   of   dword;
    end;
    tsendcmdoutparams   =   packed   record
        //   bbuffer的大小
        cbuffersize     :   dword;
        //   驱动器状态
        driverstatus   :   tdriverstatus;
        //   用于保存从驱动器读出的数据的缓冲区,实际长度由cbuffersize决定
        bbuffer             :   array[0..0]   of   byte;
    end;
    var   hdevice   :   thandle;
            cbbytesreturned   :   dword;
            scip   :   tsendcmdinparams;
            aidoutcmd   :   array   [0..(sizeof(tsendcmdoutparams)+identify_buffer_size-1)-1]   of   byte;
            idoutcmd     :   tsendcmdoutparams   absolute   aidoutcmd;
    procedure   changebyteorder(   var   data;   size   :   integer   );
    var   ptr   :   pchar;
            i   :   integer;
            c   :   char;
    begin
        ptr   :=   @data;
        for   i   :=   0   to   (size   shr   1)-1   do   begin
            c   :=   ptr^;
            ptr^   :=   (ptr+1)^;
            (ptr+1)^   :=   c;
            inc(ptr,2);
        end;
  end;
  begin
        result   :=   ' ';   //   如果出错则返回空串
        if   sysutils.win32platform=ver_platform_win32_nt   then   begin//   windows   nt,   windows   2000
                //   提示!   改变名称可适用于其它驱动器,如第二个驱动器:   '\\.\physicaldrive1\ '
                hdevice   :=   createfile(   '\\.\physicaldrive0 ',   generic_read   or   generic_write,
                    file_share_read   or   file_share_write,   nil,   open_existing,   0,   0   );
        end   else   //   version   windows   95   osr2,   windows   98
            hdevice   :=   createfile(   '\\.\smartvsd ',   0,   0,   nil,   create_new,   0,   0   );
            if   hdevice=invalid_handle_value   then   exit;
          try
            fillchar(scip,sizeof(tsendcmdinparams)-1,#0);
            fillchar(aidoutcmd,sizeof(aidoutcmd),#0);
            cbbytesreturned   :=   0;
            //   set   up   data   structures   for   identify   command.
            with   scip   do   begin
                cbuffersize     :=   identify_buffer_size;
    //             bdrivenumber   :=   0;
                with   irdriveregs   do   begin
                    bsectorcountreg     :=   1;
                    bsectornumberreg   :=   1;
    //             if   win32platform=ver_platform_win32_nt   then   bdriveheadreg   :=   $a0
    //             else   bdriveheadreg   :=   $a0   or   ((bdrivenum   and   1)   shl   4);
                    bdriveheadreg         :=   $a0;
                    bcommandreg             :=   $ec;
                end;
            end;
            if   not   deviceiocontrol(   hdevice,   $0007c088,   @scip,   sizeof(tsendcmdinparams)-1,
                @aidoutcmd,   sizeof(aidoutcmd),   cbbytesreturned,   nil   )   then   exit;
        finally
            closehandle(hdevice);
        end;
        with   pidsector(@idoutcmd.bbuffer)^   do   begin
            changebyteorder(   sserialnumber,   sizeof(sserialnumber)   );
            (pchar(@sserialnumber)+sizeof(sserialnumber))^   :=   #0;
            result   :=   pchar(@sserialnumber);
        end;
        //   更多关于   s.m.a.r.t.   ioctl   的信息可查看:
        //   http://www.microsoft.com/hwdev/download/respec/iocltapi.rtf
        //   msdn库中也有一些简单的例子
        //   windows   development   ->   win32   device   driver   kit   ->
        //   sample:   smartapp.exe   accesses   smart   stats   in   ide   drives
        //   还可以查看   http://www.mtgroup.ru/~alexk
        //   ideinfo.zip   -   一个简单的使用了s.m.a.r.t.   ioctl   api的delphi应用程序
        //   注意:
        //   winnt/win2000   -   你必须拥有对硬盘的读/写访问权限
        //   win98
        //   smartvsd.vxd   必须安装到   \windows\system\iosubsys
        //   (不要忘记在复制后重新启动系统)
    end;

    function   monthmaxday(year,month:integer)   :   integer   ;         //获取某年某月最大天数
    begin
        case   month   of
              1,3,5,7,8,10,12:     result:=31;     //如是1、3、5、7、8、10、12则最大为31天
              2:   if   (year   mod   4=0)   and   (year   mod   100 <> 0)   or   (year   mod   400=0)   then   result:=29
                    else   result:=28;         //如是闰年2月则29天,否则2月最大为28天
              else   result:=30;               //其它的4、6、9、11则最大天数为30天
        end;
    end;

   
    function   getadaptermac(ano:integer):string;
    //获取网卡的mac地址
    var
          ncb:tncb;
          adapter:tadapterstatus;
          lanaenum:tlanaenum;
          intidx:integer;   //
          crc:char;
          strtemp:string;
    begin
        result:= ' ';
        try
              zeromemory(@ncb,sizeof(ncb));
              ncb.ncb_command:=chr(ncbenum);
              netbios(@ncb);
              ncb.ncb_buffer:=@lanaenum;   //再处理enum命令
              ncb.ncb_length:=sizeof(lanaenum);
              crc:=netbios(@ncb);
              if   ord(crc) <> 0   then   exit;
              zeromemory(@ncb,sizeof(ncb));   //适配器清零
              ncb.ncb_command:=chr(ncbreset);
              ncb.ncb_lana_num:=lanaenum.lana[ano];
              crc:=netbios(@ncb);
              if   ord(crc) <> 0   then   exit;
              //得到适配器状态
              zeromemory(@ncb,sizeof(ncb));
              ncb.ncb_command:=chr(ncbastat);
              ncb.ncb_lana_num:=lanaenum.lana[ano];
              strpcopy(ncb.ncb_callname, '* ');
              ncb.ncb_buffer:=@adapter;
              ncb.ncb_length:=sizeof(adapter);
              netbios(@ncb);
              //将mac地址转换成字符串输出
              strtemp:= ' ';
              for   intidx:=0   to   5   do
              strtemp:=strtemp+inttohex(integer(adapter.adapter_address[intidx]),2);
              result:=strtemp;
        finally
        end;
end;

end.

发表于:2007-03-02 10:57:185楼 得分:0
谢谢


快速检索

最新资讯
热门点击