-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMaxLogic.knownFolders.pas
More file actions
158 lines (127 loc) · 3.79 KB
/
Copy pathMaxLogic.knownFolders.pas
File metadata and controls
158 lines (127 loc) · 3.79 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
unit MaxLogic.KnownFolders;
interface
uses
windows, classes, sysUtils, generics.collections,
Winapi.ShlObj, ioUtils, ComObj, ActiveX,
MaxLogic.ioUtils;
type
// forward declaration
TKnownFolders = class;
TKnownFolderInfo = record
Id: TIID;
category: TKFCategory;
Path: string;
Name: string;
Description: string;
LocalizedName: String;
Function IdAsString: string;
end;
TKnownFolders = class
public
// FOR A LIST OF KNOWN FOLDER IDS SEE:
// https://msdn.microsoft.com/en-us/library/dd378457%28v=vs.85%29.aspx
class function Getpath(const Id: TIID; const Default: string): string;
class function GetDownloadsPath: string;
class function EnumerateKnownFolders: TArray<TKnownFolderInfo>;
end;
implementation
{ TKnownFolders }
class function TKnownFolders.EnumerateKnownFolders: TArray<TKnownFolderInfo>;
var
KnownFolderManager: IKnownFolderManager;
f: iKnownFolder;
hr: hResult;
KFId: array of TKnownFolderID;
pKFId: PKnownFolderID;
Count: UINT;
x: Integer;
a: TArray<TKnownFolderInfo>;
df: TKnownFolderDefinition;
i: Integer;
pp: LPWSTR;
begin
result := nil;
hr := CoCreateInstance(CLSID_KnownFolderManager, nil, CLSCTX_ALL, IKnownFolderManager, KnownFolderManager);
if (hr <> S_OK) or (KnownFolderManager = nil) then
exit;
// When this method returns, contains a pointer to an array of all KNOWNFOLDERID values registered with the system. Use CoTaskMemFree to free these resources when they are no longer needed.
hr := KnownFolderManager.GetFolderIds(pKFId, Count);
if hr <> S_OK then
exit;
setLength(KFId, Count);
for x := 0 to Count - 1 do
begin
pKFId[x] := pKFId^;
Inc(pKFId, SizeOf(TKnownFolderID));
end;
setLength(a, Count);
i := 0;
for x := 0 to Count - 1 do
begin
hr := KnownFolderManager.getfolder(KFId[x], f);
if hr <> S_OK then
continue;
hr := f.GetFolderDefinition(df);
if hr = S_OK then
begin
// When this method returns, contains the address of a pointer to a null-terminated buffer that contains the path. The calling application is responsible for calling CoTaskMemFree to free this resource when it is no longer needed.
hr := f.Getpath(0, pp);
if hr = S_OK then
begin
a[i].Path := pp;
a[i].Id := KFId[x];
a[x].category := df.category;
a[x].Name := df.pszName;
a[x].Description := df.pszDescription;
a[x].LocalizedName := df.pszLocalizedName;
Inc(i);
CoTaskMemFree(pp);
end;
FreeKnownFolderDefinitionFields(df);
end;
end;
setLength(a, i);
result := a;
CoTaskMemFree(KFId);
end;
class function TKnownFolders.GetDownloadsPath: string;
var
Default: string;
FOLDERID_Downloads: TGUID;
begin
default := MaxLogic.ioUtils.GetSpecialFolderPath(sfkCurUser_My_Documents);
FOLDERID_Downloads := StringToGuid('{374DE290-123F-4565-9164-39C4925E467B}');
result := Getpath(
FOLDERID_Downloads,
default);
result := IncludeTrailingPathDelimiter(result);
end;
class
function TKnownFolders.Getpath(const Id: TIID; const Default: string): string;
var
KnownFolderManager: IKnownFolderManager;
f: iKnownFolder;
hr: hResult;
pp: LPWSTR;
begin
result := default;
hr := CoCreateInstance(CLSID_KnownFolderManager, nil, CLSCTX_ALL, IKnownFolderManager, KnownFolderManager);
if (hr <> S_OK) or (KnownFolderManager = nil) then
exit;
hr := KnownFolderManager.getfolder(Id, f);
if (hr <> S_OK) or (f = nil) then
exit;
hr := f.Getpath(0, pp);
if hr = S_OK then
begin
result := pp;
CoTaskMemFree(pp);
end;
end;
{ TKnownFolderInfo }
function TKnownFolderInfo.IdAsString: string;
begin
// You can use StringFromCLSID or StringFromGUID2 to convert the retrieved KNOWNFOLDERID values to strings.
result := GuidToString(Id);
end;
end.