Typhon获取硬盘和硬盘序列号

此代码为 CodeOcean 里的代码

unit PhysicalMediaInfomw;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, DAV_ProcessorInfoComponent, Forms, Controls,
  Graphics, Dialogs, StdCtrls, variants, windows, ActiveX, stdole2, comobj;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    ProcessorInfoComponent1: TProcessorInfoComponent;
    procedure Button1Click(Sender: TObject);
  private
    { private declarations }
  public
    function GetWin32_PhysicalMediaInfo:string;
  end;

var
  Form1: TForm1;

implementation

{$R *.frm}

function TForm1.GetWin32_PhysicalMediaInfo:string;
const
  WbemUser            ='';
  WbemPassword        ='';
  WbemComputer        ='localhost';
  wbemFlagForwardOnly = $00000020;
var
  FSWbemLocator : OLEVARIANT;
  FWMIService   : OLEVARIANT;
  FWbemObjectSet: OLEVARIANT;
  FWbemObject   : OLEVARIANT;
  oEnum         : IEnumvariant;
  lw            : LongWord;
begin;
  FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
  FWMIService   := FSWbemLocator.ConnectServer(WbemComputer, 'root\CIMV2', WbemUser, WbemPassword);
  FWbemObjectSet:= FWMIService.ExecQuery('SELECT * FROM Win32_PhysicalMedia','WQL',wbemFlagForwardOnly);
  oEnum         := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant;

  while oEnum.Next(1, FWbemObject, lw) = 0 do
  begin
    try

       if not VarIsNull(FWbemObject.Properties_.Item('SerialNumber').Value) then
          begin

          result:=FWbemObject.Properties_.Item('SerialNumber').Value;
          Memo1.Lines.Add(result);   //<===   for test Only
        end;

    except
    end;

  end;

end;


{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
begin
  Memo1.Clear;
  GetWin32_PhysicalMediaInfo;
end;

end.

发表回复

登录... 后才能评论