-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnumObj.cpp
352 lines (278 loc) · 9.7 KB
/
EnumObj.cpp
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
/* Logic to traverse the System Shell*/
#include "stdafx.h"
#include "grasp95.h"
#include "shlobj.h"
#include "EnumObj.h"
#include "EnumObj2.h"
#include "ShellApi.h"
// Global pointer to the shell's IMalloc interface.
LPMALLOC g_pMalloc;
// main - the application's entrypoint function
int ProcessShell()
{
LPITEMIDLIST pidlPrograms;
LPSHELLFOLDER pFolder;
// Get the shell's allocator.
if (!SUCCEEDED(SHGetMalloc(&g_pMalloc)))
return 1;
// Get the PIDL for the Programs folder.
if (SUCCEEDED(SHGetSpecialFolderLocation(NULL,
CSIDL_DRIVES, &pidlPrograms))) {
// Start with the desktop folder.
if (SUCCEEDED(SHGetDesktopFolder(&pFolder))) {
LPITEMIDLIST pidl;
// Process each item identifier in the list.
for (pidl = pidlPrograms; pidl != NULL;
pidl = GetNextItemID(pidl)) {
STRRET sName;
LPSHELLFOLDER pSubFolder;
LPITEMIDLIST pidlCopy;
// Copy the item identifier to a list by itself.
if ((pidlCopy = CopyItemID(pidl)) == NULL)
break;
// Display the name of the subfolder.
if (SUCCEEDED(pFolder->GetDisplayNameOf(pidlCopy, SHGDN_INFOLDER, &sName)))
PrintStrRet(pidlCopy, &sName);
// Bind to the subfolder.
if (!SUCCEEDED(pFolder->BindToObject(pidlCopy, NULL, IID_IShellFolder, (void **)&pSubFolder)))
{
g_pMalloc->Free(pidlCopy);
break;
}
// Free the copy of the item identifier.
g_pMalloc->Free(pidlCopy);
// Release the parent folder and point to the
// subfolder.
pFolder->Release();
pFolder = pSubFolder;
}
IdentifyDrives(pFolder, pidlPrograms);
// Release the last folder that was bound to.
if (pFolder != NULL)
pFolder->Release();
}
// Free the PIDL for the Programs folder.
g_pMalloc->Free(pidlPrograms);
}
// Release the shell's allocator.
g_pMalloc->Release();
return 0;
}
// GetNextItemID - points to the next element in an item identifier// list.
// Returns a PIDL if successful or NULL if at the end of the list.
// pdil - previous element
LPITEMIDLIST GetNextItemID(LPITEMIDLIST pidl)
{
// Get the size of the specified item identifier.
int cb = pidl->mkid.cb;
// If the size is zero, it is the end of the list.
if (cb == 0)
return NULL;
// Add cb to pidl (casting to increment by bytes).
pidl = (LPITEMIDLIST) (((LPBYTE) pidl) + cb);
// Return NULL if it is null-terminating or a pidl otherwise.
return (pidl->mkid.cb == 0) ? NULL : pidl;
}
// CopyItemID - creates an item identifier list containing the first
// item identifier in the specified list.
// Returns a PIDL if successful or NULL if out of memory.
LPITEMIDLIST CopyItemID(LPITEMIDLIST pidl)
{
// Get the size of the specified item identifier.
int cb = pidl->mkid.cb;
// Allocate a new item identifier list.
LPITEMIDLIST pidlNew = (LPITEMIDLIST)
g_pMalloc->Alloc(cb + sizeof(USHORT));
if (pidlNew == NULL)
return NULL;
// Copy the specified item identifier.
CopyMemory(pidlNew, pidl, cb);
// Append a terminating zero.
*((USHORT *) (((LPBYTE) pidlNew) + cb)) = 0;
return pidlNew;
}
// PrintStrRet - prints the contents of a STRRET structure.
// pidl - PIDL containing the display name if STRRET_OFFSET
// lpStr - address of the STRRET structure
void PrintStrRet(LPITEMIDLIST pidl, LPSTRRET lpStr)
{
LPSTR lpsz;
int cch;
switch (lpStr->uType) {
case STRRET_WSTR:
cch = WideCharToMultiByte(CP_OEMCP, WC_DEFAULTCHAR,
lpStr->pOleStr, -1, NULL, 0, NULL, NULL);
lpsz = (LPSTR) g_pMalloc->Alloc(cch);
if (lpsz != NULL) {
WideCharToMultiByte(CP_OEMCP, WC_DEFAULTCHAR,
lpStr->pOleStr, -1, lpsz, cch, NULL, NULL);
printf("%s\n", lpsz);
g_pMalloc->Free(lpsz);
}
break;
case STRRET_OFFSET:
printf("%s\n", ((char *) pidl) + lpStr->uOffset);
break;
case STRRET_CSTR:
printf("%s\n", lpStr->cStr);
break;
}
}
void IdentifyDrives(LPSHELLFOLDER lpShellFolder,
LPITEMIDLIST lpifq)
{
LPSHELLFOLDER lpsf2=0;
LPENUMIDLIST lpEnumList=0;
LPITEMIDLIST lpItemList=0, lpiTemp=0, lpifqThisItem;
LPCITEMIDLIST lpcItem;
LPMALLOC lpMalloc=0;
unsigned long ulFetched;
HRESULT hr;
_TCHAR szBuff[MAX_PATH];
int iSmallIcon, iBigIcon;
hr = SHGetMalloc(&lpMalloc);
if (FAILED(hr))
return;
if (SUCCEEDED(lpShellFolder->EnumObjects(NULL,
SHCONTF_FOLDERS | SHCONTF_NONFOLDERS,
&lpEnumList)))
{
int xDriveCnt = 0;
while (S_OK == lpEnumList->Next(1, &lpItemList, &ulFetched))
{
//Create a fully qualified path to the current item
//The SH* shell api's take a fully qualified path pidl,
//(see GetIcon above where I call SHGetFileInfo) whereas the
//interface methods take a relative path pidl.
ULONG ulAttrs = SFGAO_HASSUBFOLDER | SFGAO_FOLDER | SFGAO_FILESYSTEM;
lpcItem = lpItemList;
lpShellFolder->GetAttributesOf(1, &lpcItem, &ulAttrs);
if (ulAttrs & (SFGAO_FILESYSTEM))
{
//Now get the friendly name that we'll put in the treeview...
GetName(lpShellFolder, lpItemList, SHGDN_NORMAL, szBuff);
lpifqThisItem = ConcatPidls(lpifq, lpItemList);
GetNormalAndSelectedIcons(lpifqThisItem, &iSmallIcon, &iBigIcon);
TRACE(_T("%s, %d, %d\n"), szBuff, iSmallIcon, iBigIcon);
g_strDriveNames.Add(szBuff);
g_naSmallDriveIcons[xDriveCnt] = iSmallIcon;
g_naBigDriveIcons[xDriveCnt] = iBigIcon;
xDriveCnt++;
}
}
}
}
BOOL GetName(LPSHELLFOLDER lpsf,
LPITEMIDLIST lpi,
DWORD dwFlags,
LPTSTR lpFriendlyName)
{
BOOL bSuccess=TRUE;
STRRET str;
if (NOERROR==lpsf->GetDisplayNameOf(lpi, dwFlags, &str))
{
switch (str.uType)
{
case STRRET_WSTR:
WideCharToMultiByte(CP_ACP, // CodePage
0, // dwFlags
str.pOleStr, // lpWideCharStr
-1, // cchWideChar
lpFriendlyName, // lpMultiByteStr
MAX_PATH, // cchMultiByte,
NULL, // lpDefaultChar,
NULL); // lpUsedDefaultChar
break;
case STRRET_OFFSET:
lstrcpy(lpFriendlyName, (LPTSTR)lpi+str.uOffset);
break;
case STRRET_CSTR:
lstrcpy(lpFriendlyName, (LPTSTR)str.cStr);
break;
default:
bSuccess = FALSE;
break;
}
}
else
bSuccess = FALSE;
return bSuccess;
}
void GetNormalAndSelectedIcons(LPITEMIDLIST lpifq,
int* icon1, int* icon2)
{
//Note that we don't check the return value here because if GetIcon()
//fails, then we're in big trouble...
SHFILEINFO sfi;
SHGetFileInfo((LPCTSTR)lpifq,
0,
&sfi,
sizeof(SHFILEINFO),
SHGFI_PIDL |
SHGFI_SYSICONINDEX |
SHGFI_SMALLICON);
*icon1 = sfi.iIcon;
SHGetFileInfo((LPCTSTR)lpifq,
0,
&sfi,
sizeof(SHFILEINFO),
SHGFI_PIDL |
SHGFI_SYSICONINDEX |
SHGFI_OPENICON |
SHGFI_SMALLICON);
*icon2 = sfi.iIcon;
return;
}
LPITEMIDLIST ConcatPidls(LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2)
{
LPITEMIDLIST pidlNew;
UINT cb1;
UINT cb2;
if (pidl1) //May be NULL
cb1 = GetSize(pidl1) - sizeof(pidl1->mkid.cb);
else
cb1 = 0;
cb2 = GetSize(pidl2);
pidlNew = Create(cb1 + cb2);
if (pidlNew)
{
if (pidl1)
memcpy(pidlNew, pidl1, cb1);
memcpy(((LPSTR)pidlNew) + cb1, pidl2, cb2);
}
return pidlNew;
}
LPITEMIDLIST Next(LPCITEMIDLIST pidl)
{
LPSTR lpMem=(LPSTR)pidl;
lpMem+=pidl->mkid.cb;
return (LPITEMIDLIST)lpMem;
}
UINT GetSize(LPCITEMIDLIST pidl)
{
UINT cbTotal = 0;
if (pidl)
{
cbTotal += sizeof(pidl->mkid.cb); // Null terminator
while (pidl->mkid.cb)
{
cbTotal += pidl->mkid.cb;
pidl = Next(pidl);
}
}
return cbTotal;
}
LPITEMIDLIST Create(UINT cbSize)
{
LPMALLOC lpMalloc;
HRESULT hr;
LPITEMIDLIST pidl=0;
hr=SHGetMalloc(&lpMalloc);
if (FAILED(hr))
return 0;
pidl=(LPITEMIDLIST)lpMalloc->Alloc(cbSize);
if (pidl)
memset(pidl, 0, cbSize); // zero-init for external task alloc
if (lpMalloc) lpMalloc->Release();
return pidl;
}