-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJclShell.pas
More file actions
1634 lines (1498 loc) · 50.2 KB
/
Copy pathJclShell.pas
File metadata and controls
1634 lines (1498 loc) · 50.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{**************************************************************************************************}
{ }
{ Project JEDI Code Library (JCL) }
{ }
{ The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License"); }
{ you may not use this file except in compliance with the License. You may obtain a copy of the }
{ License at http://www.mozilla.org/MPL/ }
{ }
{ Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF }
{ ANY KIND, either express or implied. See the License for the specific language governing rights }
{ and limitations under the License. }
{ }
{ The Original Code is JclShell.pas. }
{ }
{ The Initial Developers of the Original Code are Marcel van Brakel and Petr Vones. }
{ Portions created by these individuals are Copyright (C) of these individuals. }
{ All Rights Reserved. }
{ }
{ Contributor(s): }
{ Rik Barker (rikbarker) }
{ Marcel van Brakel }
{ Jean-Fabien Connault (cycocrew) }
{ Aleksej Kudinov }
{ Robert Marquardt (marquardt) }
{ Robert Rossmair (rrossmair) }
{ Olivier Sannier (obones) }
{ Matthias Thoma (mthoma) }
{ Petr Vones (pvones) }
{ kogerbnz }
{ Florent Ouchet (outchy) }
{ }
{**************************************************************************************************}
{ }
{ This unit contains routines and classes which makes working with the Windows Shell a bit easier. }
{ Included are routines for working with PIDL's, special folder's, file and folder manipulation }
{ through shell interfaces, shortcut's and program execution. }
{ }
{**************************************************************************************************}
{ }
{ Last modified: $Date:: $ }
{ Revision: $Rev:: $ }
{ Author: $Author:: $ }
{ }
{**************************************************************************************************}
unit JclShell;
{$I jcl.inc}
{$I windowsonly.inc}
interface
uses
{$IFDEF UNITVERSIONING}
JclUnitVersioning,
{$ENDIF UNITVERSIONING}
{$IFDEF HAS_UNITSCOPE}
Winapi.Windows, System.SysUtils, System.Classes, Winapi.ShlObj,
{$ELSE ~HAS_UNITSCOPE}
Windows, SysUtils, Classes, ShlObj,
{$ENDIF ~HAS_UNITSCOPE}
JclBase, JclWin32, JclSysUtils;
// Files and Folders
type
TSHDeleteOption = (doSilent, doAllowUndo, doFilesOnly);
TSHDeleteOptions = set of TSHDeleteOption;
TSHRenameOption = (roSilent, roRenameOnCollision);
TSHRenameOptions = set of TSHRenameOption;
TSHCopyOption = (coSilent, coAllowUndo, coFilesOnly, coNoConfirmation);
TSHCopyOptions = set of TSHCopyOption;
TSHMoveOption = (moSilent, moAllowUndo, moFilesOnly, moNoConfirmation);
TSHMoveOptions = set of TSHMoveOption;
function SHDeleteFiles(Parent: THandle; const Files: string; Options: TSHDeleteOptions): Boolean;
function SHDeleteFolder(Parent: THandle; const Folder: string; Options: TSHDeleteOptions): Boolean;
function SHRenameFile(const Src, Dest: string; Options: TSHRenameOptions): Boolean;
function SHCopy(Parent: THandle; const Src, Dest: string; Options: TSHCopyOptions): Boolean;
function SHMove(Parent: THandle; const Src, Dest: string; Options: TSHMoveOptions): Boolean;
type
TEnumFolderFlag = (efFolders, efNonFolders, efIncludeHidden);
TEnumFolderFlags = set of TEnumFolderFlag;
TEnumFolderRec = record
DisplayName: string;
Attributes: DWORD;
IconLarge: HICON;
IconSmall: HICON;
Item: PItemIdList;
EnumIdList: IEnumIdList;
Folder: IShellFolder;
end;
function SHEnumFolderFirst(const Folder: string; Flags: TEnumFolderFlags;
var F: TEnumFolderRec): Boolean;
function SHEnumSpecialFolderFirst(SpecialFolder: DWORD; Flags: TEnumFolderFlags;
var F: TEnumFolderRec): Boolean;
procedure SHEnumFolderClose(var F: TEnumFolderRec);
function SHEnumFolderNext(var F: TEnumFolderRec): Boolean;
function GetSpecialFolderLocation(const FolderID: Integer): string;
function DisplayPropDialog(const Handle: THandle; const FileName: string): Boolean; overload;
function DisplayPropDialog(const Handle: THandle; Item: PItemIdList): Boolean; overload;
function DisplayContextMenuPidl(const Handle: THandle; const Folder: IShellFolder;
Item: PItemIdList; Pos: TPoint): Boolean;
function DisplayContextMenu(const Handle: THandle; const FileName: string;
Pos: TPoint): Boolean;
function OpenFolder(const Path: string; Parent: THandle = 0; Explore: Boolean = False): Boolean;
function OpenSpecialFolder(FolderID: Integer; Parent: THandle = 0; Explore: Boolean = False): Boolean;
// Memory Management
function SHReallocMem(var P: Pointer; Count: Integer): Boolean;
function SHAllocMem(out P: Pointer; Count: Integer): Boolean;
function SHGetMem(var P: Pointer; Count: Integer): Boolean;
function SHFreeMem(var P: Pointer): Boolean;
// Paths and PIDLs
function DriveToPidlBind(const DriveName: string; out Folder: IShellFolder): PItemIdList;
function PathToPidl(const Path: string; Folder: IShellFolder): PItemIdList;
function PathToPidlBind(const FileName: string; out Folder: IShellFolder): PItemIdList;
function PidlBindToParent(IdList: PItemIdList; out Folder: IShellFolder; out Last: PItemIdList): Boolean;
function PidlCompare(Pidl1, Pidl2: PItemIdList): Boolean;
function PidlCopy(Source: PItemIdList; out Dest: PItemIdList): Boolean;
function PidlFree(var IdList: PItemIdList): Boolean;
function PidlGetDepth(Pidl: PItemIdList): Integer;
function PidlGetLength(Pidl: PItemIdList): Integer;
function PidlGetNext(Pidl: PItemIdList): PItemIdList;
function PidlToPath(IdList: PItemIdList): string;
function StrRetFreeMem(StrRet: TStrRet): Boolean;
function StrRetToString(IdList: PItemIdList; StrRet: TStrRet; Free: Boolean): string;
// Shortcuts / Shell link
type
PShellLink = ^TShellLink;
TShellLink = record
Arguments: string;
ShowCmd: Integer;
WorkingDirectory: string;
IdList: PItemIDList;
Target: string;
Description: string;
IconLocation: string;
IconIndex: Integer;
HotKey: Word; // Use ShellLinkShortCut() to convert it to a TShortCut
end;
procedure ShellLinkFree(var Link: TShellLink);
function ShellLinkResolve(const FileName: string; out Link: TShellLink): HRESULT; overload;
function ShellLinkResolve(const FileName: string; out Link: TShellLink;
const ResolveFlags: Cardinal): HRESULT; overload;
function ShellLinkCreate(const Link: TShellLink; const FileName: string): HRESULT;
function ShellLinkCreateSystem(const Link: TShellLink; const Folder: Integer; const FileName: string): HRESULT;
function ShellLinkIcon(const Link: TShellLink): HICON; overload;
function ShellLinkIcon(const FileName: string): HICON; overload;
function ShellLinkShortCut(const Link: TShellLink): TShortCut;
// Miscellaneous
function SHDllGetVersion(const FileName: string; var Version: TDllVersionInfo): Boolean;
function GetSystemIcon(IconIndex: Integer; Flags: Cardinal): HICON;
function OverlayIcon(var Icon: HICON; Overlay: HICON; Large: Boolean): Boolean;
function OverlayIconShortCut(var Large, Small: HICON): Boolean;
function OverlayIconShared(var Large, Small: HICON): Boolean;
function SHGetItemInfoTip(const Folder: IShellFolder; Item: PItemIdList): string;
function ShellExecEx(const FileName: string; const Parameters: string = ''; const Verb: string = '';
CmdShow: Integer = SW_SHOWNORMAL): Boolean;
function ShellExec(Wnd: Integer; const Operation, FileName, Parameters, Directory: string; ShowCommand: Integer): Boolean;
function ShellExecAndWait(const FileName: string; const Parameters: string = ''; const Verb: string = '';
CmdShow: Integer = SW_SHOWNORMAL; const Directory: string = ''): Boolean;
function ShellOpenAs(const FileName: string): Boolean;
function ShellRasDial(const EntryName: string): Boolean;
function ShellRunControlPanel(const NameOrFileName: string; AppletNumber: Integer = 0): Boolean;
function RunAsAdmin(const FileName: string; const Parameters: string = ''; const Parent: THandle = 0): Boolean;
function GetFileNameIcon(const FileName: string; Flags: Cardinal = 0): HICON;
type
TJclFileExeType = (etError, etMsDos, etWin16, etWin32Gui, etWin32Con);
function GetFileExeType(const FileName: TFileName): TJclFileExeType;
function ShellFindExecutable(const FileName, DefaultDir: string): string;
//MSI functions and types used in ShellLinkResolve - copied from JwaMsi.pas
type
INSTALLSTATE = Longint;
const
MSILIB = 'msi.dll';
GetShortcutTargetName = 'MsiGetShortcutTarget' + AWSuffix;
GetComponentPathName = 'MsiGetComponentPath' + AWSuffix;
var
// MSI.DLL functions can''t be converted to Unicode due to an internal compiler bug (F2084 Internal Error: URW1021)
RtdlMsiLibHandle: TModuleHandle = INVALID_MODULEHANDLE_VALUE;
RtdlMsiGetShortcutTarget: function(szShortcutPath: LPCTSTR; szProductCode: LPTSTR;
szFeatureId: LPTSTR; szComponentCode: LPTSTR): UINT stdcall = nil;
RtdlMsiGetComponentPath: function(szProduct: LPCTSTR; szComponent: LPCTSTR;
lpPathBuf: LPTSTR; pcchBuf: LPDWORD): INSTALLSTATE stdcall = nil;
{$IFDEF UNITVERSIONING}
const
UnitVersioning: TUnitVersionInfo = (
RCSfile: '$URL$';
Revision: '$Revision$';
Date: '$Date$';
LogPath: 'JCL\source\windows';
Extra: '';
Data: nil
);
{$ENDIF UNITVERSIONING}
implementation
uses
{$IFDEF HAS_UNITSCOPE}
Winapi.ActiveX, Winapi.CommCtrl, Winapi.Messages, Winapi.ShellAPI,
{$ELSE ~HAS_UNITSCOPE}
ActiveX, CommCtrl, Messages, ShellAPI,
{$ENDIF ~HAS_UNITSCOPE}
JclFileUtils, JclStrings, JclSysInfo;
type
TWidePath = array [0..MAX_PATH-1] of WideChar;
{$IFDEF SUPPORTS_UNICODE}
TWidePathPtr = PWideChar;
{$ELSE ~SUPPORTS_UNICODE}
TWidePathPtr = TWidePath;
{$ENDIF ~SUPPORTS_UNICODE}
const
cVerbProperties = 'properties';
cVerbOpen = 'open';
cVerbExplore = 'explore';
cVerbRunas = 'runas';
//=== Files and Folders ======================================================
// Helper function and constant to map a TSHDeleteOptions set to a Cardinal
const
FOF_COMPLETELYSILENT = FOF_SILENT or FOF_NOCONFIRMATION or FOF_NOERRORUI or FOF_NOCONFIRMMKDIR;
function DeleteOptionsToCardinal(Options: TSHDeleteOptions): Cardinal;
begin
Result := 0;
if doSilent in Options then
Result := Result or FOF_COMPLETELYSILENT;
if doAllowUndo in Options then
Result := Result or FOF_ALLOWUNDO;
if doFilesOnly in Options then
Result := Result or FOF_FILESONLY;
end;
function SHDeleteFiles(Parent: THandle; const Files: string;
Options: TSHDeleteOptions): Boolean;
var
FileOp: TSHFileOpStruct;
Source: string;
begin
ResetMemory(FileOp, SizeOf(FileOp));
with FileOp do
begin
Wnd := Parent;
wFunc := FO_DELETE;
Source := Files + #0#0;
pFrom := PChar(Source);
fFlags := DeleteOptionsToCardinal(Options);
end;
Result := SHFileOperation(FileOp) = 0;
end;
function SHDeleteFolder(Parent: THandle; const Folder: string;
Options: TSHDeleteOptions): Boolean;
begin
Exclude(Options, doFilesOnly);
Result := SHDeleteFiles(Parent, PathAddSeparator(Folder) + '*.*', Options);
if Result then
Result := SHDeleteFiles(Parent, PathRemoveSeparator(Folder), Options);
end;
// Helper function to map a TSHRenameOptions set to a cardinal
function RenameOptionsToCardinal(Options: TSHRenameOptions): Cardinal;
begin
Result := 0;
if roRenameOnCollision in Options then
Result := Result or FOF_RENAMEONCOLLISION;
if roSilent in Options then
Result := Result or FOF_COMPLETELYSILENT;
end;
function SHRenameFile(const Src, Dest: string; Options: TSHRenameOptions): Boolean;
var
FileOp: TSHFileOpStruct;
Source, Destination: string;
begin
ResetMemory(FileOp, SizeOf(FileOp));
with FileOp do
begin
Wnd := GetDesktopWindow;
wFunc := FO_RENAME;
Source := Src + #0#0;
Destination := Dest + #0#0;
pFrom := PChar(Source);
pTo := PChar(Destination);
fFlags := RenameOptionsToCardinal(Options);
end;
Result := SHFileOperation(FileOp) = 0;
end;
function CopyOptionsToCardinal(Options: TSHCopyOptions): Cardinal;
begin
Result := 0;
if coSilent in Options then
Result := Result or FOF_SILENT;
if coAllowUndo in Options then
Result := Result or FOF_ALLOWUNDO;
if coFilesOnly in Options then
Result := Result or FOF_FILESONLY;
if coNoConfirmation in Options then
Result := Result or FOF_NOCONFIRMATION or FOF_NOCONFIRMMKDIR;
end;
function SHCopy(Parent: THandle; const Src, Dest: string; Options: TSHCopyOptions): Boolean;
var
FileOp: TSHFileOpStruct;
Source, Destination: string;
begin
ResetMemory(FileOp, SizeOf(FileOp));
FileOp.Wnd := Parent;
FileOp.wFunc := FO_COPY;
Source := Src + #0#0;
Destination := Dest + #0#0;
FileOp.pFrom := PChar(Source);
FileOp.pTo := PChar(Destination);
FileOp.fFlags := CopyOptionsToCardinal(Options);
Result := SHFileOperation(FileOp) = 0;
end;
function MoveOptionsToCardinal(Options: TSHMoveOptions): Cardinal;
begin
Result := 0;
if moSilent in Options then
Result := Result or FOF_SILENT;
if moAllowUndo in Options then
Result := Result or FOF_ALLOWUNDO;
if moFilesOnly in Options then
Result := Result or FOF_FILESONLY;
if moNoConfirmation in Options then
Result := Result or FOF_NOCONFIRMATION;
end;
function SHMove(Parent: THandle; const Src, Dest: string; Options: TSHMoveOptions): Boolean;
var
FileOp: TSHFileOpStruct;
Source, Destination: string;
begin
ResetMemory(FileOp, SizeOf(FileOp));
FileOp.Wnd := Parent;
FileOp.wFunc := FO_MOVE;
Source := Src + #0#0;
Destination := Dest + #0#0;
FileOp.pFrom := PChar(Source);
FileOp.pTo := PChar(Destination);
FileOp.fFlags := MoveOptionsToCardinal(Options);
Result := SHFileOperation(FileOp) = 0;
end;
function EnumFolderFlagsToCardinal(Flags: TEnumFolderFlags): Cardinal;
begin
Result := 0;
if efFolders in Flags then
Result := Result or SHCONTF_FOLDERS;
if efNonFolders in Flags then
Result := Result or SHCONTF_NONFOLDERS;
if efIncludeHidden in Flags then
Result := Result or SHCONTF_INCLUDEHIDDEN;
end;
procedure ClearEnumFolderRec(var F: TEnumFolderRec; const Free, Release: Boolean);
begin
if Release then
begin
F.EnumIdList := nil;
F.Folder := nil;
end;
if Free then
begin
PidlFree(F.Item);
DestroyIcon(F.IconLarge);
DestroyIcon(F.IconSmall);
end;
F.Attributes := 0;
F.Item := nil;
F.IconLarge := 0;
F.IconSmall := 0;
end;
procedure SHEnumFolderClose(var F: TEnumFolderRec);
begin
ClearEnumFolderRec(F, True, True);
end;
function SHEnumFolderNext(var F: TEnumFolderRec): Boolean;
const
Attr = Cardinal(SFGAO_CAPABILITYMASK or SFGAO_DISPLAYATTRMASK or SFGAO_CONTENTSMASK);
var
DisplayNameRet: TStrRet;
ItemsFetched: ULONG;
ExtractIcon: IExtractIcon;
IconFile: TWidePath;
IconIndex: Integer;
Flags: DWORD;
begin
Result := False;
ClearEnumFolderRec(F, True, False);
if (F.EnumIdList = nil) or (F.Folder = nil) then
Exit;
ItemsFetched := 0;
if F.EnumIdList.Next(1, F.Item, ItemsFetched) = NO_ERROR then
begin
DisplayNameRet.utype := 0;
F.Folder.GetDisplayNameOf(F.Item, SHGDN_INFOLDER, DisplayNameRet);
F.DisplayName := StrRetToString(F.Item, DisplayNameRet, True);
F.Attributes := Attr;
F.Folder.GetAttributesOf(1, F.Item, F.Attributes);
F.Folder.GetUIObjectOf(0, 1, F.Item, IID_IExtractIconW, nil,
Pointer(ExtractIcon));
Flags := 0;
F.IconLarge := 0;
F.IconSmall := 0;
if Assigned(ExtractIcon) then
begin
IconIndex := 0;
ExtractIcon.GetIconLocation(0, @IconFile, MAX_PATH, IconIndex, Flags);
if (IconIndex < 0) or ((Flags and GIL_NOTFILENAME) = 0) then
ExtractIconEx(@IconFile, IconIndex, F.IconLarge, F.IconSmall, 1)
else
ExtractIcon.Extract(@IconFile, IconIndex, F.IconLarge, F.IconSmall,
MakeLong(32, 16));
end;
Result := True;
end;
end;
function SHEnumSpecialFolderFirst(SpecialFolder: DWORD; Flags: TEnumFolderFlags;
var F: TEnumFolderRec): Boolean;
var
DesktopFolder: IShellFolder;
FolderPidl: PItemIdList;
begin
ClearEnumFolderRec(F, False, False);
Result := Succeeded(SHGetDesktopFolder(DesktopFolder));
if Result then
begin
if SpecialFolder = CSIDL_DESKTOP then
F.Folder := DesktopFolder
else
begin
Result := Succeeded(SHGetSpecialFolderLocation(0, SpecialFolder, FolderPidl));
if Result then
begin
try
Result := Succeeded(DesktopFolder.BindToObject(FolderPidl, nil, IID_IShellFolder, Pointer(F.Folder)));
finally
CoTaskMemFree(FolderPidl);
end;
end;
end;
end;
if Result then
begin
F.Folder.EnumObjects(0, EnumFolderFlagsToCardinal(Flags), F.EnumIdList);
Result := SHEnumFolderNext(F);
if not Result then
SHEnumFolderClose(F);
end;
end;
function SHEnumFolderFirst(const Folder: string; Flags: TEnumFolderFlags;
var F: TEnumFolderRec): Boolean;
var
DesktopFolder: IShellFolder;
FolderPidl: PItemIdList;
begin
ClearEnumFolderRec(F, False, False);
SHGetDesktopFolder(DesktopFolder);
FolderPidl := PathToPidl(PathAddSeparator(Folder), DesktopFolder);
try
DesktopFolder.BindToObject(FolderPidl, nil, IID_IShellFolder, Pointer(F.Folder));
F.Folder.EnumObjects(0, EnumFolderFlagsToCardinal(Flags), F.EnumIdList);
Result := SHEnumFolderNext(F);
if not Result then
SHEnumFolderClose(F);
finally
PidlFree(FolderPidl);
end;
end;
function GetSpecialFolderLocation(const FolderID: Integer): string;
var
FolderPidl: PItemIdList;
begin
if Succeeded(SHGetSpecialFolderLocation(0, FolderID, FolderPidl)) then
begin
Result := PidlToPath(FolderPidl);
CoTaskMemFree(FolderPidl);
end
else
Result := '';
end;
function DisplayPropDialog(const Handle: THandle; const FileName: string): Boolean;
var
Info: TShellExecuteInfo;
begin
ResetMemory(Info, SizeOf(Info));
with Info do
begin
cbSize := SizeOf(Info);
lpFile := PChar(FileName);
nShow := SW_SHOW;
fMask := SEE_MASK_INVOKEIDLIST;
Wnd := Handle;
lpVerb := cVerbProperties;
end;
{$TYPEDADDRESS ON} // need this because ShellExecuteEx is overloaded in FPC -A -W
Result := ShellExecuteEx(@Info);
{$IFNDEF TYPEDADDRESS_ON}
{$TYPEDADDRESS OFF}
{$ENDIF ~TYPEDADDRESS_ON}
end;
function DisplayPropDialog(const Handle: THandle; Item: PItemIdList): Boolean;
var
Info: TShellExecuteInfo;
begin
ResetMemory(Info, SizeOf(Info));
with Info do
begin
cbSize := SizeOf(Info);
nShow := SW_SHOW;
lpIDList := Item;
fMask := SEE_MASK_INVOKEIDLIST or SEE_MASK_IDLIST;
Wnd := Handle;
lpVerb := cVerbProperties;
end;
{$TYPEDADDRESS ON}
Result := ShellExecuteEx(@Info);
{$IFNDEF TYPEDADDRESS_ON}
{$TYPEDADDRESS OFF}
{$ENDIF ~TYPEDADDRESS_ON}
end;
// Window procedure for the callback window created by DisplayContextMenu.
// It simply forwards messages to the folder. If you don't do this then the
// system created submenu's will be empty (except for 1 stub item!)
// note: storing the IContextMenu2 pointer in the window's user data was
// 'inspired' by (read: copied from) code by Brad Stowers.
function MenuCallback(Wnd: THandle; Msg: UINT; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
var
ContextMenu2: IContextMenu2;
begin
case Msg of
WM_CREATE:
begin
ContextMenu2 := IContextMenu2(PCreateStruct(lParam).lpCreateParams);
SetWindowLongPtr(Wnd, GWLP_USERDATA, LONG_PTR(ContextMenu2));
Result := DefWindowProc(Wnd, Msg, wParam, lParam);
end;
WM_INITMENUPOPUP:
begin
ContextMenu2 := IContextMenu2(GetWindowLongPtr(Wnd, GWLP_USERDATA));
ContextMenu2.HandleMenuMsg(Msg, wParam, lParam);
Result := 0;
end;
WM_DRAWITEM, WM_MEASUREITEM:
begin
ContextMenu2 := IContextMenu2(GetWindowLongPtr(Wnd, GWLP_USERDATA));
ContextMenu2.HandleMenuMsg(Msg, wParam, lParam);
Result := 1;
end;
else
Result := DefWindowProc(Wnd, Msg, wParam, lParam);
end;
end;
// Helper function for DisplayContextMenu, creates the callback window.
function CreateMenuCallbackWnd(const ContextMenu: IContextMenu2): THandle;
const
IcmCallbackWnd = 'ICMCALLBACKWND';
var
WndClass: TWndClass;
begin
ResetMemory(WndClass, SizeOf(WndClass));
WndClass.lpszClassName := PChar(IcmCallbackWnd);
WndClass.lpfnWndProc := @MenuCallback;
WndClass.hInstance := HInstance;
{$IFDEF HAS_UNITSCOPE}Winapi.{$ENDIF}Windows.RegisterClass(WndClass);
Result := CreateWindow(IcmCallbackWnd, IcmCallbackWnd, WS_POPUPWINDOW, 0,
0, 0, 0, 0, 0, HInstance, Pointer(ContextMenu));
end;
function DisplayContextMenuPidl(const Handle: THandle; const Folder: IShellFolder;
Item: PItemIdList; Pos: TPoint): Boolean;
var
Cmd: Cardinal;
ContextMenu: IContextMenu;
ContextMenu2: IContextMenu2;
Menu: HMENU;
CommandInfo: TCMInvokeCommandInfo;
CallbackWindow: THandle;
begin
Result := False;
if (Item = nil) or (Folder = nil) then
Exit;
Folder.GetUIObjectOf(Handle, 1, Item, IID_IContextMenu, nil,
Pointer(ContextMenu));
if ContextMenu <> nil then
begin
Menu := CreatePopupMenu;
if Menu <> 0 then
begin
if Succeeded(ContextMenu.QueryContextMenu(Menu, 0, 1, $7FFF, CMF_EXPLORE)) then
begin
CallbackWindow := 0;
if Succeeded(ContextMenu.QueryInterface(IContextMenu2, ContextMenu2)) then
begin
CallbackWindow := CreateMenuCallbackWnd(ContextMenu2);
end;
ClientToScreen(Handle, Pos);
Cmd := Cardinal(TrackPopupMenu(Menu, TPM_LEFTALIGN or TPM_LEFTBUTTON or
TPM_RIGHTBUTTON or TPM_RETURNCMD, Pos.X, Pos.Y, 0, CallbackWindow, nil));
if Cmd <> 0 then
begin
ResetMemory(CommandInfo, SizeOf(CommandInfo));
CommandInfo.cbSize := SizeOf(TCMInvokeCommandInfo);
CommandInfo.hwnd := Handle;
CommandInfo.lpVerb := MakeIntResourceA(Cmd - 1);
CommandInfo.nShow := SW_SHOWNORMAL;
Result := Succeeded(ContextMenu.InvokeCommand(CommandInfo));
end;
if CallbackWindow <> 0 then
DestroyWindow(CallbackWindow);
end;
DestroyMenu(Menu);
end;
end;
end;
function DisplayContextMenu(const Handle: THandle; const FileName: string;
Pos: TPoint): Boolean;
var
ItemIdList: PItemIdList;
Folder: IShellFolder;
begin
Result := False;
ItemIdList := PathToPidlBind(FileName, Folder);
if ItemIdList <> nil then
begin
Result := DisplayContextMenuPidl(Handle, Folder, ItemIdList, Pos);
PidlFree(ItemIdList);
end;
end;
function OpenFolder(const Path: string; Parent: THandle; Explore: Boolean): Boolean;
var
Sei: TShellExecuteInfo;
begin
Result := False;
if IsDirectory(Path) then
begin
ResetMemory(Sei, SizeOf(Sei));
with Sei do
begin
cbSize := SizeOf(Sei);
Wnd := Parent;
if Explore then
lpVerb := cVerbExplore
else
lpVerb := cVerbOpen;
lpFile := PChar(Path);
nShow := SW_SHOWNORMAL;
end;
{$TYPEDADDRESS ON}
Result := ShellExecuteEx(@Sei);
{$IFNDEF TYPEDADDRESS_ON}
{$TYPEDADDRESS OFF}
{$ENDIF ~TYPEDADDRESS_ON}
end;
end;
function OpenSpecialFolder(FolderID: Integer; Parent: THandle; Explore: Boolean): Boolean;
var
Pidl: PItemIDList;
Sei: TShellExecuteInfo;
begin
Result := False;
if Succeeded(SHGetSpecialFolderLocation(Parent, FolderID, Pidl)) then
begin
ResetMemory(Sei, SizeOf(Sei));
with Sei do
begin
cbSize := SizeOf(Sei);
Wnd := Parent;
fMask := SEE_MASK_INVOKEIDLIST;
if Explore then
lpVerb := cVerbExplore
else
lpVerb := cVerbOpen;
lpIDList := Pidl;
nShow := SW_SHOWNORMAL;
if PidlToPath(Pidl) = '' then
begin
fMask := SEE_MASK_INVOKEIDLIST;
lpIDList := Pidl;
end
else
lpFile := PChar(PidlToPath(Pidl));
end;
{$TYPEDADDRESS ON}
Result := ShellExecuteEx(@Sei);
{$IFNDEF TYPEDADDRESS_ON}
{$TYPEDADDRESS OFF}
{$ENDIF ~TYPEDADDRESS_ON}
CoTaskMemFree(Pidl);
end;
end;
//=== Memory Management ======================================================
function SHAllocMem(out P: Pointer; Count: Integer): Boolean;
var
Malloc: IMalloc;
begin
Result := False;
P := nil;
if Succeeded(SHGetMalloc(Malloc)) then
begin
P := Malloc.Alloc(Count);
if P <> nil then
begin
ResetMemory(P^, Count);
Result := True;
end;
end;
end;
function SHFreeMem(var P: Pointer): Boolean;
var
Malloc: IMalloc;
begin
Result := False;
if P <> nil then
begin
if Succeeded(SHGetMalloc(Malloc)) and (Malloc.DidAlloc(P) > 0) then
begin
Malloc.Free(P);
P := nil;
Result := True;
end;
end;
end;
function SHGetMem(var P: Pointer; Count: Integer): Boolean;
var
Malloc: IMalloc;
begin
Result := False;
if Succeeded(SHGetMalloc(Malloc)) then
begin
P := Malloc.Alloc(Count);
if P <> nil then
Result := True;
end;
end;
function SHReallocMem(var P: Pointer; Count: Integer): Boolean;
var
Malloc: IMalloc;
begin
Result := False;
if Succeeded(SHGetMalloc(Malloc)) then
begin
if (P <> nil) and (Malloc.DidAlloc(P) <= 0) then
Exit;
P := Malloc.ReAlloc(P, Count);
Result := (P <> nil) or (Count = 0);
end;
end;
//=== Paths and PIDLs ========================================================
function DriveToPidlBind(const DriveName: string; out Folder: IShellFolder): PItemIdList;
var
Attr: ULONG;
Eaten: ULONG;
DesktopFolder: IShellFolder;
Drives: PItemIdList;
Path: TWidePathPtr;
begin
Result := nil;
if Succeeded(SHGetDesktopFolder(DesktopFolder)) then
begin
if Succeeded(SHGetSpecialFolderLocation(0, CSIDL_DRIVES, Drives)) then
begin
if Succeeded(DesktopFolder.BindToObject(Drives, nil, IID_IShellFolder,
Pointer(Folder))) then
begin
{$IFDEF SUPPORTS_UNICODE}
Path := PChar(PathAddSeparator(DriveName));
{$ELSE ~SUPPORTS_UNICODE}
MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, PAnsiChar(PathAddSeparator(DriveName)), -1, Path, MAX_PATH);
{$ENDIF ~SUPPORTS_UNICODE}
Attr := 0;
if Failed(Folder.ParseDisplayName(0, nil, Path, Eaten, Result, Attr)) then
begin
Folder := nil;
// Failure probably means that this is not a drive. However, do not
// call PathToPidlBind() because it may cause infinite recursion.
end;
end;
end;
CoTaskMemFree(Drives);
end;
end;
function PathToPidl(const Path: string; Folder: IShellFolder): PItemIdList;
var
DesktopFolder: IShellFolder;
CharsParsed, Attr: ULONG;
WidePath: TWidePathPtr;
begin
Result := nil;
{$IFDEF SUPPORTS_UNICODE}
WidePath := PChar(Path);
{$ELSE ~SUPPORTS_UNICODE}
MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, PAnsiChar(Path), -1, WidePath, MAX_PATH);
{$ENDIF ~SUPPORTS_UNICODE}
Attr := 0;
if Folder <> nil then
Folder.ParseDisplayName(0, nil, WidePath, CharsParsed, Result, Attr)
else
if Succeeded(SHGetDesktopFolder(DesktopFolder)) then
DesktopFolder.ParseDisplayName(0, nil, WidePath, CharsParsed, Result, Attr);
end;
function PathToPidlBind(const FileName: string; out Folder: IShellFolder): PItemIdList;
var
Attr, Eaten: ULONG;
PathIdList: PItemIdList;
DesktopFolder: IShellFolder;
Path, ItemName: TWidePathPtr;
begin
Result := nil;
{$IFDEF SUPPORTS_UNICODE}
Path := PChar(ExtractFilePath(FileName));
ItemName := PChar(ExtractFileName(FileName));
{$ELSE ~SUPPORTS_UNICODE}
MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, PAnsiChar(ExtractFilePath(FileName)), -1, Path, MAX_PATH);
MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, PAnsiChar(ExtractFileName(FileName)), -1, ItemName, MAX_PATH);
{$ENDIF ~SUPPORTS_UNICODE}
if Succeeded(SHGetDesktopFolder(DesktopFolder)) then
begin
Attr := 0;
if Succeeded(DesktopFolder.ParseDisplayName(0, nil, Path, Eaten, PathIdList,
Attr)) then
begin
if Succeeded(DesktopFolder.BindToObject(PathIdList, nil, IID_IShellFolder,
Pointer(Folder))) then
begin
if Failed(Folder.ParseDisplayName(0, nil, ItemName, Eaten, Result, Attr)) then
begin
Folder := nil;
Result := DriveToPidlBind(FileName, Folder);
end;
end;
PidlFree(PathIdList);
end
else
Result := DriveToPidlBind(FileName, Folder);
end;
end;
function PidlBindToParent(IdList: PItemIdList; out Folder: IShellFolder; out Last: PItemIdList): Boolean;
var
Path: string;
begin
Last := nil;
Path := PidlToPath(IdList);
Last := PathToPidlBind(Path, Folder);
Result := Last <> nil;
if Last = nil then
Folder := nil;
end;
function PidlCompare(Pidl1, Pidl2: PItemIdList): Boolean;
var
L: Integer;
begin
Result := False;
L := PidlGetLength(Pidl1);
if L = PidlGetLength(Pidl2) then
Result := CompareMem(Pidl1, Pidl2, L);
end;
function PidlCopy(Source: PItemIdList; out Dest: PItemIdList): Boolean;
var
L: Integer;
begin
Result := False;
Dest := Source;
if Source <> nil then
begin
L := PidlGetLength(Source) + 2;
if SHAllocMem(Pointer(Dest), L) then
begin
Move(Source^, Dest^, L);
Result := True;
end;
end;
end;
function PidlFree(var IdList: PItemIdList): Boolean;
var
Malloc: IMalloc;
begin
Result := False;
if IdList = nil then
Result := True
else
begin
Malloc := nil;
if Succeeded(SHGetMalloc(Malloc)) and (Malloc.DidAlloc(IdList) > 0) then
begin
Malloc.Free(IdList);
IdList := nil;
Result := True;
end;
end;
end;
function PidlGetDepth(Pidl: PItemIdList): Integer;
var
P: PItemIdList;
begin
Result := 0;
if Pidl <> nil then
begin
P := Pidl;
while (P^.mkId.cb <> 0) and (Result < MAX_PATH) do
begin
Inc(Result);
P := PItemIdList(@P^.mkId.abID[P^.mkId.cb - 2]);
end;
end;
if Result = MAX_PATH then
Result := -1;
end;
function PidlGetLength(Pidl: PItemIdList): Integer;
var
P: PItemIdList;
I: Integer;
begin
Result := 0;
if Pidl <> nil then
begin
I := 0;
P := Pidl;
while (P^.mkId.cb <> 0) and (I < MAX_PATH) do
begin
Inc(I);
Inc(Result, P^.mkId.cb);
P := PItemIdList(@P^.mkId.abID[P^.mkId.cb - 2]);
end;
if I = MAX_PATH then
Result := -1;
end;
end;
function PidlGetNext(Pidl: PItemIdList): PItemIdList;
begin
Result := nil;
if (Pidl <> nil) and (Pidl^.mkid.cb <> 0) then
begin
Result := PItemIdList(@Pidl^.mkId.abID[Pidl^.mkId.cb - 2]);
if Result^.mkid.cb = 0 then