IEDriveInfo.pas 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  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;
  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. FDesktop: IShellFolder;
  66. FFolders: array[TSpecialFolder] of TSpecialFolderRec;
  67. FHonorDrivePolicy: Boolean;
  68. FUseABDrives: Boolean;
  69. FLoaded: Boolean;
  70. function GetFolder(Folder: TSpecialFolder): PSpecialFolderRec;
  71. procedure ReadDriveBasicStatus(Drive: string);
  72. procedure ResetDrive(Drive: string);
  73. procedure SetHonorDrivePolicy(Value: Boolean);
  74. function GetFirstFixedDrive: Char;
  75. procedure Load;
  76. function AddDrive(Drive: string): TDriveInfoRec;
  77. function GetDriveBitMask(Drive: string): Integer;
  78. function DoAnyValidPath(DriveType: Integer; CanBeHidden: Boolean; var Path: string): Boolean;
  79. public
  80. function Get(Drive: string): TDriveInfoRec;
  81. property SpecialFolder[Folder: TSpecialFolder]: PSpecialFolderRec read GetFolder;
  82. procedure NeedData;
  83. function AnyValidPath: string;
  84. function GetDriveKey(Path: string): string;
  85. function GetDriveRoot(Drive: string): string;
  86. function IsRealDrive(Drive: string): Boolean;
  87. function IsFixedDrive(Drive: string): Boolean;
  88. function GetImageIndex(Drive: string): Integer;
  89. function GetSimpleName(Drive: string): string;
  90. function GetDisplayName(Drive: string): string;
  91. function GetPrettyName(Drive: string): string;
  92. function ReadDriveStatus(Drive: string; Flags: Integer): Boolean;
  93. procedure OverrideDrivePolicy(Drive: string);
  94. property HonorDrivePolicy: Boolean read FHonorDrivePolicy write SetHonorDrivePolicy;
  95. property FirstFixedDrive: Char read GetFirstFixedDrive;
  96. property UseABDrives: Boolean read FUseABDrives write FUseABDrives;
  97. constructor Create;
  98. destructor Destroy; override;
  99. end;
  100. function GetShellFileName(const Name: string): string; overload;
  101. function GetShellFileName(PIDL: PItemIDList): string; overload;
  102. function GetNetWorkName(Drive: string): string;
  103. function GetNetWorkConnected(Drive: string): Boolean;
  104. function IsRootPath(Path: string): Boolean;
  105. {Central drive information object instance of TDriveInfo}
  106. var
  107. DriveInfo : TDriveInfo;
  108. resourceString
  109. ErrorInvalidDrive = '%s is a invalid drive letter.';
  110. implementation
  111. uses
  112. Math, PIDL, OperationWithTimeout, PasTools, CompThread;
  113. var
  114. ThreadLock: TRTLCriticalSection;
  115. ReadyDrives: string;
  116. type
  117. TDriveInfoThread = class(TCompThread)
  118. public
  119. constructor Create(Drives: string);
  120. protected
  121. procedure Execute; override;
  122. private
  123. FDrives: string;
  124. end;
  125. constructor TDriveInfoThread.Create(Drives: string);
  126. begin
  127. inherited Create(True);
  128. FDrives := Drives;
  129. FreeOnTerminate := True;
  130. Resume;
  131. end;
  132. procedure TDriveInfoThread.Execute;
  133. var
  134. I: Integer;
  135. FreeSpace, Size: Int64;
  136. DriveRoot: string;
  137. Drive: Char;
  138. begin
  139. if Length(FDrives) = 1 then
  140. begin
  141. Drive := FDrives[1];
  142. DriveRoot := DriveInfo.GetDriveRoot(Drive);
  143. if GetDiskFreeSpaceEx(PChar(DriveRoot), FreeSpace, Size, nil) then
  144. begin
  145. EnterCriticalSection(ThreadLock);
  146. ReadyDrives := ReadyDrives + Drive;
  147. LeaveCriticalSection(ThreadLock);
  148. end;
  149. end
  150. else
  151. begin
  152. for I := 1 to Length(FDrives) do
  153. begin
  154. TDriveInfoThread.Create(FDrives[I]);
  155. Sleep(100);
  156. end;
  157. end;
  158. end;
  159. constructor TDriveInfo.Create;
  160. begin
  161. inherited;
  162. FHonorDrivePolicy := True;
  163. FUseABDrives := True;
  164. FLoaded := False;
  165. FData := TObjectDictionary<string, TDriveInfoRec>.Create([doOwnsValues]);
  166. end; {TDriveInfo.Create}
  167. destructor TDriveInfo.Destroy;
  168. begin
  169. FData.Free;
  170. inherited;
  171. end; {TDriveInfo.Destroy}
  172. procedure TDriveInfo.NeedData;
  173. var
  174. I: Integer;
  175. Drive: Char;
  176. begin
  177. if not FLoaded then
  178. begin
  179. Load;
  180. FLoaded := True;
  181. end;
  182. EnterCriticalSection(ThreadLock);
  183. try
  184. for I := 1 to Length(ReadyDrives) do
  185. begin
  186. Drive := ReadyDrives[I];
  187. Assert(FData.ContainsKey(Drive));
  188. FData[Drive].DriveReady := True;
  189. end;
  190. ReadyDrives := '';
  191. finally
  192. LeaveCriticalSection(ThreadLock);
  193. end;
  194. end;
  195. function TDriveInfo.DoAnyValidPath(DriveType: Integer; CanBeHidden: Boolean; var Path: string): Boolean;
  196. var
  197. Drive: TRealDrive;
  198. DriveInfoRec: TDriveInfoRec;
  199. begin
  200. for Drive := SystemDrive to LastDrive do
  201. begin
  202. DriveInfoRec := Get(Drive);
  203. if (DriveInfoRec.Valid or
  204. (CanBeHidden and DriveInfoRec.ValidButHiddenByDrivePolicy)) and
  205. (DriveInfoRec.DriveType = DriveType) and
  206. DirectoryExists(ApiPath(GetDriveRoot(Drive))) then
  207. begin
  208. Result := True;
  209. Path := GetDriveRoot(Drive);
  210. Exit;
  211. end;
  212. end;
  213. Result := False;
  214. end;
  215. function TDriveInfo.AnyValidPath: string;
  216. begin
  217. if (not DoAnyValidPath(DRIVE_FIXED, False, Result)) and
  218. (not DoAnyValidPath(DRIVE_FIXED, True, Result)) and
  219. (not DoAnyValidPath(DRIVE_REMOTE, False, Result)) then
  220. begin
  221. raise Exception.Create(SNoValidPath);
  222. end;
  223. end;
  224. function TDriveInfo.IsRealDrive(Drive: string): Boolean;
  225. begin
  226. Result := (Length(Drive) = 1);
  227. Assert((not Result) or ((Drive[1] >= FirstDrive) and (Drive[1] <= LastDrive)));
  228. end;
  229. function TDriveInfo.IsFixedDrive(Drive: string): Boolean;
  230. begin
  231. Result := True;
  232. if IsRealDrive(Drive) and (Drive[1] < FirstFixedDrive) then Result := False;
  233. end;
  234. function TDriveInfo.GetDriveKey(Path: string): string;
  235. begin
  236. Result := ExtractFileDrive(Path);
  237. if (Length(Result) = 2) and (Result[2] = DriveDelim) then
  238. begin
  239. Result := Upcase(Result[1]);
  240. end
  241. else
  242. if IsUncPath(Path) then
  243. begin
  244. Result := LowerCase(Result);
  245. end
  246. else
  247. begin
  248. raise EConvertError.Create(Format(ErrorInvalidDrive, [Path]))
  249. end;
  250. end;
  251. function TDriveInfo.GetDriveRoot(Drive: string): string;
  252. begin
  253. if IsRealDrive(Drive) then
  254. begin
  255. Result := Drive + ':\'
  256. end
  257. else
  258. begin
  259. Assert(IsUncPath(Drive));
  260. Result := IncludeTrailingBackslash(Drive);
  261. end;
  262. end;
  263. function TDriveInfo.GetFolder(Folder: TSpecialFolder): PSpecialFolderRec;
  264. var
  265. FileInfo: TShFileInfo;
  266. Path: PChar;
  267. Flags: Word;
  268. begin
  269. NeedData;
  270. Assert((Folder >= Low(FFolders)) and (Folder <= High(FFolders)));
  271. with FFolders[Folder] do
  272. begin
  273. if not Valid then
  274. begin
  275. SpecialFolderLocation(Folder, Location, PIDL);
  276. if Assigned(PIDL) then
  277. begin
  278. Path := PChar(PIDL);
  279. Flags := SHGFI_PIDL;
  280. end
  281. else
  282. begin
  283. Path := PChar(Location);
  284. Flags := 0;
  285. end;
  286. SHGetFileInfo(Path, 0, FileInfo, SizeOf(FileInfo),
  287. SHGFI_DISPLAYNAME or SHGFI_SYSICONINDEX or SHGFI_SMALLICON or Flags);
  288. ImageIndex := FileInfo.iIcon;
  289. DisplayName := FileInfo.szDisplayName;
  290. Valid := True;
  291. end;
  292. end;
  293. Result := @FFolders[Folder];
  294. end;
  295. procedure TDriveInfo.SetHonorDrivePolicy(Value: Boolean);
  296. var
  297. Drive: TRealDrive;
  298. begin
  299. if HonorDrivePolicy <> Value then
  300. begin
  301. FHonorDrivePolicy := Value;
  302. if FLoaded then
  303. begin
  304. for Drive := FirstDrive to LastDrive do
  305. begin
  306. ReadDriveBasicStatus(Drive);
  307. end;
  308. end;
  309. end;
  310. end;
  311. function TDriveInfo.GetFirstFixedDrive: Char;
  312. begin
  313. if UseABDrives then Result := FirstDrive
  314. else Result := SystemDrive;
  315. end;
  316. function TDriveInfo.GetDriveBitMask(Drive: string): Integer;
  317. begin
  318. Assert(IsRealDrive(Drive));
  319. Result := (1 shl (Ord(Drive[1]) - Ord('A')));
  320. end;
  321. procedure TDriveInfo.ReadDriveBasicStatus(Drive: string);
  322. var
  323. ValidDriveType: Boolean;
  324. HiddenByDrivePolicy: Boolean;
  325. begin
  326. Assert(FData.ContainsKey(Drive));
  327. with FData[Drive] do
  328. begin
  329. DriveType := Windows.GetDriveType(PChar(GetDriveRoot(Drive)));
  330. ValidDriveType := (DriveType in [DRIVE_REMOVABLE, DRIVE_FIXED, DRIVE_CDROM, DRIVE_RAMDISK, DRIVE_REMOTE]);
  331. HiddenByDrivePolicy :=
  332. FHonorDrivePolicy and IsRealDrive(Drive) and ((GetDriveBitMask(Drive) and FNoDrives) <> 0);
  333. ValidButHiddenByDrivePolicy := ValidDriveType and HiddenByDrivePolicy;
  334. Valid := ValidDriveType and (not HiddenByDrivePolicy);
  335. end;
  336. end;
  337. procedure TDriveInfo.ResetDrive(Drive: string);
  338. begin
  339. with FData[Drive] do
  340. begin
  341. DriveReady := False;
  342. DisplayName := '';
  343. PrettyName := '';
  344. DriveSerial := 0;
  345. Size := -1;
  346. ImageIndex := 0;
  347. end;
  348. end;
  349. procedure TDriveInfo.Load;
  350. var
  351. Drive: TRealDrive;
  352. Reg: TRegistry;
  353. Folder: TSpecialFolder;
  354. Drives: string;
  355. begin
  356. AppLog('Loading drives');
  357. FNoDrives := 0;
  358. Reg := TRegistry.Create;
  359. try
  360. if Reg.OpenKeyReadOnly('Software\Microsoft\Windows\CurrentVersion\Policies\Explorer') Then
  361. Reg.ReadBinaryData('NoDrives', FNoDrives, SizeOf(FNoDrives));
  362. except
  363. try
  364. FNoDrives := Reg.ReadInteger('NoDrives');
  365. except
  366. end;
  367. end;
  368. Reg.Free;
  369. AppLog(Format('NoDrives mask: %d', [Integer(FNoDrives)]));
  370. FDesktop := nil;
  371. Drives := EmptyStr;
  372. for Drive := FirstDrive to LastDrive do
  373. begin
  374. if AddDrive(Drive).Valid then
  375. Drives := Drives + Drive;
  376. end;
  377. if Length(Drives) > 0 then
  378. begin
  379. AppLog(Format('Drives found: %s', [Drives]));
  380. TDriveInfoThread.Create(Drives);
  381. end;
  382. for Folder := Low(FFolders) to High(FFolders) do
  383. FFolders[Folder].Valid := False;
  384. end;
  385. function TDriveInfo.AddDrive(Drive: string): TDriveInfoRec;
  386. begin
  387. Result := TDriveInfoRec.Create;
  388. FData.Add(Drive, Result);
  389. ResetDrive(Drive);
  390. ReadDriveBasicStatus(Drive);
  391. end;
  392. function TDriveInfo.GetImageIndex(Drive: string): Integer;
  393. begin
  394. NeedData;
  395. Result := 0;
  396. if Get(Drive).Valid then
  397. begin
  398. if Get(Drive).ImageIndex = 0 then
  399. ReadDriveStatus(Drive, dsImageIndex);
  400. Result := Get(Drive).ImageIndex;
  401. end;
  402. end; {TDriveInfo.GetImageIndex}
  403. function TDriveInfo.GetDisplayName(Drive: string): string;
  404. begin
  405. if Get(Drive).Valid then
  406. begin
  407. if Length(Get(Drive).DisplayName) = 0 then
  408. ReadDriveStatus(Drive, dsDisplayName);
  409. Result := Get(Drive).DisplayName;
  410. end
  411. else
  412. begin
  413. Result := GetSimpleName(Drive);
  414. end;
  415. end; {TDriveInfo.GetDisplayname}
  416. function TDriveInfo.GetPrettyName(Drive: string): string;
  417. begin
  418. if Get(Drive).Valid then
  419. begin
  420. if Length(Get(Drive).PrettyName) = 0 then
  421. ReadDriveStatus(Drive, dsDisplayName);
  422. Result := Get(Drive).PrettyName;
  423. end
  424. else
  425. begin
  426. Result := GetSimpleName(Drive);
  427. end;
  428. end; {TDriveInfo.GetPrettyName}
  429. function TDriveInfo.GetSimpleName(Drive: string): string;
  430. begin
  431. Result := Drive;
  432. if IsRealDrive(Result) then Result := Result + ':';
  433. end;
  434. function TDriveInfo.Get(Drive: string): TDriveInfoRec;
  435. begin
  436. NeedData;
  437. // We might want to wait for ReadyDrives to beempty before returning
  438. // (or even better do that only in DriveReady getter)
  439. if not FData.TryGetValue(Drive, Result) then
  440. begin
  441. Assert(IsUncPath(Drive));
  442. Result := AddDrive(Drive);
  443. end;
  444. end; {TDriveInfo.GetData}
  445. function TDriveInfo.ReadDriveStatus(Drive: string; Flags: Integer): Boolean;
  446. var
  447. ErrorMode: Word;
  448. FileInfo: TShFileInfo;
  449. DriveRoot: string;
  450. DriveID: string;
  451. CPos: Integer;
  452. Eaten: ULONG;
  453. ShAttr: ULONG;
  454. MaxFileNameLength: DWORD;
  455. FileSystemFlags: DWORD;
  456. FreeSpace: Int64;
  457. SimpleName: string;
  458. DriveInfoRec: TDriveInfoRec;
  459. S: string;
  460. begin
  461. // Among other, this makes sure the pending drive-ready status from the background thread are collected,
  462. // before we overwrite it with fresh status here.
  463. NeedData;
  464. if not Assigned(FDesktop) then
  465. SHGetDesktopFolder(FDesktop);
  466. DriveRoot := GetDriveRoot(Drive);
  467. // When this method is called, the entry always exists already
  468. Assert(FData.ContainsKey(Drive));
  469. DriveInfoRec := FData[Drive];
  470. with DriveInfoRec do
  471. begin
  472. Init := True;
  473. ReadDriveBasicStatus(Drive);
  474. if Valid then
  475. begin
  476. if (not Assigned(PIDL)) and IsFixedDrive(Drive) then
  477. begin
  478. ShAttr := 0;
  479. if DriveType = DRIVE_REMOTE then
  480. begin
  481. ShellFolderParseDisplayNameWithTimeout(
  482. FDesktop, Application.Handle, nil, PChar(DriveRoot), Eaten, PIDL, ShAttr, 2 * MSecsPerSec);
  483. end
  484. else
  485. begin
  486. FDesktop.ParseDisplayName(Application.Handle, nil, PChar(DriveRoot), Eaten, PIDL, ShAttr);
  487. end;
  488. end;
  489. {Read driveStatus:}
  490. if (Flags and dsSize) <> 0 then
  491. begin
  492. { turn off critical errors }
  493. ErrorMode := SetErrorMode(SEM_FailCriticalErrors or SEM_NOOPENFILEERRORBOX);
  494. try
  495. DriveReady := GetDiskFreeSpaceEx(PChar(DriveRoot), FreeSpace, Size, nil);
  496. if DriveReady then
  497. begin
  498. {Access the physical drive:}
  499. if GetVolumeInformation(PChar(DriveRoot), nil, 0,
  500. @DriveSerial, MaxFileNameLength, FileSystemFlags,
  501. nil, 0) then
  502. begin
  503. end
  504. else
  505. begin
  506. DriveSerial := 0;
  507. end;
  508. end
  509. else
  510. begin
  511. DriveSerial := 0;
  512. end;
  513. finally
  514. { restore old error mode }
  515. SetErrorMode(ErrorMode);
  516. end;
  517. end;
  518. {DisplayName:}
  519. if (Flags and dsDisplayName) <> 0 then
  520. begin
  521. {Fetch drives displayname:}
  522. SimpleName := GetSimpleName(Drive);
  523. if Assigned(PIDL) then DisplayName := GetShellFileName(PIDL)
  524. else
  525. begin
  526. // typical reason we do not have PIDL is that it took too long to
  527. // call ParseDisplayName, in what case calling SHGetFileInfo with
  528. // path (instead of PIDL) will take long too, avoiding that and using
  529. // fallback
  530. DisplayName := '(' + SimpleName + ')';
  531. end;
  532. if DriveType <> DRIVE_REMOTE then
  533. begin
  534. PrettyName := SimpleName + ' ' + DisplayName;
  535. S := ' (' + SimpleName + ')';
  536. CPos := Pos(S, PrettyName);
  537. if CPos > 0 then
  538. Delete(PrettyName, CPos, Length(S));
  539. end
  540. else
  541. if IsRealDrive(Drive) then
  542. begin
  543. DriveID := GetNetWorkName(Drive);
  544. PrettyName := Format('%s %s (%s)', [SimpleName, ExtractFileName(DriveID), ExtractFileDir(DriveID)]);
  545. end
  546. else
  547. begin
  548. Assert(IsUncPath(DriveRoot));
  549. PrettyName := SimpleName;
  550. end;
  551. end;
  552. {ImageIndex:}
  553. if (Flags and dsImageIndex) <> 0 then
  554. begin
  555. if Assigned(PIDL) then
  556. begin
  557. SHGetFileInfo(PChar(PIDL), 0, FileInfo, SizeOf(FileInfo), SHGFI_SYSICONINDEX or SHGFI_SMALLICON or SHGFI_PIDL)
  558. end
  559. else
  560. begin
  561. SHGetFileInfo(PChar(DriveRoot), 0, FileInfo, SizeOf(FileInfo), SHGFI_SYSICONINDEX or SHGFI_SMALLICON);
  562. end;
  563. ImageIndex := FileInfo.iIcon;
  564. end;
  565. end
  566. else
  567. begin
  568. if Assigned(PIDL) then
  569. FreePIDL(PIDL);
  570. ResetDrive(Drive);
  571. end;
  572. Result := Valid and DriveReady;
  573. end;
  574. end; {TDriveInfo.ReadDriveStatus}
  575. procedure TDriveInfo.OverrideDrivePolicy(Drive: string);
  576. var
  577. Mask: DWORD;
  578. begin
  579. Assert(FData.ContainsKey(Drive));
  580. Assert(FData[Drive].ValidButHiddenByDrivePolicy);
  581. Mask := (not GetDriveBitMask(Drive));
  582. FNoDrives := FNoDrives and Mask;
  583. ReadDriveBasicStatus(Drive);
  584. Assert(FData[Drive].Valid);
  585. end;
  586. function GetShellFileName(const Name: string): string;
  587. var
  588. SFI: TSHFileInfo;
  589. E: Integer;
  590. begin
  591. E := SetErrorMode(SEM_FAILCRITICALERRORS);
  592. try
  593. if SHGetFileInfo(PChar(Name), 0, SFI, SizeOf(TSHFileInfo), SHGFI_DISPLAYNAME) <> 0 then
  594. Result := SFI.szDisplayName;
  595. finally
  596. SetErrorMode(E);
  597. end;
  598. end; {GetShellFileName}
  599. function GetShellFileName(PIDL: PItemIDList): string;
  600. var
  601. SFI: TSHFileInfo;
  602. E: Integer;
  603. begin
  604. E := SetErrorMode(SEM_FAILCRITICALERRORS);
  605. try
  606. if SHGetFileInfo(PChar(PIDL), 0, SFI, SizeOf(TSHFileInfo), SHGFI_PIDL or SHGFI_DISPLAYNAME) <> 0 then
  607. Result := SFI.szDisplayName;
  608. finally
  609. SetErrorMode(E);
  610. end;
  611. end; {GetShellFileName}
  612. function GetNetWorkName(Drive: string): string;
  613. var
  614. Path: string;
  615. P: array[0..MAX_PATH] of Char;
  616. MaxLen : DWORD;
  617. begin
  618. Path := ExcludeTrailingBackslash(DriveInfo.GetDriveRoot(Drive));
  619. MaxLen := MAX_PATH;
  620. if WNetGetConnection(PChar(Path), P, MaxLen) = NO_ERROR then
  621. Result := P
  622. else
  623. Result := '';
  624. end; {GetNetWorkName}
  625. type
  626. LPBYTE = ^BYTE;
  627. LMSTR = LPWSTR;
  628. NET_API_STATUS = DWORD;
  629. _USE_INFO_1 = record
  630. ui1_local: LMSTR;
  631. ui1_remote: LMSTR;
  632. ui1_password: LMSTR;
  633. ui1_status: DWORD;
  634. ui1_asg_type: DWORD;
  635. ui1_refcount: DWORD;
  636. ui1_usecount: DWORD;
  637. end;
  638. USE_INFO_1 = _USE_INFO_1;
  639. PUSE_INFO_1 = ^USE_INFO_1;
  640. LPVOID = Pointer;
  641. const
  642. USE_OK = 0;
  643. USE_PAUSED = 1;
  644. USE_SESSLOST = 2;
  645. USE_DISCONN = 2;
  646. USE_NETERR = 3;
  647. USE_CONN = 4;
  648. USE_RECONN = 5;
  649. function NetUseGetInfo(UncServerName: LMSTR; UseName: LMSTR; Level: DWORD; var BufPtr: LPBYTE): NET_API_STATUS; stdcall; external 'netapi32.dll';
  650. function NetApiBufferFree(Buffer: Pointer): DWORD; stdcall; external 'netapi32.dll';
  651. function GetNetWorkConnected(Drive: string): Boolean;
  652. var
  653. BufPtr: LPBYTE;
  654. NetResult: Integer;
  655. ServerName: string;
  656. PServerName: PChar;
  657. Name: string;
  658. P: Integer;
  659. begin
  660. Name := '';
  661. PServerName := nil;
  662. if DriveInfo.IsRealDrive(Drive) then
  663. begin
  664. Name := Drive + ':';
  665. end
  666. else
  667. if IsUncPath(Drive) then
  668. begin
  669. Name := Copy(Drive, 3, Length(Drive) - 2);
  670. P := Pos('\', Name);
  671. if P > 0 then
  672. begin
  673. ServerName := Copy(Name, P + 1, Length(Name) - P);
  674. PServerName := PChar(ServerName);
  675. SetLength(Name, P - 1);
  676. end
  677. else
  678. begin
  679. Assert(False);
  680. end;
  681. end
  682. else
  683. begin
  684. Assert(False);
  685. end;
  686. if Name = '' then
  687. begin
  688. Result := False;
  689. end
  690. else
  691. begin
  692. NetResult := NetUseGetInfo(PServerName, PChar(Name), 1, BufPtr);
  693. if NetResult = 0 then
  694. begin
  695. Result := (PUSE_INFO_1(BufPtr)^.ui1_status = USE_OK);
  696. NetApiBufferFree(LPVOID(BufPtr));
  697. end
  698. else
  699. begin
  700. // NetUseGetInfo works for DFS shares only, hence when it fails
  701. // we suppose different share type and fallback to "connected"
  702. Result := True;
  703. end;
  704. end;
  705. end;
  706. function IsRootPath(Path: string): Boolean;
  707. begin
  708. Result := SameText(ExcludeTrailingBackslash(ExtractFileDrive(Path)), ExcludeTrailingBackslash(Path));
  709. end;
  710. initialization
  711. InitializeCriticalSection(ThreadLock);
  712. if not Assigned(DriveInfo) then
  713. DriveInfo := TDriveInfo.Create;
  714. finalization
  715. if Assigned(DriveInfo) then
  716. begin
  717. DriveInfo.Free;
  718. DriveInfo := nil;
  719. end;
  720. DeleteCriticalSection(ThreadLock);
  721. end.