delphi - 如何使用 Free Pascal 调用物理连接的硬盘列表,或者如果失败,则使用 Delphi?

标签 delphi freepascal lazarus

进一步this questionthis one我最近问过,但没有正确的细节......最后 this one我在 Free Pascal 论坛上专门问过......

任何人都可以为我提供指导、示例或链接,解释如何使用 Free Pascal 调用物理连接的硬盘列表,或者如果失败,则使用 Delphi,无论磁盘是否已由操作系统与否?我试图实现的屏幕截图中显示了一个示例(此屏幕截图中显示的是另一个软件产品)。因此,提取逻辑卷列表(C:\、E:\等)并不是我想要做的。如果磁盘有操作系统无法挂载的文件系统,我仍然想查看列出的物理磁盘。

我强调 C\C++\C Sharp 的例子很多,但不是我所追求的。我主要需要 Free Pascal 示例,或者,如果做不到这一点,则需要 Delphi。

enter image description here

最佳答案

尝试Win32_DiskDrive WMI 类,查看示例代码

{$mode objfpc}{$H+}
uses
  SysUtils,ActiveX,ComObj,Variants;
{$R *.res}

// The Win32_DiskDrive class represents a physical disk drive as seen by a computer running the Win32 operating system. Any interface to a Win32 physical disk drive is a descendent (or member) of this class. The features of the disk drive seen through this object correspond to the logical and management characteristics of the drive. In some cases, this may not reflect the actual physical characteristics of the device. Any object based on another logical device would not be a member of this class.
// Example: IDE Fixed Disk.

procedure  GetWin32_DiskDriveInfo;
const
  WbemUser            ='';
  WbemPassword        ='';
  WbemComputer        ='localhost';
  wbemFlagForwardOnly = $00000020;
var
  FSWbemLocator : OLEVariant;
  FWMIService   : OLEVariant;
  FWbemObjectSet: OLEVariant;
  FWbemObject   : Variant;
  oEnum         : IEnumvariant;
  sValue        : string;
begin;
  FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
  FWMIService   := FSWbemLocator.ConnectServer(WbemComputer, 'root\CIMV2', WbemUser, WbemPassword);
  FWbemObjectSet:= FWMIService.ExecQuery('SELECT * FROM Win32_DiskDrive','WQL',wbemFlagForwardOnly);
  oEnum         := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant;
  while oEnum.Next(1, FWbemObject, nil) = 0 do
  begin
    sValue:= FWbemObject.Properties_.Item('Caption').Value;
    Writeln(Format('Caption        %s',[sValue]));// String
    sValue:= FWbemObject.Properties_.Item('DeviceID').Value;
    Writeln(Format('DeviceID       %s',[sValue]));// String
    sValue:= FWbemObject.Properties_.Item('Model').Value;
    Writeln(Format('Model          %s',[sValue]));// String
    sValue:= FWbemObject.Properties_.Item('Partitions').Value;
    Writeln(Format('Partitions     %s',[sValue]));// Uint32
    sValue:= FWbemObject.Properties_.Item('PNPDeviceID').Value;
    Writeln(Format('PNPDeviceID    %s',[sValue]));// String
    sValue:= FormatFloat('#,', FWbemObject.Properties_.Item('Size').Value / (1024*1024));
    Writeln(Format('Size           %s mb',[sValue]));// Uint64

    Writeln;
    FWbemObject:= Unassigned;
  end;
end;

begin
  try
    GetWin32_DiskDriveInfo;
  except
    on E:EOleException do
        Writeln(Format('EOleException %s %x', [E.Message,E.ErrorCode]));
    on E:Exception do
        Writeln(E.Classname, ':', E.Message);
  end;
  Writeln('Press Enter to exit');
  Readln;
end.    

运行此代码后,您将得到如下输出

enter image description here

关于delphi - 如何使用 Free Pascal 调用物理连接的硬盘列表,或者如果失败,则使用 Delphi?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8519658/

相关文章:

mysql - 在 Delphi 中为 MySQL 创建 UDF

delphi - TForm.TipMode 属性有什么用?

linux - Indy 10 以 1024 个 block 发送数据。如何增加 block 大小?

XML 解析,TXMLDocument

delphi - SendMessage 问题(Delphi)

visual-studio-code - 如何在Visual Studio Code SERVER上运行/编译Pascal? (在Ubuntu上使用FPC吗?)

pascal - 有没有办法确定对对象过程的引用是否对应于 Pascal 中的对象实例?

delphi - 拉撒路。相当于 Unicode 符号的 Chr()

mysql - Delphi代码不会保存到Mysql数据库

google-maps - 使用谷歌地图和拉撒路的纬度经度选择器