-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshellapi.cpp
304 lines (239 loc) · 5.16 KB
/
shellapi.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
#include "StdAfx.h"
#include <shlobj.h>
#include <objbase.h>
/*
*
*
*/
IMalloc* GetMalloc()
{
static IMalloc* g_pMalloc = 0;
if ( g_pMalloc==0 )
::SHGetMalloc(&g_pMalloc);
return g_pMalloc;
}
/*
*
*
*/
IShellFolder* GetDesktopFolder()
{
static IShellFolder* g_pDesktopFolder = 0;
if ( g_pDesktopFolder==0 )
::SHGetDesktopFolder(&g_pDesktopFolder);
return g_pDesktopFolder;
}
/*
*
*
*/
HRESULT StrRetToStr(STRRET StrRet, LPTSTR* str, LPITEMIDLIST pidl)
{
HRESULT hr = S_OK;
int cch;
LPSTR strOffset;
*str = NULL; // Assume failure
switch (StrRet.uType)
{
case STRRET_WSTR:
cch = WideCharToMultiByte(CP_ACP, 0, StrRet.pOleStr, -1, NULL, 0, NULL, NULL);
*str = (LPTSTR)GetMalloc()->Alloc(cch * sizeof(TCHAR));
if (*str != NULL)
WideCharToMultiByte(CP_ACP, 0, StrRet.pOleStr, -1, *str, cch, NULL, NULL);
else
hr = E_FAIL;
break;
case STRRET_OFFSET:
strOffset = (((char *) pidl) + StrRet.uOffset);
cch = lstrlen(strOffset) + 1; // NULL terminator
*str = (LPTSTR)GetMalloc()->Alloc(cch * sizeof(TCHAR));
if (*str != NULL)
strcpy(*str, strOffset);
else
hr = E_FAIL;
break;
case STRRET_CSTR:
cch = lstrlen(StrRet.cStr) + 1; // NULL terminator
*str = (LPTSTR)GetMalloc()->Alloc(cch * sizeof(TCHAR));
if (*str != NULL)
strcpy(*str, StrRet.cStr);
else
hr = E_FAIL;
break;
}
return hr;
}
/*
*
*
*/
int GetItemIDCount(LPCITEMIDLIST pidl)
{
if ( !pidl )
return 0;
int nCount = 0;
BYTE* pCur = (BYTE*)pidl;
while ( ((LPCITEMIDLIST)pCur)->mkid.cb )
{
nCount ++;
pCur += ((LPCITEMIDLIST)pCur)->mkid.cb;
}
return nCount;
}
/*
* get size of PIDL - returns PIDL size (even if it's composite
* PIDL - i.e. composed from more than one PIDLs)
*
*/
int GetItemIDSize(LPCITEMIDLIST pidl)
{
if ( !pidl )
return 0;
int nSize = 0;
while( pidl->mkid.cb )
{
nSize += pidl->mkid.cb;
pidl = (LPCITEMIDLIST)(((LPBYTE)pidl) + pidl->mkid.cb);
}
return nSize;
}
/*
*
*
*/
LPITEMIDLIST CopyItemID(LPCITEMIDLIST pidl, int cb=-1)
{
if ( GetMalloc()==0 )
return NULL;
// Get the size of the specified item identifier.
if ( cb == -1 )
cb = GetItemIDSize(pidl);
// Allocate a new item identifier list.
LPITEMIDLIST pidlNew = (LPITEMIDLIST)GetMalloc()->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;
}
/*
*
*
*/
LPBYTE GetItemIDPos(LPCITEMIDLIST pidl, int nPos)
{
if ( !pidl )
return 0;
int nCount = 0;
BYTE* pCur = (BYTE*)pidl;
while( ((LPCITEMIDLIST)pCur)->mkid.cb )
{
if( nCount == nPos )
return pCur;
nCount ++;
pCur += ((LPCITEMIDLIST)pCur)->mkid.cb;// + sizeof(pidl->mkid.cb);
}
if( nCount == nPos )
return pCur;
return NULL;
}
/*
*
*
*/
HRESULT _SHBindToParent(LPCITEMIDLIST pidl, REFIID riid, VOID **ppv, LPCITEMIDLIST *ppidlLast)
{
HRESULT hr;
if ( !pidl || !ppv )
return E_POINTER;
int nCount = ::GetItemIDCount(pidl);
if ( nCount == 0 )
{
// this is desktop pidl or invalid PIDL
return E_POINTER;
}
if ( nCount == 1 )
{
// this is desktop item
if ( SUCCEEDED(hr = GetDesktopFolder()->QueryInterface(riid, ppv)) )
{
if ( ppidlLast )
*ppidlLast = ::CopyItemID(pidl);
}
return hr;
}
LPBYTE pRel = ::GetItemIDPos(pidl, nCount-1);
LPCITEMIDLIST pidlPar = ::CopyItemID(pidl, pRel-(LPBYTE)pidl);
IShellFolder* pFolder;
if ( FAILED(hr = GetDesktopFolder()->BindToObject(pidlPar, NULL, IID_IShellFolder, (void**)&pFolder)) )
{
return hr;
}
if ( SUCCEEDED(hr = pFolder->QueryInterface(riid, ppv)) )
{
if ( ppidlLast )
*ppidlLast = ::CopyItemID((LPCITEMIDLIST)pRel);
}
return hr;
}
/*
*
*
*/
BOOL GetShellItemName( LPCITEMIDLIST pidl, DWORD dwFlags, char* sDisplayName)
{
SHFILEINFO sfi;
IShellFolder* pFolder;
LPCITEMIDLIST pidlRel;
if ( SUCCEEDED(_SHBindToParent(pidl, IID_IShellFolder,(void**)&pFolder, &pidlRel)) )
{
STRRET str;
if ( SUCCEEDED(pFolder->GetDisplayNameOf(pidlRel, dwFlags, &str)) )
{
LPTSTR lpszName = NULL;
StrRetToStr(str, &lpszName, (LPITEMIDLIST)pidlRel);
lstrcpy(sDisplayName,lpszName);
}
}
else
if ( ::SHGetFileInfo((LPCTSTR)pidl, NULL, &sfi, sizeof(sfi), SHGFI_PIDL|SHGFI_DISPLAYNAME) )
{
lstrcpy( sDisplayName, sfi.szDisplayName );
}
else
{
return FALSE;
}
return TRUE;
}
/*
*
*
*/
LPITEMIDLIST MergeItemID(LPCITEMIDLIST pidl,...)
{
va_list marker;
int nSize = GetItemIDSize(pidl) + sizeof(pidl->mkid.cb);
LPITEMIDLIST p;
// count size of pidl
va_start(marker,pidl);
while ( p = va_arg(marker, LPITEMIDLIST) )
nSize += GetItemIDSize(p);
va_end(marker);
// allocate and merge pidls
LPITEMIDLIST pidlNew = (LPITEMIDLIST)GetMalloc()->Alloc(nSize);
if ( pidlNew == NULL )
return NULL;
va_start(marker,pidl);
CopyMemory(((LPBYTE)pidlNew), pidl, nSize = GetItemIDSize(pidl));
while( p = va_arg(marker, LPITEMIDLIST) )
{
CopyMemory(((LPBYTE)pidlNew) + nSize, p, GetItemIDSize(p));
nSize += p->mkid.cb;
}
va_end(marker);
*((USHORT *)(((LPBYTE) pidlNew) + nSize)) = 0;
return pidlNew;
}