如何使用Free Pascal调用物理连接的硬盘列表,否则,Delphi? [英] How to call a list of the physically attached hard disks using Free Pascal, or, failing that, Delphi?

查看:178
本文介绍了如何使用Free Pascal调用物理连接的硬盘列表,否则,Delphi?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了 WMI类,检查此示例代码

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

// Win32_DiskDrive类代表运行Win32操作系统的计算机所看到的物理磁盘驱动器。 Win32物理磁盘驱动器的任何接口都是此类的后代(或成员)。通过该对象看到的磁盘驱动器的功能对应于驱动器的逻辑和管理特性。在某些情况下,这可能不会反映设备的实际物理特性。基于另一个逻辑设备的任何对象都不会是此类的成员。
//示例:IDE固定磁盘。

程序GetWin32_DiskDriveInfo;
const
WbemUser ='';
WbemPassword ='';
WbemComputer ='localhost';
wbemFlagForwardOnly = $ 00000020;
var
FSWbemLocator:OLEVariant;
FWMIService:OLEVariant;
FWbemObjectSet:OLEVariant;
FWbemObject:Variant;
oEnum:IEnumvariant;
sValue:string;
开始;
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')。
Writeln(Format('Caption%s',[sValue])); // String
sValue:= FWbemObject.Properties_.Item('DeviceID')。
Writeln(格式('DeviceID%s',[sValue])); // String
sValue:= FWbemObject.Properties_.Item('Model')。
Writeln(Format('Model%s',[sValue])); // String
sValue:= FWbemObject.Properties_.Item('Partitions')。
Writeln(Format('Partitions%s',[sValue])); // Uint32
sValue:= FWbemObject.Properties_.Item('PNPDeviceID')。
Writeln(Format('PNPDeviceID%s',[sValue])); // String
sValue:= FormatFloat('#,',FWbemObject.Properties_.Item('Size' (1024 * 1024));
Writeln(Format('Size%s mb',[sValue])); // Uint64

Writeln;
FWbemObject:=未分配;
结束
结束

begin
try
GetWin32_DiskDriveInfo;
除了
在E:EOleException do
Writeln(Format('EOleException%s%x',[E.Message,E.ErrorCode]));
在E:异常do
Writeln(E.Classname,':',E.Message);
结束
Writeln('按Enter键退出);
Readln;
结束。

运行此代码后,您将得到一个这样的输出




Further to this question and this one that I asked more recently but without the correct specifics...and lastly this one that I asked at the Free Pascal forum specifically....

Can anyone provide me with guidance, examples or a link to something somewhere that explains how to call a list of the physically attached hard disks using Free Pascal, or, failing that, Delphi, regardless of whether the disks have been mounted by the operating system or not? An example is shown in the screenshot of what I am trying to achive (what is shown in this screenshot is by another software product). So pulling a list of logical volumes (C:\, E:\ etc) is not what I am trying to do. And if the disk has a filesystem that the operating system cannot mount, I still want to see the physical disk listed.

I stress that C\C++\C Sharp examples are plentifull but not what I am after. I need primarily Free Pascal example, or, failing that, Delphi.

解决方案

Try the Win32_DiskDrive WMI class, check this sample code

{$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.    

After of running this code you will get a output like this

这篇关于如何使用Free Pascal调用物理连接的硬盘列表,否则,Delphi?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆