IEDriveInfo.pas 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  1. unit IEDriveInfo;
  2. {==================================================================
  3. Component TDriveInfo / Version 2.6 / January 2000
  4. ====================================================
  5. Description:
  6. ============
  7. Central drive management class. Provides information about all
  8. installed drives, for example wether drive is valid, disk is inserted
  9. displayname or volume size.
  10. Author:
  11. =======
  12. (c) Ingo Eckel 1999
  13. Sodener Weg 38
  14. 65812 Bad Soden
  15. Germany
  16. For detailed documentation and history see the documentation in TDriveInfo.htm.
  17. {==================================================================}
  18. {Required compiler options:}
  19. {$A+,B-,X+,H+,P+}
  20. {$WARN SYMBOL_PLATFORM OFF}
  21. interface
  22. uses
  23. Windows, Registry, SysUtils, Classes, ComCtrls, ShellApi, ShlObj, CommCtrl, Forms,
  24. BaseUtils, System.Generics.Collections, Vcl.Graphics;
  25. const
  26. {Flags used by TDriveInfo.ReadDriveStatus and TDriveView.RefreshRootNodes:}
  27. dsValid = 0; {checks only whether drive is still valid}
  28. dsImageIndex = 1; {Fetch imageindex, if not allready fetched}
  29. dsSize = 2; {Fetch disk size and serialnumber}
  30. dsDisplayName = 4; {Fetch drives displayname}
  31. dsAll = dsImageIndex or dsSize or dsDisplayName;
  32. FirstDrive = 'A';
  33. SystemDrive = 'C';
  34. LastDrive = 'Z';
  35. FirstSpecialFolder = CSIDL_DESKTOP;
  36. LastSpecialFolder = CSIDL_PRINTHOOD;
  37. type
  38. TDriveInfoRec = class
  39. PIDL : PItemIDList; {Fully qualyfied PIDL}
  40. Init : Boolean; {Drivestatus was updated once}
  41. Valid : Boolean; {Drivestatus is valid}
  42. ValidButHiddenByDrivePolicy: Boolean;
  43. DriveReady : Boolean; {Drive is ready}
  44. DriveType : Integer; {DRIVE_REMOVABLE, DRIVE_FIXED, DRIVE_CDROM, DRIVE_RAMDISK, DRIVE_REMOTE}
  45. DisplayName : string; {Windows displayname}
  46. PrettyName : string; {Prettyfied displayname}
  47. DriveSerial : DWORD; {Serial number of the drive}
  48. Size : Int64; {Drivesize}
  49. ImageIndex : Integer; {Drive imageIndex}
  50. end;
  51. TRealDrive = char;
  52. TSpecialFolder = FirstSpecialFolder..LastSpecialFolder;
  53. PSpecialFolderRec = ^TSpecialFolderRec;
  54. TSpecialFolderRec = record
  55. Valid: Boolean;
  56. Location: string;
  57. DisplayName: string;
  58. ImageIndex: Integer;
  59. PIDL: PItemIDList;
  60. end;
  61. TDriveInfo = class(TObject)
  62. private
  63. FData: TObjectDictionary<string, TDriveInfoRec>;
  64. FNoDrives: DWORD;
  65. FNoViewOnDrive: DWORD;
  66. FDesktop: IShellFolder;
  67. FFolders: array[TSpecialFolder] of TSpecialFolderRec;
  68. FHonorDrivePolicy: Integer;
  69. FUseABDrives: Boolean;
  70. FLoaded: Boolean;
  71. function GetFolder(Folder: TSpecialFolder): PSpecialFolderRec;
  72. procedure ReadDriveBasicStatus(Drive: string);
  73. procedure ResetDrive(Drive: string);
  74. procedure SetHonorDrivePolicy(Value: Integer);
  75. function GetFirstFixedDrive: Char;
  76. procedure Load;
  77. function AddDrive(Drive: string): TDriveInfoRec;
  78. function GetDriveBitMask(Drive: string): Integer;
  79. function DoAnyValidPath(DriveType: Integer; CanBeHidden: Boolean; var Path: string): Boolean;
  80. function ReadDriveMask(Reg: TRegistry; ValueName: string): DWORD;
  81. public
  82. function Get(Drive: string): TDriveInfoRec;
  83. property SpecialFolder[Folder: TSpecialFolder]: PSpecialFolderRec read GetFolder;
  84. procedure NeedData;
  85. function AnyValidPath: string;
  86. function GetDriveKey(Path: string): string;
  87. function GetDriveRoot(Drive: string): string;
  88. function IsRealDrive(Drive: string): Boolean;
  89. function IsFixedDrive(Drive: string): Boolean;
  90. function GetImageIndex(Drive: string): Integer;
  91. function GetSimpleName(Drive: string): string;
  92. function GetDisplayName(Drive: string): string;
  93. function GetPrettyName(Drive: string): string;
  94. function ReadDriveStatus(Drive: string; Flags: Integer): Boolean;
  95. procedure OverrideDrivePolicy(Drive: string);
  96. property HonorDrivePolicy: Integer read FHonorDrivePolicy write SetHonorDrivePolicy;
  97. property FirstFixedDrive: Char read GetFirstFixedDrive;
  98. property UseABDrives: Boolean read FUseABDrives write FUseABDrives;
  99. constructor Create;
  100. destructor Destroy; override;
  101. end;
  102. function GetShellFileName(const Name: string): string; overload;
  103. function GetShellFileName(PIDL: PItemIDList): string; overload;
  104. function GetNetWorkName(Drive: string): string;
  105. function GetNetWorkConnected(Drive: string): Boolean;
  106. function IsRootPath(Path: string): Boolean;
  107. function GetThumbnail(Path: string; Size: TSize): TBitmap;
  108. {Central drive information object instance of TDriveInfo}
  109. var
  110. DriveInfo : TDriveInfo;
  111. resourceString
  112. ErrorInvalidDrive = '%s is a invalid drive letter.';
  113. implementation
  114. uses
  115. Math, PIDL, OperationWithTimeout, PasTools, CompThread;
  116. type
  117. PRGBQuadArray = ^TRGBQuadArray; // From graphics.pas
  118. TRGBQuadArray = array[Byte] of TRGBQuad; // From graphics.pas
  119. var
  120. ThreadLock: TRTLCriticalSection;
  121. ReadyDrives: string;
  122. type
  123. TDriveInfoThread = class(TCompThread)
  124. public
  125. constructor Create(Drives: string);
  126. protected
  127. procedure Execute; override;
  128. private
  129. FDrives: string;
  130. end;
  131. constructor TDriveInfoThread.Create(Drives: string);
  132. begin
  133. inherited Create(True);
  134. FDrives := Drives;
  135. FreeOnTerminate := True;
  136. Resume;
  137. end;
  138. procedure TDriveInfoThread.Execute;
  139. var
  140. I: Integer;
  141. FreeSpace, Size: Int64;
  142. DriveRoot: string;
  143. Drive: Char;
  144. begin
  145. if Length(FDrives) = 1 then
  146. begin
  147. Drive := FDrives[1];
  148. DriveRoot := DriveInfo.GetDriveRoot(Drive);
  149. if GetDiskFreeSpaceEx(PChar(DriveRoot), FreeSpace, Size, nil) then
  150. begin
  151. EnterCriticalSection(ThreadLock);
  152. ReadyDrives := ReadyDrives + Drive;
  153. LeaveCriticalSection(ThreadLock);
  154. end;
  155. end
  156. else
  157. begin
  158. for I := 1 to Length(FDrives) do
  159. begin
  160. TDriveInfoThread.Create(FDrives[I]);
  161. Sleep(100);
  162. end;
  163. end;
  164. end;
  165. constructor TDriveInfo.Create;
  166. begin
  167. inherited;
  168. FHonorDrivePolicy := 1;
  169. FUseABDrives := True;
  170. FLoaded := False;
  171. FData := TObjectDictionary<string, TDriveInfoRec>.Create([doOwnsValues]);
  172. end; {TDriveInfo.Create}
  173. destructor TDriveInfo.Destroy;
  174. begin
  175. FData.Free;
  176. inherited;
  177. end; {TDriveInfo.Destroy}
  178. procedure TDriveInfo.NeedData;
  179. var
  180. I: Integer;
  181. Drive: Char;
  182. begin
  183. if not FLoaded then
  184. begin
  185. Load;
  186. FLoaded := True;
  187. end;
  188. EnterCriticalSection(ThreadLock);
  189. try
  190. for I := 1 to Length(ReadyDrives) do
  191. begin
  192. Drive := ReadyDrives[I];
  193. Assert(FData.ContainsKey(Drive));
  194. FData[Drive].DriveReady := True;
  195. end;
  196. ReadyDrives := '';
  197. finally
  198. LeaveCriticalSection(ThreadLock);
  199. end;
  200. end;
  201. function TDriveInfo.DoAnyValidPath(DriveType: Integer; CanBeHidden: Boolean; var Path: string): Boolean;
  202. var
  203. Drive: TRealDrive;
  204. DriveInfoRec: TDriveInfoRec;
  205. begin
  206. for Drive := SystemDrive to LastDrive do
  207. begin
  208. DriveInfoRec := Get(Drive);
  209. if (DriveInfoRec.Valid or
  210. (CanBeHidden and DriveInfoRec.ValidButHiddenByDrivePolicy)) and
  211. (DriveInfoRec.DriveType = DriveType) and
  212. DirectoryExists(ApiPath(GetDriveRoot(Drive))) then
  213. begin
  214. Result := True;
  215. Path := GetDriveRoot(Drive);
  216. Exit;
  217. end;
  218. end;
  219. Result := False;
  220. end;
  221. function TDriveInfo.AnyValidPath: string;
  222. begin
  223. if (not DoAnyValidPath(DRIVE_FIXED, False, Result)) and
  224. (not DoAnyValidPath(DRIVE_FIXED, True, Result)) and
  225. (not DoAnyValidPath(DRIVE_REMOTE, False, Result)) then
  226. begin
  227. raise Exception.Create(SNoValidPath);
  228. end;
  229. end;
  230. function TDriveInfo.IsRealDrive(Drive: string): Boolean;
  231. begin
  232. Result := (Length(Drive) = 1);
  233. Assert((not Result) or ((Drive[1] >= FirstDrive) and (Drive[1] <= LastDrive)));
  234. end;
  235. function TDriveInfo.IsFixedDrive(Drive: string): Boolean;
  236. begin
  237. Result := True;
  238. if IsRealDrive(Drive) and (Drive[1] < FirstFixedDrive) then Result := False;
  239. end;
  240. function TDriveInfo.GetDriveKey(Path: string): string;
  241. begin
  242. Result := ExtractFileDrive(Path);
  243. if (Length(Result) = 2) and (Result[2] = DriveDelim) then
  244. begin
  245. Result := Upcase(Result[1]);
  246. end
  247. else
  248. if IsUncPath(Path) then
  249. begin
  250. Result := LowerCase(Result);
  251. end
  252. else
  253. begin
  254. raise EConvertError.Create(Format(ErrorInvalidDrive, [Path]))
  255. end;
  256. end;
  257. function TDriveInfo.GetDriveRoot(Drive: string): string;
  258. begin
  259. if IsRealDrive(Drive) then
  260. begin
  261. Result := Drive + ':\'
  262. end
  263. else
  264. begin
  265. Assert(IsUncPath(Drive));
  266. Result := IncludeTrailingBackslash(Drive);
  267. end;
  268. end;
  269. function TDriveInfo.GetFolder(Folder: TSpecialFolder): PSpecialFolderRec;
  270. var
  271. FileInfo: TShFileInfo;
  272. Path: PChar;
  273. Flags: Word;
  274. begin
  275. NeedData;
  276. Assert((Folder >= Low(FFolders)) and (Folder <= High(FFolders)));
  277. with FFolders[Folder] do
  278. begin
  279. if not Valid then
  280. begin
  281. SpecialFolderLocation(Folder, Location, PIDL);
  282. if Assigned(PIDL) then
  283. begin
  284. Path := PChar(PIDL);
  285. Flags := SHGFI_PIDL;
  286. end
  287. else
  288. begin
  289. Path := PChar(Location);
  290. Flags := 0;
  291. end;
  292. SHGetFileInfo(Path, 0, FileInfo, SizeOf(FileInfo),
  293. SHGFI_DISPLAYNAME or SHGFI_SYSICONINDEX or SHGFI_SMALLICON or Flags);
  294. ImageIndex := FileInfo.iIcon;
  295. DisplayName := FileInfo.szDisplayName;
  296. Valid := True;
  297. end;
  298. end;
  299. Result := @FFolders[Folder];
  300. end;
  301. procedure TDriveInfo.SetHonorDrivePolicy(Value: Integer);
  302. var
  303. Drive: TRealDrive;
  304. begin
  305. if HonorDrivePolicy <> Value then
  306. begin
  307. FHonorDrivePolicy := Value;
  308. if FLoaded then
  309. begin
  310. for Drive := FirstDrive to LastDrive do
  311. begin
  312. ReadDriveBasicStatus(Drive);
  313. end;
  314. end;
  315. end;
  316. end;
  317. function TDriveInfo.GetFirstFixedDrive: Char;
  318. begin
  319. if UseABDrives then Result := FirstDrive
  320. else Result := SystemDrive;
  321. end;
  322. function TDriveInfo.GetDriveBitMask(Drive: string): Integer;
  323. begin
  324. Assert(IsRealDrive(Drive));
  325. Result := (1 shl (Ord(Drive[1]) - Ord('A')));
  326. end;
  327. procedure TDriveInfo.ReadDriveBasicStatus(Drive: string);
  328. var
  329. ValidDriveType: Boolean;
  330. InaccessibleByDrivePolicy, HiddenByDrivePolicy: Boolean;
  331. DriveBitMask: Integer;
  332. begin
  333. Assert(FData.ContainsKey(Drive));
  334. with FData[Drive] do
  335. begin
  336. DriveType := Windows.GetDriveType(PChar(GetDriveRoot(Drive)));
  337. DriveBitMask := GetDriveBitMask(Drive);
  338. InaccessibleByDrivePolicy :=
  339. IsRealDrive(Drive) and ((HonorDrivePolicy and 2) <> 0) and ((DriveBitMask and FNoViewOnDrive) <> 0);
  340. HiddenByDrivePolicy :=
  341. IsRealDrive(Drive) and ((HonorDrivePolicy and 1) <> 0) and ((DriveBitMask and FNoDrives) <> 0);
  342. ValidDriveType :=
  343. (DriveType in [DRIVE_REMOVABLE, DRIVE_FIXED, DRIVE_CDROM, DRIVE_RAMDISK, DRIVE_REMOTE]) and
  344. (not InaccessibleByDrivePolicy);
  345. ValidButHiddenByDrivePolicy := ValidDriveType and HiddenByDrivePolicy;
  346. Valid := ValidDriveType and (not HiddenByDrivePolicy);
  347. end;
  348. end;
  349. procedure TDriveInfo.ResetDrive(Drive: string);
  350. begin
  351. with FData[Drive] do
  352. begin
  353. DriveReady := False;
  354. DisplayName := '';
  355. PrettyName := '';
  356. DriveSerial := 0;
  357. Size := -1;
  358. ImageIndex := 0;
  359. end;
  360. end;
  361. function TDriveInfo.ReadDriveMask(Reg: TRegistry; ValueName: string): DWORD;
  362. var
  363. DataInfo: TRegDataInfo;
  364. begin
  365. Result := 0;
  366. if Reg.GetDataInfo(ValueName, DataInfo) then
  367. begin
  368. if (DataInfo.RegData = rdBinary) and (DataInfo.DataSize >= SizeOf(Result)) then
  369. begin
  370. Reg.ReadBinaryData(ValueName, Result, SizeOf(Result));
  371. end
  372. else
  373. if DataInfo.RegData = rdInteger then
  374. begin
  375. Result := Reg.ReadInteger(ValueName);
  376. end;
  377. end;
  378. end;
  379. procedure TDriveInfo.Load;
  380. var
  381. Drive: TRealDrive;
  382. Reg: TRegistry;
  383. Folder: TSpecialFolder;
  384. Drives: string;
  385. begin
  386. AppLog('Loading drives');
  387. Reg := TRegistry.Create;
  388. FNoDrives := 0;
  389. FNoViewOnDrive := 0;
  390. try
  391. if Reg.OpenKeyReadOnly('Software\Microsoft\Windows\CurrentVersion\Policies\Explorer') then
  392. begin
  393. FNoDrives := ReadDriveMask(Reg, 'NoDrives');
  394. FNoViewOnDrive := ReadDriveMask(Reg, 'NoViewOnDrive');
  395. end;
  396. finally
  397. Reg.Free;
  398. end;
  399. AppLog(Format('NoDrives mask: %d', [Integer(FNoDrives)]));
  400. AppLog(Format('NoViewOnDrive mask: %d', [Integer(FNoViewOnDrive)]));
  401. FDesktop := nil;
  402. Drives := EmptyStr;
  403. for Drive := FirstDrive to LastDrive do
  404. begin
  405. if AddDrive(Drive).Valid then
  406. Drives := Drives + Drive;
  407. end;
  408. if Length(Drives) > 0 then
  409. begin
  410. AppLog(Format('Drives found: %s', [Drives]));
  411. TDriveInfoThread.Create(Drives);
  412. end;
  413. for Folder := Low(FFolders) to High(FFolders) do
  414. FFolders[Folder].Valid := False;
  415. end;
  416. function TDriveInfo.AddDrive(Drive: string): TDriveInfoRec;
  417. begin
  418. Result := TDriveInfoRec.Create;
  419. FData.Add(Drive, Result);
  420. ResetDrive(Drive);
  421. ReadDriveBasicStatus(Drive);
  422. end;
  423. function TDriveInfo.GetImageIndex(Drive: string): Integer;
  424. begin
  425. NeedData;
  426. Result := 0;
  427. if Get(Drive).Valid then
  428. begin
  429. if Get(Drive).ImageIndex = 0 then
  430. ReadDriveStatus(Drive, dsImageIndex);
  431. Result := Get(Drive).ImageIndex;
  432. end;
  433. end; {TDriveInfo.GetImageIndex}
  434. function TDriveInfo.GetDisplayName(Drive: string): string;
  435. begin
  436. if Get(Drive).Valid then
  437. begin
  438. if Length(Get(Drive).DisplayName) = 0 then
  439. ReadDriveStatus(Drive, dsDisplayName);
  440. Result := Get(Drive).DisplayName;
  441. end
  442. else
  443. begin
  444. Result := GetSimpleName(Drive);
  445. end;
  446. end; {TDriveInfo.GetDisplayname}
  447. function TDriveInfo.GetPrettyName(Drive: string): string;
  448. begin
  449. if Get(Drive).Valid then
  450. begin
  451. if Length(Get(Drive).PrettyName) = 0 then
  452. ReadDriveStatus(Drive, dsDisplayName);
  453. Result := Get(Drive).PrettyName;
  454. end
  455. else
  456. begin
  457. Result := GetSimpleName(Drive);
  458. end;
  459. end; {TDriveInfo.GetPrettyName}
  460. function TDriveInfo.GetSimpleName(Drive: string): string;
  461. begin
  462. Result := Drive;
  463. if IsRealDrive(Result) then Result := Result + ':';
  464. end;
  465. function TDriveInfo.Get(Drive: string): TDriveInfoRec;
  466. begin
  467. NeedData;
  468. // We might want to wait for ReadyDrives to beempty before returning
  469. // (or even better do that only in DriveReady getter)
  470. if not FData.TryGetValue(Drive, Result) then
  471. begin
  472. Assert(IsUncPath(Drive));
  473. Result := AddDrive(Drive);
  474. end;
  475. end; {TDriveInfo.GetData}
  476. function TDriveInfo.ReadDriveStatus(Drive: string; Flags: Integer): Boolean;
  477. var
  478. ErrorMode: Word;
  479. FileInfo: TShFileInfo;
  480. DriveRoot: string;
  481. DriveID: string;
  482. CPos: Integer;
  483. Eaten: ULONG;
  484. ShAttr: ULONG;
  485. MaxFileNameLength: DWORD;
  486. FileSystemFlags: DWORD;
  487. FreeSpace: Int64;
  488. SimpleName: string;
  489. DriveInfoRec: TDriveInfoRec;
  490. S: string;
  491. begin
  492. // Among other, this makes sure the pending drive-ready status from the background thread are collected,
  493. // before we overwrite it with fresh status here.
  494. NeedData;
  495. if not Assigned(FDesktop) then
  496. SHGetDesktopFolder(FDesktop);
  497. DriveRoot := GetDriveRoot(Drive);
  498. // When this method is called, the entry always exists already
  499. Assert(FData.ContainsKey(Drive));
  500. DriveInfoRec := FData[Drive];
  501. with DriveInfoRec do
  502. begin
  503. Init := True;
  504. ReadDriveBasicStatus(Drive);
  505. if Valid then
  506. begin
  507. if (not Assigned(PIDL)) and IsFixedDrive(Drive) then
  508. begin
  509. ShAttr := 0;
  510. if DriveType = DRIVE_REMOTE then
  511. begin
  512. ShellFolderParseDisplayNameWithTimeout(
  513. FDesktop, Application.Handle, nil, PChar(DriveRoot), Eaten, PIDL, ShAttr, 2 * MSecsPerSec);
  514. end
  515. else
  516. begin
  517. FDesktop.ParseDisplayName(Application.Handle, nil, PChar(DriveRoot), Eaten, PIDL, ShAttr);
  518. end;
  519. end;
  520. {Read driveStatus:}
  521. if (Flags and dsSize) <> 0 then
  522. begin
  523. { turn off critical errors }
  524. ErrorMode := SetErrorMode(SEM_FailCriticalErrors or SEM_NOOPENFILEERRORBOX);
  525. try
  526. DriveReady := GetDiskFreeSpaceEx(PChar(DriveRoot), FreeSpace, Size, nil);
  527. if DriveReady then
  528. begin
  529. {Access the physical drive:}
  530. if GetVolumeInformation(PChar(DriveRoot), nil, 0,
  531. @DriveSerial, MaxFileNameLength, FileSystemFlags,
  532. nil, 0) then
  533. begin
  534. end
  535. else
  536. begin
  537. DriveSerial := 0;
  538. end;
  539. end
  540. else
  541. begin
  542. DriveSerial := 0;
  543. end;
  544. finally
  545. { restore old error mode }
  546. SetErrorMode(ErrorMode);
  547. end;
  548. end;
  549. {DisplayName:}
  550. if (Flags and dsDisplayName) <> 0 then
  551. begin
  552. {Fetch drives displayname:}
  553. SimpleName := GetSimpleName(Drive);
  554. if Assigned(PIDL) then DisplayName := GetShellFileName(PIDL)
  555. else
  556. begin
  557. // typical reason we do not have PIDL is that it took too long to
  558. // call ParseDisplayName, in what case calling SHGetFileInfo with
  559. // path (instead of PIDL) will take long too, avoiding that and using
  560. // fallback
  561. DisplayName := '(' + SimpleName + ')';
  562. end;
  563. if DriveType <> DRIVE_REMOTE then
  564. begin
  565. PrettyName := SimpleName + ' ' + DisplayName;
  566. S := ' (' + SimpleName + ')';
  567. CPos := Pos(S, PrettyName);
  568. if CPos > 0 then
  569. Delete(PrettyName, CPos, Length(S));
  570. end
  571. else
  572. if IsRealDrive(Drive) then
  573. begin
  574. DriveID := GetNetWorkName(Drive);
  575. PrettyName := Format('%s %s (%s)', [SimpleName, ExtractFileName(DriveID), ExtractFileDir(DriveID)]);
  576. end
  577. else
  578. begin
  579. Assert(IsUncPath(DriveRoot));
  580. PrettyName := SimpleName;
  581. end;
  582. end;
  583. {ImageIndex:}
  584. if (Flags and dsImageIndex) <> 0 then
  585. begin
  586. if Assigned(PIDL) then
  587. begin
  588. SHGetFileInfo(PChar(PIDL), 0, FileInfo, SizeOf(FileInfo), SHGFI_SYSICONINDEX or SHGFI_SMALLICON or SHGFI_PIDL)
  589. end
  590. else
  591. begin
  592. SHGetFileInfo(PChar(DriveRoot), 0, FileInfo, SizeOf(FileInfo), SHGFI_SYSICONINDEX or SHGFI_SMALLICON);
  593. end;
  594. ImageIndex := FileInfo.iIcon;
  595. end;
  596. end
  597. else
  598. begin
  599. if Assigned(PIDL) then
  600. FreePIDL(PIDL);
  601. ResetDrive(Drive);
  602. end;
  603. Result := Valid and DriveReady;
  604. end;
  605. end; {TDriveInfo.ReadDriveStatus}
  606. procedure TDriveInfo.OverrideDrivePolicy(Drive: string);
  607. var
  608. Mask: DWORD;
  609. begin
  610. Assert(FData.ContainsKey(Drive));
  611. Assert(FData[Drive].ValidButHiddenByDrivePolicy);
  612. Mask := (not GetDriveBitMask(Drive));
  613. FNoDrives := FNoDrives and Mask;
  614. ReadDriveBasicStatus(Drive);
  615. Assert(FData[Drive].Valid);
  616. end;
  617. function GetShellFileName(const Name: string): string;
  618. var
  619. SFI: TSHFileInfo;
  620. E: Integer;
  621. begin
  622. E := SetErrorMode(SEM_FAILCRITICALERRORS);
  623. try
  624. if SHGetFileInfo(PChar(Name), 0, SFI, SizeOf(TSHFileInfo), SHGFI_DISPLAYNAME) <> 0 then
  625. Result := SFI.szDisplayName;
  626. finally
  627. SetErrorMode(E);
  628. end;
  629. end; {GetShellFileName}
  630. function GetShellFileName(PIDL: PItemIDList): string;
  631. var
  632. SFI: TSHFileInfo;
  633. E: Integer;
  634. begin
  635. E := SetErrorMode(SEM_FAILCRITICALERRORS);
  636. try
  637. if SHGetFileInfo(PChar(PIDL), 0, SFI, SizeOf(TSHFileInfo), SHGFI_PIDL or SHGFI_DISPLAYNAME) <> 0 then
  638. Result := SFI.szDisplayName;
  639. finally
  640. SetErrorMode(E);
  641. end;
  642. end; {GetShellFileName}
  643. function GetNetWorkName(Drive: string): string;
  644. var
  645. Path: string;
  646. P: array[0..MAX_PATH] of Char;
  647. MaxLen : DWORD;
  648. begin
  649. Path := ExcludeTrailingBackslash(DriveInfo.GetDriveRoot(Drive));
  650. MaxLen := MAX_PATH;
  651. if WNetGetConnection(PChar(Path), P, MaxLen) = NO_ERROR then
  652. Result := P
  653. else
  654. Result := '';
  655. end; {GetNetWorkName}
  656. type
  657. LPBYTE = ^BYTE;
  658. LMSTR = LPWSTR;
  659. NET_API_STATUS = DWORD;
  660. _USE_INFO_1 = record
  661. ui1_local: LMSTR;
  662. ui1_remote: LMSTR;
  663. ui1_password: LMSTR;
  664. ui1_status: DWORD;
  665. ui1_asg_type: DWORD;
  666. ui1_refcount: DWORD;
  667. ui1_usecount: DWORD;
  668. end;
  669. USE_INFO_1 = _USE_INFO_1;
  670. PUSE_INFO_1 = ^USE_INFO_1;
  671. LPVOID = Pointer;
  672. const
  673. USE_OK = 0;
  674. USE_PAUSED = 1;
  675. USE_SESSLOST = 2;
  676. USE_DISCONN = 2;
  677. USE_NETERR = 3;
  678. USE_CONN = 4;
  679. USE_RECONN = 5;
  680. function NetUseGetInfo(UncServerName: LMSTR; UseName: LMSTR; Level: DWORD; var BufPtr: LPBYTE): NET_API_STATUS; stdcall; external 'netapi32.dll';
  681. function NetApiBufferFree(Buffer: Pointer): DWORD; stdcall; external 'netapi32.dll';
  682. function GetNetWorkConnected(Drive: string): Boolean;
  683. var
  684. BufPtr: LPBYTE;
  685. NetResult: Integer;
  686. ServerName: string;
  687. PServerName: PChar;
  688. Name: string;
  689. P: Integer;
  690. begin
  691. Name := '';
  692. PServerName := nil;
  693. if DriveInfo.IsRealDrive(Drive) then
  694. begin
  695. Name := Drive + ':';
  696. end
  697. else
  698. if IsUncPath(Drive) then
  699. begin
  700. Name := Copy(Drive, 3, Length(Drive) - 2);
  701. P := Pos('\', Name);
  702. if P > 0 then
  703. begin
  704. ServerName := Copy(Name, P + 1, Length(Name) - P);
  705. PServerName := PChar(ServerName);
  706. SetLength(Name, P - 1);
  707. end
  708. else
  709. begin
  710. Assert(False);
  711. end;
  712. end
  713. else
  714. begin
  715. Assert(False);
  716. end;
  717. if Name = '' then
  718. begin
  719. Result := False;
  720. end
  721. else
  722. begin
  723. NetResult := NetUseGetInfo(PServerName, PChar(Name), 1, BufPtr);
  724. if NetResult = 0 then
  725. begin
  726. Result := (PUSE_INFO_1(BufPtr)^.ui1_status = USE_OK);
  727. NetApiBufferFree(LPVOID(BufPtr));
  728. end
  729. else
  730. begin
  731. // NetUseGetInfo works for DFS shares only, hence when it fails
  732. // we suppose different share type and fallback to "connected"
  733. Result := True;
  734. end;
  735. end;
  736. end;
  737. function IsRootPath(Path: string): Boolean;
  738. begin
  739. Result := SameText(ExcludeTrailingBackslash(ExtractFileDrive(Path)), ExcludeTrailingBackslash(Path));
  740. end;
  741. function GetThumbnail(Path: string; Size: TSize): TBitmap;
  742. var
  743. ImageFactory: IShellItemImageFactory;
  744. X, Y: Integer;
  745. Row: PRGBQuadArray;
  746. Pixel: PRGBQuad;
  747. Alpha: Byte;
  748. Handle: HBITMAP;
  749. begin
  750. Result := nil;
  751. SHCreateItemFromParsingName(PChar(Path), nil, IShellItemImageFactory, ImageFactory);
  752. if Assigned(ImageFactory) then
  753. begin
  754. if Succeeded(ImageFactory.GetImage(Size, SIIGBF_RESIZETOFIT, Handle)) then
  755. begin
  756. Result := TBitmap.Create;
  757. try
  758. Result.Handle := Handle;
  759. Result.PixelFormat := pf32bit;
  760. for Y := 0 to Result.Height - 1 do
  761. begin
  762. Row := Result.ScanLine[Y];
  763. for X := 0 to Result.Width - 1 do
  764. begin
  765. Pixel := @Row[X];
  766. Alpha := Pixel.rgbReserved;
  767. Pixel.rgbBlue := (Pixel.rgbBlue * Alpha) div 255;
  768. Pixel.rgbGreen := (Pixel.rgbGreen * Alpha) div 255;
  769. Pixel.rgbRed := (Pixel.rgbRed * Alpha) div 255;
  770. end;
  771. end;
  772. except
  773. Result.Free;
  774. raise;
  775. end;
  776. end;
  777. ImageFactory := nil; // Redundant?
  778. end;
  779. end;
  780. initialization
  781. InitializeCriticalSection(ThreadLock);
  782. if not Assigned(DriveInfo) then
  783. DriveInfo := TDriveInfo.Create;
  784. finalization
  785. if Assigned(DriveInfo) then
  786. begin
  787. DriveInfo.Free;
  788. DriveInfo := nil;
  789. end;
  790. DeleteCriticalSection(ThreadLock);
  791. end.