对于带有驱动器号的已安装驱动器,请调用 Win32 ShellApi 函数SHGetSpecialFolderLocation(0, CSIDL_DRIVES, Drives)。声明局部变量Drives: PItemIdList。这是在 delphi 中名为 ShellAPI 的单元中。希望 FreePascal 中存在类似的单元。
对于未安装的驱动器,您必须以某种方式通过GUID_DEVINTERFACE_DISK 的设备驱动程序类枚举设备驱动程序。 windows的SetupAPI应该可以帮到你。
您可以从 JEDI JCL 或 JEDI API 项目中获取 SetupAPI.pas。
procedure GetListFromSetupApi(aStrings: TStrings);
var
iDev: Integer;
RegDataType: Cardinal;
reqSize:DWORD;
prop:Cardinal;
pszData:PByte;
hinfo: HDEVINFO;
bResult: BOOL;
devinfo: SP_DEVINFO_DATA;
dwRequiredSize,dwPropertyRegDataType,dwAllocSz:Cardinal;
begin
LoadSetupApi;
if not Assigned(SetupDiGetClassDevs) then
Exit;
hinfo := SetupDiGetClassDevs(@GUID_DEVINTERFACE_DISK, nil, HWND(nil),
DIGCF_DEVICEINTERFACE or DIGCF_PRESENT or DIGCF_PROFILE);
devinfo.ClassGuid.D1 := 0;
devinfo.ClassGuid.D2 := 0;
devinfo.ClassGuid.D3 := 0;
devinfo.cbSize := SizeOf(SP_DEVINFO_DATA);
iDev := 0;
while SetupDiEnumDeviceInfo(hinfo, iDev, devinfo) do
begin
dwRequiredSize := 0;
prop := SPDRP_PHYSICAL_DEVICE_OBJECT_NAME;
// results on my computer:
// \Device\Ide\IAAStorageDevice-1
// \Device\Ide\IAAStorageDevice-2
// \Device\0000008a (this one is a usb disk, use SPDRP_ENUMERATOR_NAME, returns USBSTOR)
// prop := SPDRP_ENUMERATOR_NAME; // results: IDE, USBSTOR, or other bus type.
// prop := SPDRP_LOCATION_INFORMATION; // a number like 1,2,3.
{ SPDRP_DRIVER - driver guid }
{ Get Size of property }
SetupDiGetDeviceRegistryProperty
(hinfo,
devinfo,
prop,
dwPropertyRegDataType,
nil,
0,
dwRequiredSize); { dwRequiredSize should be around 88 after this point, in unicode delphi }
if dwRequiredSize>0 then begin
dwAllocSz := dwRequiredSize+4;
pszData := AllocMem(dwAllocSz);
bResult := SetupDiGetDeviceRegistryProperty
(hinfo,
devinfo,
prop,
dwPropertyRegDataType,
pszData,
dwAllocSz,
dwRequiredSize);
aStrings.Add(IntToStr(aStrings.Count)+': '+PChar(pszData));
FreeMem(pszData);
end;
inc(iDev);
end;
SetupDiDestroyDeviceInfoList(hinfo);
end;
包含上述代码和相应 JEDI API 单元的完整 DELPHI 示例是 here。你可以很容易地将它改编为 free pascal 和 lazarus。