Sync with trunk r63502.
[reactos.git] / dll / win32 / shell32 / pidl.cpp
1 /*
2 * pidl Handling
3 *
4 * Copyright 1998 Juergen Schmied
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 *
20 * NOTES
21 * a pidl == NULL means desktop and is legal
22 *
23 */
24
25 #include "precomp.h"
26
27 WINE_DEFAULT_DEBUG_CHANNEL(pidl);
28 WINE_DECLARE_DEBUG_CHANNEL(shell);
29
30 /* from comctl32.dll */
31 EXTERN_C LPVOID WINAPI Alloc(INT);
32 EXTERN_C BOOL WINAPI Free(LPVOID);
33
34 static LPSTR _ILGetSTextPointer(LPCITEMIDLIST pidl);
35 static LPWSTR _ILGetTextPointerW(LPCITEMIDLIST pidl);
36
37 /*************************************************************************
38 * ILGetDisplayNameExA [SHELL32.186]
39 *
40 * Retrieves the display name of an ItemIDList
41 *
42 * PARAMS
43 * psf [I] Shell Folder to start with, if NULL the desktop is used
44 * pidl [I] ItemIDList relative to the psf to get the display name for
45 * path [O] Filled in with the display name, assumed to be at least MAX_PATH long
46 * type [I] Type of display name to retrieve
47 * 0 = SHGDN_FORPARSING | SHGDN_FORADDRESSBAR uses always the desktop as root
48 * 1 = SHGDN_NORMAL relative to the root folder
49 * 2 = SHGDN_INFOLDER relative to the root folder, only the last name
50 *
51 * RETURNS
52 * True if the display name could be retrieved successfully, False otherwise
53 */
54 static BOOL ILGetDisplayNameExA(LPSHELLFOLDER psf, LPCITEMIDLIST pidl, LPSTR path, DWORD type)
55 {
56 BOOL ret = FALSE;
57 WCHAR wPath[MAX_PATH];
58
59 TRACE("%p %p %p %d\n", psf, pidl, path, type);
60
61 if (!pidl || !path)
62 return FALSE;
63
64 ret = ILGetDisplayNameExW(psf, pidl, wPath, type);
65 WideCharToMultiByte(CP_ACP, 0, wPath, -1, path, MAX_PATH, NULL, NULL);
66 TRACE("%p %p %s\n", psf, pidl, debugstr_a(path));
67
68 return ret;
69 }
70
71 BOOL WINAPI ILGetDisplayNameExW(LPSHELLFOLDER psf, LPCITEMIDLIST pidl, LPWSTR path, DWORD type)
72 {
73 CComPtr<IShellFolder> psfParent;
74 LPSHELLFOLDER lsf = psf;
75 HRESULT ret = NO_ERROR;
76 LPCITEMIDLIST pidllast;
77 STRRET strret;
78 DWORD flag;
79
80 TRACE("%p %p %p %d\n", psf, pidl, path, type);
81
82 if (!pidl || !path)
83 return FALSE;
84
85 if (!lsf)
86 {
87 ret = SHGetDesktopFolder(&lsf);
88 if (FAILED(ret))
89 return FALSE;
90 }
91
92 if (type <= 2)
93 {
94 switch (type)
95 {
96 case ILGDN_FORPARSING:
97 flag = SHGDN_FORPARSING | SHGDN_FORADDRESSBAR;
98 break;
99 case ILGDN_NORMAL:
100 flag = SHGDN_NORMAL;
101 break;
102 case ILGDN_INFOLDER:
103 flag = SHGDN_INFOLDER;
104 break;
105 default:
106 FIXME("Unknown type parameter = %x\n", type);
107 flag = SHGDN_FORPARSING | SHGDN_FORADDRESSBAR;
108 break;
109 }
110 if (!*(const WORD*)pidl || type == ILGDN_FORPARSING)
111 {
112 ret = lsf->GetDisplayNameOf(pidl, flag, &strret);
113 if (SUCCEEDED(ret))
114 {
115 if(!StrRetToStrNW(path, MAX_PATH, &strret, pidl))
116 ret = E_FAIL;
117 }
118 }
119 else
120 {
121 ret = SHBindToParent(pidl, IID_PPV_ARG(IShellFolder, &psfParent), &pidllast);
122 if (SUCCEEDED(ret))
123 {
124 ret = psfParent->GetDisplayNameOf(pidllast, flag, &strret);
125 if (SUCCEEDED(ret))
126 {
127 if(!StrRetToStrNW(path, MAX_PATH, &strret, pidllast))
128 ret = E_FAIL;
129 }
130 }
131 }
132 }
133
134 TRACE("%p %p %s\n", psf, pidl, debugstr_w(path));
135
136 if (!psf)
137 lsf->Release();
138 return SUCCEEDED(ret);
139 }
140
141 /*************************************************************************
142 * ILGetDisplayNameEx [SHELL32.186]
143 */
144 BOOL WINAPI ILGetDisplayNameEx(LPSHELLFOLDER psf, LPCITEMIDLIST pidl, LPVOID path, DWORD type)
145 {
146 TRACE_(shell)("%p %p %p %d\n", psf, pidl, path, type);
147
148 if (SHELL_OsIsUnicode())
149 return ILGetDisplayNameExW(psf, pidl, (LPWSTR)path, type);
150 return ILGetDisplayNameExA(psf, pidl, (LPSTR)path, type);
151 }
152
153 /*************************************************************************
154 * ILGetDisplayName [SHELL32.15]
155 */
156 BOOL WINAPI ILGetDisplayName(LPCITEMIDLIST pidl, LPVOID path)
157 {
158 TRACE_(shell)("%p %p\n", pidl, path);
159
160 if (SHELL_OsIsUnicode())
161 return ILGetDisplayNameExW(NULL, pidl, (LPWSTR)path, ILGDN_FORPARSING);
162 return ILGetDisplayNameExA(NULL, pidl, (LPSTR)path, ILGDN_FORPARSING);
163 }
164
165 /*************************************************************************
166 * ILFindLastID [SHELL32.16]
167 *
168 * NOTES
169 * observed: pidl=Desktop return=pidl
170 */
171 LPITEMIDLIST WINAPI ILFindLastID(LPCITEMIDLIST pidl)
172 {
173 LPCITEMIDLIST pidlLast = pidl;
174
175 TRACE("(pidl=%p)\n", pidl);
176
177 if (!pidl)
178 return NULL;
179
180 while (pidl->mkid.cb)
181 {
182 pidlLast = pidl;
183 pidl = ILGetNext(pidl);
184 }
185 return (LPITEMIDLIST)pidlLast;
186 }
187
188 /*************************************************************************
189 * ILRemoveLastID [SHELL32.17]
190 *
191 * NOTES
192 * when pidl=Desktop return=FALSE
193 */
194 BOOL WINAPI ILRemoveLastID(LPITEMIDLIST pidl)
195 {
196 TRACE_(shell)("pidl=%p\n", pidl);
197
198 if (!pidl || !pidl->mkid.cb)
199 return 0;
200 ILFindLastID(pidl)->mkid.cb = 0;
201 return 1;
202 }
203
204 /*************************************************************************
205 * ILClone [SHELL32.18]
206 *
207 * NOTES
208 * duplicate an idlist
209 */
210 LPITEMIDLIST WINAPI ILClone (LPCITEMIDLIST pidl)
211 {
212 DWORD len;
213 LPITEMIDLIST newpidl;
214
215 if (!pidl)
216 return NULL;
217
218 len = ILGetSize(pidl);
219 newpidl = (LPITEMIDLIST)SHAlloc(len);
220 if (newpidl)
221 memcpy(newpidl, pidl, len);
222
223 TRACE("pidl=%p newpidl=%p\n", pidl, newpidl);
224 pdump(pidl);
225
226 return newpidl;
227 }
228
229 /*************************************************************************
230 * ILCloneFirst [SHELL32.19]
231 *
232 * NOTES
233 * duplicates the first idlist of a complex pidl
234 */
235 LPITEMIDLIST WINAPI ILCloneFirst(LPCITEMIDLIST pidl)
236 {
237 DWORD len;
238 LPITEMIDLIST pidlNew = NULL;
239
240 TRACE("pidl=%p\n", pidl);
241 pdump(pidl);
242
243 if (pidl)
244 {
245 len = pidl->mkid.cb;
246 pidlNew = (LPITEMIDLIST)SHAlloc(len + 2);
247 if (pidlNew)
248 {
249 memcpy(pidlNew, pidl, len + 2); /* 2 -> mind a desktop pidl */
250
251 if (len)
252 ILGetNext(pidlNew)->mkid.cb = 0x00;
253 }
254 }
255 TRACE("-- newpidl=%p\n", pidlNew);
256
257 return pidlNew;
258 }
259
260 /*************************************************************************
261 * ILLoadFromStream (SHELL32.26)
262 *
263 * NOTES
264 * the first two bytes are the len, the pidl is following then
265 */
266 HRESULT WINAPI ILLoadFromStream (IStream * pStream, LPITEMIDLIST * ppPidl)
267 {
268 WORD wLen = 0;
269 DWORD dwBytesRead;
270 HRESULT ret = E_FAIL;
271
272
273 TRACE_(shell)("%p %p\n", pStream , ppPidl);
274
275 SHFree(*ppPidl);
276 *ppPidl = NULL;
277
278 pStream->AddRef ();
279
280 if (SUCCEEDED(pStream->Read(&wLen, 2, &dwBytesRead)))
281 {
282 TRACE("PIDL length is %d\n", wLen);
283 if (wLen != 0)
284 {
285 *ppPidl = (LPITEMIDLIST)SHAlloc(wLen);
286 if (SUCCEEDED(pStream->Read(*ppPidl , wLen, &dwBytesRead)))
287 {
288 TRACE("Stream read OK\n");
289 ret = S_OK;
290 }
291 else
292 {
293 WARN("reading pidl failed\n");
294 SHFree(*ppPidl);
295 *ppPidl = NULL;
296 }
297 }
298 else
299 {
300 *ppPidl = NULL;
301 ret = S_OK;
302 }
303 }
304
305 /* we are not yet fully compatible */
306 if (*ppPidl && !pcheck(*ppPidl))
307 {
308 WARN("Check failed\n");
309 SHFree(*ppPidl);
310 *ppPidl = NULL;
311 }
312
313 pStream->Release ();
314 TRACE("done\n");
315 return ret;
316 }
317
318 /*************************************************************************
319 * ILSaveToStream (SHELL32.27)
320 *
321 * NOTES
322 * the first two bytes are the len, the pidl is following then
323 */
324 HRESULT WINAPI ILSaveToStream (IStream * pStream, LPCITEMIDLIST pPidl)
325 {
326 WORD wLen = 0;
327 HRESULT ret = E_FAIL;
328
329 TRACE_(shell)("%p %p\n", pStream, pPidl);
330
331 pStream->AddRef ();
332
333 wLen = ILGetSize(pPidl);
334
335 if (SUCCEEDED(pStream->Write(&wLen, 2, NULL)))
336 {
337 if (SUCCEEDED(pStream->Write(pPidl, wLen, NULL)))
338 ret = S_OK;
339 }
340 pStream->Release ();
341
342 return ret;
343 }
344
345 /*************************************************************************
346 * SHILCreateFromPath [SHELL32.28]
347 *
348 * Create an ItemIDList from a path
349 *
350 * PARAMS
351 * path [I]
352 * ppidl [O]
353 * attributes [I/O] requested attributes on call and actual attributes when
354 * the function returns
355 *
356 * RETURNS
357 * NO_ERROR if successful, or an OLE errer code otherwise
358 *
359 * NOTES
360 * Wrapper for IShellFolder_ParseDisplayName().
361 */
362 HRESULT WINAPI SHILCreateFromPathA(LPCSTR path, LPITEMIDLIST * ppidl, DWORD * attributes)
363 {
364 WCHAR lpszDisplayName[MAX_PATH];
365
366 TRACE_(shell)("%s %p 0x%08x\n", path, ppidl, attributes ? *attributes : 0);
367
368 if (!MultiByteToWideChar(CP_ACP, 0, path, -1, lpszDisplayName, MAX_PATH))
369 lpszDisplayName[MAX_PATH-1] = 0;
370
371 return SHILCreateFromPathW(lpszDisplayName, ppidl, attributes);
372 }
373
374 HRESULT WINAPI SHILCreateFromPathW(LPCWSTR path, LPITEMIDLIST * ppidl, DWORD * attributes)
375 {
376 CComPtr<IShellFolder> sf;
377 DWORD pchEaten;
378 HRESULT ret = E_FAIL;
379
380 TRACE_(shell)("%s %p 0x%08x\n", debugstr_w(path), ppidl, attributes ? *attributes : 0);
381
382 if (SUCCEEDED (SHGetDesktopFolder(&sf)))
383 ret = sf->ParseDisplayName(0, NULL, (LPWSTR)path, &pchEaten, ppidl, attributes);
384 return ret;
385 }
386
387 EXTERN_C HRESULT WINAPI SHILCreateFromPathAW (LPCVOID path, LPITEMIDLIST * ppidl, DWORD * attributes)
388 {
389 if ( SHELL_OsIsUnicode())
390 return SHILCreateFromPathW ((LPCWSTR)path, ppidl, attributes);
391 return SHILCreateFromPathA ((LPCSTR)path, ppidl, attributes);
392 }
393
394 /*************************************************************************
395 * SHCloneSpecialIDList [SHELL32.89]
396 *
397 * Create an ItemIDList to one of the special folders.
398
399 * PARAMS
400 * hwndOwner [in]
401 * nFolder [in] CSIDL_xxxxx
402 * fCreate [in] Create folder if it does not exist
403 *
404 * RETURNS
405 * Success: The newly created pidl
406 * Failure: NULL, if inputs are invalid.
407 *
408 * NOTES
409 * exported by ordinal.
410 * Caller is responsible for deallocating the returned ItemIDList with the
411 * shells IMalloc interface, aka ILFree.
412 */
413 LPITEMIDLIST WINAPI SHCloneSpecialIDList(HWND hwndOwner, int nFolder, BOOL fCreate)
414 {
415 LPITEMIDLIST ppidl;
416 TRACE_(shell)("(hwnd=%p,csidl=0x%x,%s).\n", hwndOwner, nFolder, fCreate ? "T" : "F");
417
418 if (fCreate)
419 nFolder |= CSIDL_FLAG_CREATE;
420
421 SHGetSpecialFolderLocation(hwndOwner, nFolder, &ppidl);
422 return ppidl;
423 }
424
425 /*************************************************************************
426 * ILGlobalClone [SHELL32.20]
427 *
428 * Clones an ItemIDList using Alloc.
429 *
430 * PARAMS
431 * pidl [I] ItemIDList to clone
432 *
433 * RETURNS
434 * Newly allocated ItemIDList.
435 *
436 * NOTES
437 * exported by ordinal.
438 */
439 LPITEMIDLIST WINAPI ILGlobalClone(LPCITEMIDLIST pidl)
440 {
441 DWORD len;
442 LPITEMIDLIST newpidl;
443
444 if (!pidl)
445 return NULL;
446
447 len = ILGetSize(pidl);
448 newpidl = (LPITEMIDLIST)Alloc(len);
449 if (newpidl)
450 memcpy(newpidl, pidl, len);
451
452 TRACE("pidl=%p newpidl=%p\n", pidl, newpidl);
453 pdump(pidl);
454
455 return newpidl;
456 }
457
458 /*************************************************************************
459 * ILIsEqual [SHELL32.21]
460 *
461 */
462 BOOL WINAPI ILIsEqual(LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2)
463 {
464 char szData1[MAX_PATH];
465 char szData2[MAX_PATH];
466
467 LPCITEMIDLIST pidltemp1 = pidl1;
468 LPCITEMIDLIST pidltemp2 = pidl2;
469
470 TRACE("pidl1=%p pidl2=%p\n", pidl1, pidl2);
471
472 /*
473 * Explorer reads from registry directly (StreamMRU),
474 * so we can only check here
475 */
476 if (!pcheck(pidl1) || !pcheck (pidl2))
477 return FALSE;
478
479 pdump (pidl1);
480 pdump (pidl2);
481
482 if (!pidl1 || !pidl2)
483 return FALSE;
484
485 while (pidltemp1->mkid.cb && pidltemp2->mkid.cb)
486 {
487 _ILSimpleGetText(pidltemp1, szData1, MAX_PATH);
488 _ILSimpleGetText(pidltemp2, szData2, MAX_PATH);
489
490 if (strcmp( szData1, szData2 ))
491 return FALSE;
492
493 pidltemp1 = ILGetNext(pidltemp1);
494 pidltemp2 = ILGetNext(pidltemp2);
495 }
496
497 if (!pidltemp1->mkid.cb && !pidltemp2->mkid.cb)
498 return TRUE;
499
500 return FALSE;
501 }
502
503 /*************************************************************************
504 * ILIsParent [SHELL32.23]
505 *
506 * Verifies that pidlParent is indeed the (immediate) parent of pidlChild.
507 *
508 * PARAMS
509 * pidlParent [I]
510 * pidlChild [I]
511 * bImmediate [I] only return true if the parent is the direct parent
512 * of the child
513 *
514 * RETURNS
515 * True if the parent ItemIDlist is a complete part of the child ItemIdList,
516 * False otherwise.
517 *
518 * NOTES
519 * parent = a/b, child = a/b/c -> true, c is in folder a/b
520 * child = a/b/c/d -> false if bImmediate is true, d is not in folder a/b
521 * child = a/b/c/d -> true if bImmediate is false, d is in a subfolder of a/b
522 */
523 BOOL WINAPI ILIsParent(LPCITEMIDLIST pidlParent, LPCITEMIDLIST pidlChild, BOOL bImmediate)
524 {
525 char szData1[MAX_PATH];
526 char szData2[MAX_PATH];
527 LPCITEMIDLIST pParent = pidlParent;
528 LPCITEMIDLIST pChild = pidlChild;
529
530 TRACE("%p %p %x\n", pidlParent, pidlChild, bImmediate);
531
532 if (!pParent || !pChild)
533 return FALSE;
534
535 while (pParent->mkid.cb && pChild->mkid.cb)
536 {
537 _ILSimpleGetText(pParent, szData1, MAX_PATH);
538 _ILSimpleGetText(pChild, szData2, MAX_PATH);
539
540 if (strcmp( szData1, szData2 ))
541 return FALSE;
542
543 pParent = ILGetNext(pParent);
544 pChild = ILGetNext(pChild);
545 }
546
547 /* child shorter or has equal length to parent */
548 if (pParent->mkid.cb || !pChild->mkid.cb)
549 return FALSE;
550
551 /* not immediate descent */
552 if ( ILGetNext(pChild)->mkid.cb && bImmediate)
553 return FALSE;
554
555 return TRUE;
556 }
557
558 /*************************************************************************
559 * ILFindChild [SHELL32.24]
560 *
561 * Compares elements from pidl1 and pidl2.
562 *
563 * PARAMS
564 * pidl1 [I]
565 * pidl2 [I]
566 *
567 * RETURNS
568 * pidl1 is desktop pidl2
569 * pidl1 shorter pidl2 pointer to first different element of pidl2
570 * if there was at least one equal element
571 * pidl2 shorter pidl1 0
572 * pidl2 equal pidl1 pointer to last 0x00-element of pidl2
573 *
574 * NOTES
575 * exported by ordinal.
576 */
577 LPITEMIDLIST WINAPI ILFindChild(LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2)
578 {
579 char szData1[MAX_PATH];
580 char szData2[MAX_PATH];
581
582 LPCITEMIDLIST pidltemp1 = pidl1;
583 LPCITEMIDLIST pidltemp2 = pidl2;
584 LPCITEMIDLIST ret = NULL;
585
586 TRACE("pidl1=%p pidl2=%p\n", pidl1, pidl2);
587
588 /* explorer reads from registry directly (StreamMRU),
589 so we can only check here */
590 if ((!pcheck (pidl1)) || (!pcheck (pidl2)))
591 return FALSE;
592
593 pdump (pidl1);
594 pdump (pidl2);
595
596 if (_ILIsDesktop(pidl1))
597 {
598 ret = pidl2;
599 }
600 else
601 {
602 while (pidltemp1->mkid.cb && pidltemp2->mkid.cb)
603 {
604 _ILSimpleGetText(pidltemp1, szData1, MAX_PATH);
605 _ILSimpleGetText(pidltemp2, szData2, MAX_PATH);
606
607 if (strcmp(szData1, szData2))
608 break;
609
610 pidltemp1 = ILGetNext(pidltemp1);
611 pidltemp2 = ILGetNext(pidltemp2);
612 ret = pidltemp2;
613 }
614
615 if (pidltemp1->mkid.cb)
616 ret = NULL; /* elements of pidl1 left*/
617 }
618 TRACE_(shell)("--- %p\n", ret);
619 return (LPITEMIDLIST)ret; /* pidl 1 is shorter */
620 }
621
622 /*************************************************************************
623 * ILCombine [SHELL32.25]
624 *
625 * Concatenates two complex ItemIDLists.
626 *
627 * PARAMS
628 * pidl1 [I] first complex ItemIDLists
629 * pidl2 [I] complex ItemIDLists to append
630 *
631 * RETURNS
632 * if both pidl's == NULL NULL
633 * if pidl1 == NULL cloned pidl2
634 * if pidl2 == NULL cloned pidl1
635 * otherwise new pidl with pidl2 appended to pidl1
636 *
637 * NOTES
638 * exported by ordinal.
639 * Does not destroy the passed in ItemIDLists!
640 */
641 LPITEMIDLIST WINAPI ILCombine(LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2)
642 {
643 DWORD len1, len2;
644 LPITEMIDLIST pidlNew;
645
646 TRACE("pidl=%p pidl=%p\n", pidl1, pidl2);
647
648 if (!pidl1 && !pidl2) return NULL;
649
650 pdump (pidl1);
651 pdump (pidl2);
652
653 if (!pidl1)
654 {
655 pidlNew = ILClone(pidl2);
656 return pidlNew;
657 }
658
659 if (!pidl2)
660 {
661 pidlNew = ILClone(pidl1);
662 return pidlNew;
663 }
664
665 len1 = ILGetSize(pidl1) - 2;
666 len2 = ILGetSize(pidl2);
667 pidlNew = (LPITEMIDLIST)SHAlloc(len1 + len2);
668
669 if (pidlNew)
670 {
671 memcpy(pidlNew, pidl1, len1);
672 memcpy(((BYTE *)pidlNew) + len1, pidl2, len2);
673 }
674
675 /* TRACE(pidl,"--new pidl=%p\n",pidlNew);*/
676 return pidlNew;
677 }
678
679 /*************************************************************************
680 * SHGetRealIDL [SHELL32.98]
681 *
682 * NOTES
683 */
684 HRESULT WINAPI SHGetRealIDL(LPSHELLFOLDER lpsf, LPCITEMIDLIST pidlSimple, LPITEMIDLIST *pidlReal)
685 {
686 CComPtr<IDataObject> pDataObj;
687 HRESULT hr;
688
689 hr = lpsf->GetUIObjectOf(0, 1, &pidlSimple, IID_NULL_PPV_ARG(IDataObject, &pDataObj));
690 if (SUCCEEDED(hr))
691 {
692 STGMEDIUM medium;
693 FORMATETC fmt;
694
695 fmt.cfFormat = RegisterClipboardFormatW(CFSTR_SHELLIDLIST);
696 fmt.ptd = NULL;
697 fmt.dwAspect = DVASPECT_CONTENT;
698 fmt.lindex = -1;
699 fmt.tymed = TYMED_HGLOBAL;
700
701 hr = pDataObj->GetData(&fmt, &medium);
702
703 if (SUCCEEDED(hr))
704 {
705 /*assert(pida->cidl==1);*/
706 LPIDA pida = (LPIDA)GlobalLock(medium.hGlobal);
707
708 LPCITEMIDLIST pidl_folder = (LPCITEMIDLIST) ((LPBYTE)pida + pida->aoffset[0]);
709 LPCITEMIDLIST pidl_child = (LPCITEMIDLIST) ((LPBYTE)pida + pida->aoffset[1]);
710
711 *pidlReal = ILCombine(pidl_folder, pidl_child);
712
713 if (!*pidlReal)
714 hr = E_OUTOFMEMORY;
715
716 GlobalUnlock(medium.hGlobal);
717 GlobalFree(medium.hGlobal);
718 }
719 }
720
721 return hr;
722 }
723
724 /*************************************************************************
725 * SHLogILFromFSIL [SHELL32.95]
726 *
727 * NOTES
728 * pild = CSIDL_DESKTOP ret = 0
729 * pild = CSIDL_DRIVES ret = 0
730 */
731 EXTERN_C LPITEMIDLIST WINAPI SHLogILFromFSIL(LPITEMIDLIST pidl)
732 {
733 FIXME("(pidl=%p)\n", pidl);
734
735 pdump(pidl);
736
737 return 0;
738 }
739
740 /*************************************************************************
741 * ILGetSize [SHELL32.152]
742 *
743 * Gets the byte size of an ItemIDList including zero terminator
744 *
745 * PARAMS
746 * pidl [I] ItemIDList
747 *
748 * RETURNS
749 * size of pidl in bytes
750 *
751 * NOTES
752 * exported by ordinal
753 */
754 UINT WINAPI ILGetSize(LPCITEMIDLIST pidl)
755 {
756 LPCSHITEMID si = &(pidl->mkid);
757 UINT len = 0;
758
759 if (pidl)
760 {
761 while (si->cb)
762 {
763 len += si->cb;
764 si = (LPCSHITEMID)(((const BYTE*)si) + si->cb);
765 }
766 len += 2;
767 }
768 TRACE("pidl=%p size=%u\n", pidl, len);
769 return len;
770 }
771
772 /*************************************************************************
773 * ILGetNext [SHELL32.153]
774 *
775 * Gets the next ItemID of an ItemIDList
776 *
777 * PARAMS
778 * pidl [I] ItemIDList
779 *
780 * RETURNS
781 * null -> null
782 * desktop -> null
783 * simple pidl -> pointer to 0x0000 element
784 *
785 * NOTES
786 * exported by ordinal.
787 */
788 LPITEMIDLIST WINAPI ILGetNext(LPCITEMIDLIST pidl)
789 {
790 WORD len;
791
792 TRACE("%p\n", pidl);
793
794 if (pidl)
795 {
796 len = pidl->mkid.cb;
797 if (len)
798 {
799 pidl = (LPCITEMIDLIST) (((const BYTE*)pidl) + len);
800 TRACE("-- %p\n", pidl);
801 return (LPITEMIDLIST)pidl;
802 }
803 }
804 return NULL;
805 }
806
807 /*************************************************************************
808 * ILAppend [SHELL32.154]
809 *
810 * Adds the single ItemID item to the ItemIDList indicated by pidl.
811 * If bEnd is FALSE, inserts the item in the front of the list,
812 * otherwise it adds the item to the end. (???)
813 *
814 * PARAMS
815 * pidl [I] ItemIDList to extend
816 * item [I] ItemID to prepend/append
817 * bEnd [I] Indicates if the item should be appended
818 *
819 * NOTES
820 * Destroys the passed in idlist! (???)
821 */
822 EXTERN_C LPITEMIDLIST WINAPI ILAppend(LPITEMIDLIST pidl, LPCITEMIDLIST item, BOOL bEnd)
823 {
824 LPITEMIDLIST idlRet;
825
826 WARN("(pidl=%p,pidl=%p,%08u)semi-stub\n", pidl, item, bEnd);
827
828 pdump (pidl);
829 pdump (item);
830
831 if (_ILIsDesktop(pidl))
832 {
833 idlRet = ILClone(item);
834 SHFree (pidl);
835 return idlRet;
836 }
837
838 if (bEnd)
839 idlRet = ILCombine(pidl, item);
840 else
841 idlRet = ILCombine(item, pidl);
842
843 SHFree(pidl);
844 return idlRet;
845 }
846
847 /*************************************************************************
848 * ILFree [SHELL32.155]
849 *
850 * Frees memory (if not NULL) allocated by SHMalloc allocator
851 *
852 * PARAMS
853 * pidl [I]
854 *
855 * RETURNS
856 * Nothing
857 *
858 * NOTES
859 * exported by ordinal
860 */
861 void WINAPI ILFree(LPITEMIDLIST pidl)
862 {
863 TRACE("(pidl=%p)\n", pidl);
864 SHFree(pidl);
865 }
866
867 /*************************************************************************
868 * ILGlobalFree [SHELL32.156]
869 *
870 * Frees memory (if not NULL) allocated by Alloc allocator
871 *
872 * PARAMS
873 * pidl [I]
874 *
875 * RETURNS
876 * Nothing
877 *
878 * NOTES
879 * exported by ordinal.
880 */
881 void WINAPI ILGlobalFree( LPITEMIDLIST pidl)
882 {
883 TRACE("%p\n", pidl);
884
885 Free(pidl);
886 }
887
888 /*************************************************************************
889 * ILCreateFromPathA [SHELL32.189]
890 *
891 * Creates a complex ItemIDList from a path and returns it.
892 *
893 * PARAMS
894 * path [I]
895 *
896 * RETURNS
897 * the newly created complex ItemIDList or NULL if failed
898 *
899 * NOTES
900 * exported by ordinal.
901 */
902 LPITEMIDLIST WINAPI ILCreateFromPathA (LPCSTR path)
903 {
904 LPITEMIDLIST pidlnew = NULL;
905
906 TRACE_(shell)("%s\n", debugstr_a(path));
907
908 if (SUCCEEDED(SHILCreateFromPathA(path, &pidlnew, NULL)))
909 return pidlnew;
910 return NULL;
911 }
912
913 /*************************************************************************
914 * ILCreateFromPathW [SHELL32.190]
915 *
916 * See ILCreateFromPathA.
917 */
918 LPITEMIDLIST WINAPI ILCreateFromPathW (LPCWSTR path)
919 {
920 LPITEMIDLIST pidlnew = NULL;
921
922 TRACE_(shell)("%s\n", debugstr_w(path));
923
924 if (SUCCEEDED(SHILCreateFromPathW(path, &pidlnew, NULL)))
925 return pidlnew;
926 return NULL;
927 }
928
929 /*************************************************************************
930 * ILCreateFromPath [SHELL32.157]
931 */
932 EXTERN_C LPITEMIDLIST WINAPI ILCreateFromPathAW (LPCVOID path)
933 {
934 if ( SHELL_OsIsUnicode())
935 return ILCreateFromPathW ((LPCWSTR)path);
936 return ILCreateFromPathA ((LPCSTR)path);
937 }
938
939 /*************************************************************************
940 * _ILParsePathW [internal]
941 *
942 * Creates an ItemIDList from a path and returns it.
943 *
944 * PARAMS
945 * path [I] path to parse and convert into an ItemIDList
946 * lpFindFile [I] pointer to buffer to initialize the FileSystem
947 * Bind Data object with
948 * bBindCtx [I] indicates to create a BindContext and assign a
949 * FileSystem Bind Data object
950 * ppidl [O] the newly create ItemIDList
951 * prgfInOut [I/O] requested attributes on input and actual
952 * attributes on return
953 *
954 * RETURNS
955 * NO_ERROR on success or an OLE error code
956 *
957 * NOTES
958 * If either lpFindFile is non-NULL or bBindCtx is TRUE, this function
959 * creates a BindContext object and assigns a FileSystem Bind Data object
960 * to it, passing the BindContext to IShellFolder_ParseDisplayName. Each
961 * IShellFolder uses that FileSystem Bind Data object of the BindContext
962 * to pass data about the current path element to the next object. This
963 * is used to avoid having to verify the current path element on disk, so
964 * that creating an ItemIDList from a nonexistent path still can work.
965 */
966 static HRESULT _ILParsePathW(LPCWSTR path, LPWIN32_FIND_DATAW lpFindFile,
967 BOOL bBindCtx, LPITEMIDLIST *ppidl, LPDWORD prgfInOut)
968 {
969 CComPtr<IShellFolder> pSF;
970 CComPtr<IBindCtx> pBC;
971 HRESULT ret;
972
973 TRACE("%s %p %d (%p)->%p (%p)->0x%x\n", debugstr_w(path), lpFindFile, bBindCtx,
974 ppidl, ppidl ? *ppidl : NULL,
975 prgfInOut, prgfInOut ? *prgfInOut : 0);
976
977 ret = SHGetDesktopFolder(&pSF);
978 if (FAILED(ret))
979 return ret;
980
981 if (lpFindFile || bBindCtx)
982 ret = IFileSystemBindData_Constructor(lpFindFile, &pBC);
983
984 if (SUCCEEDED(ret))
985 {
986 ret = pSF->ParseDisplayName(0, pBC, (LPOLESTR)path, NULL, ppidl, prgfInOut);
987 }
988
989 if (FAILED(ret) && ppidl)
990 *ppidl = NULL;
991
992 TRACE("%s %p 0x%x\n", debugstr_w(path), ppidl ? *ppidl : NULL, prgfInOut ? *prgfInOut : 0);
993
994 return ret;
995 }
996
997 /*************************************************************************
998 * SHSimpleIDListFromPath [SHELL32.162]
999 *
1000 * Creates a simple ItemIDList from a path and returns it. This function
1001 * does not fail on nonexistent paths.
1002 *
1003 * PARAMS
1004 * path [I] path to parse and convert into an ItemIDList
1005 *
1006 * RETURNS
1007 * the newly created simple ItemIDList
1008 *
1009 * NOTES
1010 * Simple in the name does not mean a relative ItemIDList but rather a
1011 * fully qualified list, where only the file name is filled in and the
1012 * directory flag for those ItemID elements this is known about, eg.
1013 * it is not the last element in the ItemIDList or the actual directory
1014 * exists on disk.
1015 * exported by ordinal.
1016 */
1017 LPITEMIDLIST WINAPI SHSimpleIDListFromPathA(LPCSTR lpszPath)
1018 {
1019 LPITEMIDLIST pidl = NULL;
1020 LPWSTR wPath = NULL;
1021 int len;
1022
1023 TRACE("%s\n", debugstr_a(lpszPath));
1024
1025 if (lpszPath)
1026 {
1027 len = MultiByteToWideChar(CP_ACP, 0, lpszPath, -1, NULL, 0);
1028 wPath = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1029 MultiByteToWideChar(CP_ACP, 0, lpszPath, -1, wPath, len);
1030 }
1031
1032 _ILParsePathW(wPath, NULL, TRUE, &pidl, NULL);
1033
1034 HeapFree(GetProcessHeap(), 0, wPath);
1035 TRACE("%s %p\n", debugstr_a(lpszPath), pidl);
1036 return pidl;
1037 }
1038
1039 LPITEMIDLIST WINAPI SHSimpleIDListFromPathW(LPCWSTR lpszPath)
1040 {
1041 LPITEMIDLIST pidl = NULL;
1042
1043 TRACE("%s\n", debugstr_w(lpszPath));
1044
1045 _ILParsePathW(lpszPath, NULL, TRUE, &pidl, NULL);
1046 TRACE("%s %p\n", debugstr_w(lpszPath), pidl);
1047 return pidl;
1048 }
1049
1050 EXTERN_C LPITEMIDLIST WINAPI SHSimpleIDListFromPathAW(LPCVOID lpszPath)
1051 {
1052 if ( SHELL_OsIsUnicode())
1053 return SHSimpleIDListFromPathW ((LPCWSTR)lpszPath);
1054 return SHSimpleIDListFromPathA ((LPCSTR)lpszPath);
1055 }
1056
1057 /*************************************************************************
1058 * SHGetDataFromIDListA [SHELL32.247]
1059 *
1060 * NOTES
1061 * the pidl can be a simple one. since we can't get the path out of the pidl
1062 * we have to take all data from the pidl
1063 */
1064 HRESULT WINAPI SHGetDataFromIDListA(LPSHELLFOLDER psf, LPCITEMIDLIST pidl,
1065 int nFormat, LPVOID dest, int len)
1066 {
1067 LPSTR filename, shortname;
1068 WIN32_FIND_DATAA * pfd;
1069
1070 TRACE_(shell)("sf=%p pidl=%p 0x%04x %p 0x%04x stub\n", psf, pidl, nFormat, dest, len);
1071
1072 pdump(pidl);
1073 if (!psf || !dest)
1074 return E_INVALIDARG;
1075
1076 switch (nFormat)
1077 {
1078 case SHGDFIL_FINDDATA:
1079 pfd = (WIN32_FIND_DATAA *)dest;
1080
1081 if (_ILIsDrive(pidl) || _ILIsSpecialFolder(pidl))
1082 return E_INVALIDARG;
1083
1084 if (len < (int)sizeof(WIN32_FIND_DATAA))
1085 return E_INVALIDARG;
1086
1087 ZeroMemory(pfd, sizeof (WIN32_FIND_DATAA));
1088 _ILGetFileDateTime( pidl, &(pfd->ftLastWriteTime));
1089 pfd->dwFileAttributes = _ILGetFileAttributes(pidl, NULL, 0);
1090 pfd->nFileSizeLow = _ILGetFileSize ( pidl, NULL, 0);
1091
1092 filename = _ILGetTextPointer(pidl);
1093 shortname = _ILGetSTextPointer(pidl);
1094
1095 if (filename)
1096 lstrcpynA(pfd->cFileName, filename, sizeof(pfd->cFileName));
1097 else
1098 pfd->cFileName[0] = '\0';
1099
1100 if (shortname)
1101 lstrcpynA(pfd->cAlternateFileName, shortname, sizeof(pfd->cAlternateFileName));
1102 else
1103 pfd->cAlternateFileName[0] = '\0';
1104 return S_OK;
1105
1106 case SHGDFIL_NETRESOURCE:
1107 case SHGDFIL_DESCRIPTIONID:
1108 FIXME_(shell)("SHGDFIL %i stub\n", nFormat);
1109 break;
1110
1111 default:
1112 ERR_(shell)("Unknown SHGDFIL %i, please report\n", nFormat);
1113 }
1114
1115 return E_INVALIDARG;
1116 }
1117
1118 /*************************************************************************
1119 * SHGetDataFromIDListW [SHELL32.248]
1120 *
1121 */
1122 HRESULT WINAPI SHGetDataFromIDListW(LPSHELLFOLDER psf, LPCITEMIDLIST pidl,
1123 int nFormat, LPVOID dest, int len)
1124 {
1125 LPSTR filename, shortname;
1126 WIN32_FIND_DATAW * pfd = (WIN32_FIND_DATAW *)dest;
1127
1128 TRACE_(shell)("sf=%p pidl=%p 0x%04x %p 0x%04x stub\n", psf, pidl, nFormat, dest, len);
1129
1130 pdump(pidl);
1131
1132 if (!psf || !dest)
1133 return E_INVALIDARG;
1134
1135 switch (nFormat)
1136 {
1137 case SHGDFIL_FINDDATA:
1138 pfd = (WIN32_FIND_DATAW *)dest;
1139
1140 if (_ILIsDrive(pidl))
1141 return E_INVALIDARG;
1142
1143 if (len < (int)sizeof(WIN32_FIND_DATAW))
1144 return E_INVALIDARG;
1145
1146 ZeroMemory(pfd, sizeof (WIN32_FIND_DATAW));
1147 _ILGetFileDateTime( pidl, &(pfd->ftLastWriteTime));
1148 pfd->dwFileAttributes = _ILGetFileAttributes(pidl, NULL, 0);
1149 pfd->nFileSizeLow = _ILGetFileSize ( pidl, NULL, 0);
1150
1151 filename = _ILGetTextPointer(pidl);
1152 shortname = _ILGetSTextPointer(pidl);
1153
1154 if (!filename)
1155 pfd->cFileName[0] = '\0';
1156 else if (!MultiByteToWideChar(CP_ACP, 0, filename, -1, pfd->cFileName, MAX_PATH))
1157 pfd->cFileName[MAX_PATH-1] = 0;
1158
1159 if (!shortname)
1160 pfd->cAlternateFileName[0] = '\0';
1161 else if (!MultiByteToWideChar(CP_ACP, 0, shortname, -1, pfd->cAlternateFileName, 14))
1162 pfd->cAlternateFileName[13] = 0;
1163 return S_OK;
1164
1165 case SHGDFIL_NETRESOURCE:
1166 case SHGDFIL_DESCRIPTIONID:
1167 FIXME_(shell)("SHGDFIL %i stub\n", nFormat);
1168 break;
1169
1170 default:
1171 ERR_(shell)("Unknown SHGDFIL %i, please report\n", nFormat);
1172 }
1173
1174 return E_INVALIDARG;
1175 }
1176
1177 /*************************************************************************
1178 * SHGetPathFromIDListA [SHELL32.@][NT 4.0: SHELL32.220]
1179 *
1180 * PARAMETERS
1181 * pidl, [IN] pidl
1182 * pszPath [OUT] path
1183 *
1184 * RETURNS
1185 * path from a passed PIDL.
1186 *
1187 * NOTES
1188 * NULL returns FALSE
1189 * desktop pidl gives path to desktop directory back
1190 * special pidls returning FALSE
1191 */
1192 BOOL WINAPI SHGetPathFromIDListA(LPCITEMIDLIST pidl, LPSTR pszPath)
1193 {
1194 WCHAR wszPath[MAX_PATH];
1195 BOOL bSuccess;
1196
1197 bSuccess = SHGetPathFromIDListW(pidl, wszPath);
1198 WideCharToMultiByte(CP_ACP, 0, wszPath, -1, pszPath, MAX_PATH, NULL, NULL);
1199
1200 return bSuccess;
1201 }
1202
1203 /*************************************************************************
1204 * SHGetPathFromIDListW [SHELL32.@]
1205 *
1206 * See SHGetPathFromIDListA.
1207 */
1208 BOOL WINAPI SHGetPathFromIDListW(LPCITEMIDLIST pidl, LPWSTR pszPath)
1209 {
1210 HRESULT hr;
1211 LPCITEMIDLIST pidlLast;
1212 CComPtr<IShellFolder> psfFolder;
1213 DWORD dwAttributes;
1214 STRRET strret;
1215
1216 TRACE_(shell)("(pidl=%p,%p)\n", pidl, pszPath);
1217 pdump(pidl);
1218
1219 *pszPath = '\0';
1220 if (!pidl)
1221 return FALSE;
1222
1223 hr = SHBindToParent(pidl, IID_PPV_ARG(IShellFolder, &psfFolder), &pidlLast);
1224 if (FAILED(hr))
1225 {
1226 ERR("SHBindToParent failed: %x\n", hr);
1227 return FALSE;
1228 }
1229
1230 dwAttributes = SFGAO_FILESYSTEM;
1231 hr = psfFolder->GetAttributesOf(1, &pidlLast, &dwAttributes);
1232 if (FAILED(hr) || !(dwAttributes & SFGAO_FILESYSTEM))
1233 {
1234 WARN("Wrong dwAttributes or GetAttributesOf failed: %x\n", hr);
1235 return FALSE;
1236 }
1237
1238 hr = psfFolder->GetDisplayNameOf(pidlLast, SHGDN_FORPARSING, &strret);
1239 if (FAILED(hr)) return FALSE;
1240
1241 hr = StrRetToBufW(&strret, pidlLast, pszPath, MAX_PATH);
1242
1243 TRACE_(shell)("-- %s, 0x%08x\n", debugstr_w(pszPath), hr);
1244 return SUCCEEDED(hr);
1245 }
1246
1247 /*************************************************************************
1248 * SHBindToParent [shell version 5.0]
1249 */
1250 HRESULT WINAPI SHBindToParent(LPCITEMIDLIST pidl, REFIID riid, LPVOID *ppv, LPCITEMIDLIST *ppidlLast)
1251 {
1252 CComPtr<IShellFolder> psfDesktop;
1253 HRESULT hr = E_FAIL;
1254
1255 TRACE_(shell)("pidl=%p\n", pidl);
1256 pdump(pidl);
1257
1258 if (!pidl || !ppv)
1259 return E_INVALIDARG;
1260
1261 *ppv = NULL;
1262 if (ppidlLast)
1263 *ppidlLast = NULL;
1264
1265 hr = SHGetDesktopFolder(&psfDesktop);
1266 if (FAILED(hr))
1267 return hr;
1268
1269 if (_ILIsPidlSimple(pidl))
1270 {
1271 /* we are on desktop level */
1272 hr = psfDesktop->QueryInterface(riid, ppv);
1273 }
1274 else
1275 {
1276 LPITEMIDLIST pidlParent = ILClone(pidl);
1277 ILRemoveLastID(pidlParent);
1278 hr = psfDesktop->BindToObject(pidlParent, NULL, riid, ppv);
1279 SHFree (pidlParent);
1280 }
1281
1282 if (SUCCEEDED(hr) && ppidlLast)
1283 *ppidlLast = ILFindLastID(pidl);
1284
1285 TRACE_(shell)("-- psf=%p pidl=%p ret=0x%08x\n", *ppv, (ppidlLast) ? *ppidlLast : NULL, hr);
1286 return hr;
1287 }
1288
1289 /**************************************************************************
1290 *
1291 * internal functions
1292 *
1293 * ### 1. section creating pidls ###
1294 *
1295 *************************************************************************
1296 */
1297
1298 /* Basic PIDL constructor. Allocates size + 5 bytes, where:
1299 * - two bytes are SHITEMID.cb
1300 * - one byte is PIDLDATA.type
1301 * - two bytes are the NULL PIDL terminator
1302 * Sets type of the returned PIDL to type.
1303 */
1304 static LPITEMIDLIST _ILAlloc(PIDLTYPE type, unsigned int size)
1305 {
1306 LPITEMIDLIST pidlOut = NULL;
1307
1308 pidlOut = (LPITEMIDLIST)SHAlloc(size + 5);
1309 if(pidlOut)
1310 {
1311 LPPIDLDATA pData;
1312 LPITEMIDLIST pidlNext;
1313
1314 ZeroMemory(pidlOut, size + 5);
1315 pidlOut->mkid.cb = size + 3;
1316
1317 pData = _ILGetDataPointer(pidlOut);
1318 if (pData)
1319 pData->type = type;
1320
1321 pidlNext = ILGetNext(pidlOut);
1322 if (pidlNext)
1323 pidlNext->mkid.cb = 0x00;
1324 TRACE("-- (pidl=%p, size=%u)\n", pidlOut, size);
1325 }
1326
1327 return pidlOut;
1328 }
1329
1330 LPITEMIDLIST _ILCreateDesktop(void)
1331 {
1332 LPITEMIDLIST ret;
1333
1334 TRACE("()\n");
1335 ret = (LPITEMIDLIST)SHAlloc(2);
1336 if (ret)
1337 ret->mkid.cb = 0;
1338 return ret;
1339 }
1340
1341 LPITEMIDLIST _ILCreateMyComputer(void)
1342 {
1343 TRACE("()\n");
1344 return _ILCreateGuid(PT_GUID, CLSID_MyComputer);
1345 }
1346
1347 LPITEMIDLIST _ILCreateMyDocuments(void)
1348 {
1349 TRACE("()\n");
1350 return _ILCreateGuid(PT_GUID, CLSID_MyDocuments);
1351 }
1352
1353 LPITEMIDLIST _ILCreateIExplore(void)
1354 {
1355 TRACE("()\n");
1356 return _ILCreateGuid(PT_GUID, CLSID_Internet);
1357 }
1358
1359 LPITEMIDLIST _ILCreateControlPanel(void)
1360 {
1361 LPITEMIDLIST parent = _ILCreateGuid(PT_GUID, CLSID_MyComputer), ret = NULL;
1362
1363 TRACE("()\n");
1364 if (parent)
1365 {
1366 LPITEMIDLIST cpl = _ILCreateGuid(PT_SHELLEXT, CLSID_ControlPanel);
1367
1368 if (cpl)
1369 {
1370 ret = ILCombine(parent, cpl);
1371 SHFree(cpl);
1372 }
1373 SHFree(parent);
1374 }
1375 return ret;
1376 }
1377
1378 LPITEMIDLIST _ILCreatePrinters(void)
1379 {
1380 LPITEMIDLIST parent = _ILCreateGuid(PT_GUID, CLSID_MyComputer), ret = NULL;
1381
1382 TRACE("()\n");
1383 if (parent)
1384 {
1385 LPITEMIDLIST printers = _ILCreateGuid(PT_YAGUID, CLSID_Printers);
1386
1387 if (printers)
1388 {
1389 ret = ILCombine(parent, printers);
1390 SHFree(printers);
1391 }
1392 SHFree(parent);
1393 }
1394 return ret;
1395 }
1396
1397 LPITEMIDLIST _ILCreateNetwork(void)
1398 {
1399 TRACE("()\n");
1400 return _ILCreateGuid(PT_GUID, CLSID_NetworkPlaces);
1401 }
1402
1403 LPITEMIDLIST _ILCreateBitBucket(void)
1404 {
1405 TRACE("()\n");
1406 return _ILCreateGuid(PT_GUID, CLSID_RecycleBin);
1407 }
1408
1409 LPITEMIDLIST _ILCreateAdminTools(void)
1410 {
1411 TRACE("()\n");
1412 return _ILCreateGuid(PT_GUID, CLSID_AdminFolderShortcut); //FIXME
1413 }
1414
1415 LPITEMIDLIST _ILCreateGuid(PIDLTYPE type, REFIID guid)
1416 {
1417 LPITEMIDLIST pidlOut;
1418
1419 if (type == PT_SHELLEXT || type == PT_GUID || type == PT_YAGUID)
1420 {
1421 pidlOut = _ILAlloc(type, sizeof(GUIDStruct));
1422 if (pidlOut)
1423 {
1424 LPPIDLDATA pData = _ILGetDataPointer(pidlOut);
1425
1426 pData->u.guid.guid = guid;
1427 TRACE("-- create GUID-pidl %s\n",
1428 debugstr_guid(&(pData->u.guid.guid)));
1429 }
1430 }
1431 else
1432 {
1433 WARN("%d: invalid type for GUID\n", type);
1434 pidlOut = NULL;
1435 }
1436 return pidlOut;
1437 }
1438
1439 LPITEMIDLIST _ILCreateGuidFromStrA(LPCSTR szGUID)
1440 {
1441 IID iid;
1442
1443 if (FAILED(SHCLSIDFromStringA(szGUID, &iid)))
1444 {
1445 ERR("%s is not a GUID\n", szGUID);
1446 return NULL;
1447 }
1448 return _ILCreateGuid(PT_GUID, iid);
1449 }
1450
1451 LPITEMIDLIST _ILCreateGuidFromStrW(LPCWSTR szGUID)
1452 {
1453 IID iid;
1454
1455 if (FAILED(CLSIDFromString((LPOLESTR)szGUID, &iid)))
1456 {
1457 ERR("%s is not a GUID\n", debugstr_w(szGUID));
1458 return NULL;
1459 }
1460 return _ILCreateGuid(PT_GUID, iid);
1461 }
1462
1463 LPITEMIDLIST _ILCreateFromFindDataW( const WIN32_FIND_DATAW *wfd )
1464 {
1465 char buff[MAX_PATH + 14 +1]; /* see WIN32_FIND_DATA */
1466 DWORD len, len1, wlen, alen, cbData;
1467 LPITEMIDLIST pidl;
1468 PIDLTYPE type;
1469
1470 if (!wfd)
1471 return NULL;
1472
1473 TRACE("(%s, %s)\n", debugstr_w(wfd->cAlternateFileName), debugstr_w(wfd->cFileName));
1474
1475 /* prepare buffer with both names */
1476 len = WideCharToMultiByte(CP_ACP, 0, wfd->cFileName, -1, buff, MAX_PATH, NULL, NULL);
1477 len1 = WideCharToMultiByte(CP_ACP, 0, wfd->cAlternateFileName, -1, buff + len, sizeof(buff) - len, NULL, NULL);
1478 alen = len + len1;
1479
1480 type = (wfd->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? PT_FOLDER : PT_VALUE;
1481
1482 wlen = wcslen(wfd->cFileName) + 1;
1483 cbData = sizeof(FileStruct) - 1 + (alen + (alen & 1)); // Note: szNames field is initially 1 byte long
1484 cbData += sizeof(FileStructW) - 1 + wlen * sizeof(WCHAR); // Note: wszName field is initially 1 byte long
1485 cbData += sizeof(WORD); // offset to FileStructW
1486 pidl = _ILAlloc(type, cbData);
1487 if (pidl)
1488 {
1489 LPPIDLDATA pData = _ILGetDataPointer(pidl);
1490 FileStruct *fs = &pData->u.file;
1491 FileStructW *fsw;
1492 WORD *pOffsetW;
1493
1494 FileTimeToDosDateTime( &wfd->ftLastWriteTime, &fs->uFileDate, &fs->uFileTime);
1495 fs->dwFileSize = wfd->nFileSizeLow;
1496 fs->uFileAttribs = wfd->dwFileAttributes;
1497 memcpy(fs->szNames, buff, alen);
1498
1499 fsw = (FileStructW*)(pData->u.file.szNames + alen + (alen & 0x1));
1500 fsw->cbLen = sizeof(FileStructW) - 1 + wlen * sizeof(WCHAR) + sizeof(WORD);
1501 FileTimeToDosDateTime( &wfd->ftCreationTime, &fsw->uCreationDate, &fsw->uCreationTime);
1502 FileTimeToDosDateTime( &wfd->ftLastAccessTime, &fsw->uLastAccessDate, &fsw->uLastAccessTime);
1503 memcpy(fsw->wszName, wfd->cFileName, wlen * sizeof(WCHAR));
1504
1505 pOffsetW = (WORD*)((LPBYTE)pidl + pidl->mkid.cb - sizeof(WORD));
1506 *pOffsetW = (LPBYTE)fsw - (LPBYTE)pidl;
1507 TRACE("-- Set Value: %s\n", debugstr_w(fsw->wszName));
1508 }
1509 return pidl;
1510
1511 }
1512
1513 HRESULT _ILCreateFromPathW(LPCWSTR szPath, LPITEMIDLIST* ppidl)
1514 {
1515 HANDLE hFile;
1516 WIN32_FIND_DATAW stffile;
1517
1518 if (!ppidl)
1519 return E_INVALIDARG;
1520
1521 hFile = FindFirstFileW(szPath, &stffile);
1522 if (hFile == INVALID_HANDLE_VALUE)
1523 return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
1524
1525 FindClose(hFile);
1526
1527 *ppidl = _ILCreateFromFindDataW(&stffile);
1528
1529 return *ppidl ? S_OK : E_OUTOFMEMORY;
1530 }
1531
1532 LPITEMIDLIST _ILCreateDrive(LPCWSTR lpszNew)
1533 {
1534 LPITEMIDLIST pidlOut;
1535
1536 TRACE("(%s)\n", debugstr_w(lpszNew));
1537
1538 pidlOut = _ILAlloc(PT_DRIVE, sizeof(DriveStruct));
1539 if (pidlOut)
1540 {
1541 LPSTR pszDest;
1542
1543 pszDest = _ILGetTextPointer(pidlOut);
1544 if (pszDest)
1545 {
1546 strcpy(pszDest, "x:\\");
1547 pszDest[0] = towupper(lpszNew[0]);
1548 TRACE("-- create Drive: %s\n", debugstr_a(pszDest));
1549 }
1550 }
1551 return pidlOut;
1552 }
1553
1554 /**************************************************************************
1555 * _ILGetDrive()
1556 *
1557 * Gets the text for the drive eg. 'c:\'
1558 *
1559 * RETURNS
1560 * strlen (lpszText)
1561 */
1562 DWORD _ILGetDrive(LPCITEMIDLIST pidl, LPSTR pOut, UINT uSize)
1563 {
1564 TRACE("(%p,%p,%u)\n", pidl, pOut, uSize);
1565
1566 if(_ILIsMyComputer(pidl))
1567 pidl = ILGetNext(pidl);
1568
1569 if (pidl && _ILIsDrive(pidl))
1570 return _ILSimpleGetText(pidl, pOut, uSize);
1571
1572 return 0;
1573 }
1574
1575 /**************************************************************************
1576 *
1577 * ### 2. section testing pidls ###
1578 *
1579 **************************************************************************
1580 * _ILIsUnicode()
1581 * _ILIsDesktop()
1582 * _ILIsMyComputer()
1583 * _ILIsSpecialFolder()
1584 * _ILIsDrive()
1585 * _ILIsFolder()
1586 * _ILIsValue()
1587 * _ILIsPidlSimple()
1588 */
1589 BOOL _ILIsUnicode(LPCITEMIDLIST pidl)
1590 {
1591 LPPIDLDATA lpPData = _ILGetDataPointer(pidl);
1592
1593 TRACE("(%p)\n", pidl);
1594
1595 return (pidl && lpPData && PT_VALUEW == lpPData->type);
1596 }
1597
1598 BOOL _ILIsDesktop(LPCITEMIDLIST pidl)
1599 {
1600 TRACE("(%p)\n", pidl);
1601
1602 return pidl && pidl->mkid.cb ? 0 : 1;
1603 }
1604
1605 BOOL _ILIsMyDocuments(LPCITEMIDLIST pidl)
1606 {
1607 IID *iid = _ILGetGUIDPointer(pidl);
1608
1609 TRACE("(%p)\n", pidl);
1610
1611 if (iid)
1612 return IsEqualIID(*iid, CLSID_MyDocuments);
1613 return FALSE;
1614 }
1615
1616 BOOL _ILIsControlPanel(LPCITEMIDLIST pidl)
1617 {
1618 IID *iid = _ILGetGUIDPointer(pidl);
1619
1620 TRACE("(%p)\n", pidl);
1621
1622 if (iid)
1623 return IsEqualIID(*iid, CLSID_ControlPanel);
1624 return FALSE;
1625 }
1626
1627 BOOL _ILIsNetHood(LPCITEMIDLIST pidl)
1628 {
1629 IID *iid = _ILGetGUIDPointer(pidl);
1630
1631 TRACE("(%p)\n", pidl);
1632
1633 if (iid)
1634 return IsEqualIID(*iid, CLSID_NetworkPlaces);
1635 return FALSE;
1636 }
1637
1638
1639 LPITEMIDLIST _ILCreateNetHood(void)
1640 {
1641 return _ILCreateGuid(PT_GUID, CLSID_NetworkPlaces);
1642 }
1643
1644 LPITEMIDLIST _ILCreateFont(void)
1645 {
1646 return _ILCreateGuid(PT_GUID, CLSID_FontsFolderShortcut);
1647 }
1648
1649 BOOL _ILIsMyComputer(LPCITEMIDLIST pidl)
1650 {
1651 IID *iid = _ILGetGUIDPointer(pidl);
1652
1653 TRACE("(%p)\n", pidl);
1654
1655 if (iid)
1656 return IsEqualIID(*iid, CLSID_MyComputer);
1657 return FALSE;
1658 }
1659
1660 BOOL _ILIsPrinter(LPCITEMIDLIST pidl)
1661 {
1662 IID *iid = _ILGetGUIDPointer(pidl);
1663
1664 TRACE("(%p)\n", pidl);
1665
1666 if (iid)
1667 return IsEqualIID(*iid, CLSID_Printers);
1668 return FALSE;
1669 }
1670
1671 BOOL _ILIsBitBucket(LPCITEMIDLIST pidl)
1672 {
1673 IID *iid = _ILGetGUIDPointer(pidl);
1674
1675 TRACE("(%p)\n", pidl);
1676
1677 if (iid)
1678 return IsEqualIID(*iid, CLSID_RecycleBin);
1679 return FALSE;
1680 }
1681
1682 BOOL _ILIsAdminTools(LPCITEMIDLIST pidl)
1683 {
1684 IID *iid = _ILGetGUIDPointer(pidl);
1685
1686 TRACE("(%p)\n", pidl);
1687
1688 if (iid)
1689 return IsEqualIID(*iid, CLSID_AdminFolderShortcut);
1690 else
1691 return FALSE;
1692 }
1693
1694 BOOL _ILIsSpecialFolder (LPCITEMIDLIST pidl)
1695 {
1696 LPPIDLDATA lpPData = _ILGetDataPointer(pidl);
1697
1698 TRACE("(%p)\n", pidl);
1699
1700 return (pidl && ( (lpPData && (PT_GUID == lpPData->type || PT_SHELLEXT == lpPData->type || PT_YAGUID == lpPData->type)) ||
1701 (pidl && pidl->mkid.cb == 0x00)
1702 ));
1703 }
1704
1705 BOOL _ILIsDrive(LPCITEMIDLIST pidl)
1706 {
1707 LPPIDLDATA lpPData = _ILGetDataPointer(pidl);
1708
1709 TRACE("(%p)\n", pidl);
1710
1711 return (pidl && lpPData && (PT_DRIVE == lpPData->type ||
1712 PT_DRIVE1 == lpPData->type ||
1713 PT_DRIVE2 == lpPData->type ||
1714 PT_DRIVE3 == lpPData->type));
1715 }
1716
1717 BOOL _ILIsFolder(LPCITEMIDLIST pidl)
1718 {
1719 LPPIDLDATA lpPData = _ILGetDataPointer(pidl);
1720
1721 TRACE("(%p)\n", pidl);
1722
1723 return (pidl && lpPData && (PT_FOLDER == lpPData->type || PT_FOLDER1 == lpPData->type));
1724 }
1725
1726 BOOL _ILIsValue(LPCITEMIDLIST pidl)
1727 {
1728 LPPIDLDATA lpPData = _ILGetDataPointer(pidl);
1729
1730 TRACE("(%p)\n", pidl);
1731
1732 return (pidl && lpPData && PT_VALUE == lpPData->type);
1733 }
1734
1735 BOOL _ILIsCPanelStruct(LPCITEMIDLIST pidl)
1736 {
1737 LPPIDLDATA lpPData = _ILGetDataPointer(pidl);
1738
1739 TRACE("(%p)\n", pidl);
1740
1741 return (pidl && lpPData && (lpPData->type == 0));
1742 }
1743
1744 /**************************************************************************
1745 * _ILIsPidlSimple
1746 */
1747 BOOL _ILIsPidlSimple(LPCITEMIDLIST pidl)
1748 {
1749 BOOL ret = TRUE;
1750
1751 if(! _ILIsDesktop(pidl)) /* pidl=NULL or mkid.cb=0 */
1752 {
1753 WORD len = pidl->mkid.cb;
1754 LPCITEMIDLIST pidlnext = (LPCITEMIDLIST) (((const BYTE*)pidl) + len );
1755
1756 if (pidlnext->mkid.cb)
1757 ret = FALSE;
1758 }
1759
1760 TRACE("%s\n", ret ? "Yes" : "No");
1761 return ret;
1762 }
1763
1764 /**************************************************************************
1765 *
1766 * ### 3. section getting values from pidls ###
1767 */
1768
1769 /**************************************************************************
1770 * _ILSimpleGetText
1771 *
1772 * gets the text for the first item in the pidl (eg. simple pidl)
1773 *
1774 * returns the length of the string
1775 */
1776 DWORD _ILSimpleGetText (LPCITEMIDLIST pidl, LPSTR szOut, UINT uOutSize)
1777 {
1778 DWORD dwReturn = 0;
1779 LPSTR szSrc;
1780 LPWSTR szSrcW;
1781 GUID const * riid;
1782 char szTemp[MAX_PATH];
1783
1784 TRACE("(%p %p %x)\n", pidl, szOut, uOutSize);
1785
1786 if (!pidl)
1787 return 0;
1788
1789 if (szOut)
1790 *szOut = 0;
1791
1792 if (_ILIsDesktop(pidl))
1793 {
1794 /* desktop */
1795 if (HCR_GetClassNameA(CLSID_ShellDesktop, szTemp, MAX_PATH))
1796 {
1797 if (szOut)
1798 lstrcpynA(szOut, szTemp, uOutSize);
1799
1800 dwReturn = strlen (szTemp);
1801 }
1802 }
1803 else if (( szSrc = _ILGetTextPointer(pidl) ))
1804 {
1805 /* filesystem */
1806 if (szOut)
1807 lstrcpynA(szOut, szSrc, uOutSize);
1808
1809 dwReturn = strlen(szSrc);
1810 }
1811 else if (( szSrcW = _ILGetTextPointerW(pidl) ))
1812 {
1813 /* unicode filesystem */
1814 WideCharToMultiByte(CP_ACP, 0, szSrcW, -1, szTemp, MAX_PATH, NULL, NULL);
1815
1816 if (szOut)
1817 lstrcpynA(szOut, szTemp, uOutSize);
1818
1819 dwReturn = strlen (szTemp);
1820 }
1821 else if (( riid = _ILGetGUIDPointer(pidl) ))
1822 {
1823 /* special folder */
1824 if (HCR_GetClassNameA(*riid, szTemp, MAX_PATH) )
1825 {
1826 if (szOut)
1827 lstrcpynA(szOut, szTemp, uOutSize);
1828
1829 dwReturn = strlen (szTemp);
1830 }
1831 }
1832 else
1833 {
1834 ERR("-- no text\n");
1835 }
1836
1837 TRACE("-- (%p=%s 0x%08x)\n", szOut, debugstr_a(szOut), dwReturn);
1838 return dwReturn;
1839 }
1840
1841 /**************************************************************************
1842 * _ILSimpleGetTextW
1843 *
1844 * gets the text for the first item in the pidl (eg. simple pidl)
1845 *
1846 * returns the length of the string
1847 */
1848 DWORD _ILSimpleGetTextW (LPCITEMIDLIST pidl, LPWSTR szOut, UINT uOutSize)
1849 {
1850 DWORD dwReturn;
1851 FileStructW *pFileStructW = _ILGetFileStructW(pidl);
1852
1853 TRACE("(%p %p %x)\n", pidl, szOut, uOutSize);
1854
1855 if (pFileStructW) {
1856 lstrcpynW(szOut, pFileStructW->wszName, uOutSize);
1857 dwReturn = wcslen(pFileStructW->wszName);
1858 } else {
1859 GUID const * riid;
1860 WCHAR szTemp[MAX_PATH];
1861 LPSTR szSrc;
1862 LPWSTR szSrcW;
1863 dwReturn = 0;
1864
1865 if (!pidl)
1866 return 0;
1867
1868 if (szOut)
1869 *szOut = 0;
1870
1871 if (_ILIsDesktop(pidl))
1872 {
1873 /* desktop */
1874 if (HCR_GetClassNameW(CLSID_ShellDesktop, szTemp, MAX_PATH))
1875 {
1876 if (szOut)
1877 lstrcpynW(szOut, szTemp, uOutSize);
1878
1879 dwReturn = wcslen (szTemp);
1880 }
1881 }
1882 else if (( szSrcW = _ILGetTextPointerW(pidl) ))
1883 {
1884 /* unicode filesystem */
1885 if (szOut)
1886 lstrcpynW(szOut, szSrcW, uOutSize);
1887
1888 dwReturn = wcslen(szSrcW);
1889 }
1890 else if (( szSrc = _ILGetTextPointer(pidl) ))
1891 {
1892 /* filesystem */
1893 MultiByteToWideChar(CP_ACP, 0, szSrc, -1, szTemp, MAX_PATH);
1894
1895 if (szOut)
1896 lstrcpynW(szOut, szTemp, uOutSize);
1897
1898 dwReturn = wcslen (szTemp);
1899 }
1900 else if (( riid = _ILGetGUIDPointer(pidl) ))
1901 {
1902 /* special folder */
1903 if ( HCR_GetClassNameW(*riid, szTemp, MAX_PATH) )
1904 {
1905 if (szOut)
1906 lstrcpynW(szOut, szTemp, uOutSize);
1907
1908 dwReturn = wcslen (szTemp);
1909 }
1910 }
1911 else
1912 {
1913 ERR("-- no text\n");
1914 }
1915 }
1916
1917 TRACE("-- (%p=%s 0x%08x)\n", szOut, debugstr_w(szOut), dwReturn);
1918 return dwReturn;
1919 }
1920
1921 /**************************************************************************
1922 *
1923 * ### 4. getting pointers to parts of pidls ###
1924 *
1925 **************************************************************************
1926 * _ILGetDataPointer()
1927 */
1928 LPPIDLDATA _ILGetDataPointer(LPCITEMIDLIST pidl)
1929 {
1930 if(pidl && pidl->mkid.cb != 0x00)
1931 return (LPPIDLDATA)pidl->mkid.abID;
1932 return NULL;
1933 }
1934
1935 /**************************************************************************
1936 * _ILGetTextPointerW()
1937 * gets a pointer to the unicode long filename string stored in the pidl
1938 */
1939 static LPWSTR _ILGetTextPointerW(LPCITEMIDLIST pidl)
1940 {
1941 /* TRACE(pidl,"(pidl%p)\n", pidl);*/
1942
1943 LPPIDLDATA pdata = _ILGetDataPointer(pidl);
1944
1945 if (!pdata)
1946 return NULL;
1947
1948 switch (pdata->type)
1949 {
1950 case PT_GUID:
1951 case PT_SHELLEXT:
1952 case PT_YAGUID:
1953 return NULL;
1954
1955 case PT_DRIVE:
1956 case PT_DRIVE1:
1957 case PT_DRIVE2:
1958 case PT_DRIVE3:
1959 /*return (LPSTR)&(pdata->u.drive.szDriveName);*/
1960 return NULL;
1961
1962 case PT_FOLDER:
1963 case PT_FOLDER1:
1964 case PT_VALUE:
1965 case PT_IESPECIAL1:
1966 case PT_IESPECIAL2:
1967 /*return (LPSTR)&(pdata->u.file.szNames);*/
1968 return NULL;
1969
1970 case PT_WORKGRP:
1971 case PT_COMP:
1972 case PT_NETWORK:
1973 case PT_NETPROVIDER:
1974 case PT_SHARE:
1975 /*return (LPSTR)&(pdata->u.network.szNames);*/
1976 return NULL;
1977
1978 case PT_VALUEW:
1979 return (LPWSTR)pdata->u.file.szNames;
1980 }
1981 return NULL;
1982 }
1983
1984
1985 /**************************************************************************
1986 * _ILGetTextPointer()
1987 * gets a pointer to the long filename string stored in the pidl
1988 */
1989 LPSTR _ILGetTextPointer(LPCITEMIDLIST pidl)
1990 {
1991 /* TRACE(pidl,"(pidl%p)\n", pidl);*/
1992
1993 LPPIDLDATA pdata = _ILGetDataPointer(pidl);
1994
1995 if (!pdata)
1996 return NULL;
1997
1998 switch (pdata->type)
1999 {
2000 case PT_GUID:
2001 case PT_SHELLEXT:
2002 case PT_YAGUID:
2003 return NULL;
2004
2005 case PT_DRIVE:
2006 case PT_DRIVE1:
2007 case PT_DRIVE2:
2008 case PT_DRIVE3:
2009 return pdata->u.drive.szDriveName;
2010
2011 case PT_FOLDER:
2012 case PT_FOLDER1:
2013 case PT_VALUE:
2014 case PT_IESPECIAL1:
2015 case PT_IESPECIAL2:
2016 return pdata->u.file.szNames;
2017
2018 case PT_WORKGRP:
2019 case PT_COMP:
2020 case PT_NETWORK:
2021 case PT_NETPROVIDER:
2022 case PT_SHARE:
2023 return pdata->u.network.szNames;
2024
2025 case PT_CPLAPPLET:
2026 return pdata->u.cpanel.szName;
2027 }
2028 return NULL;
2029 }
2030
2031 /**************************************************************************
2032 * _ILGetSTextPointer()
2033 * gets a pointer to the short filename string stored in the pidl
2034 */
2035 static LPSTR _ILGetSTextPointer(LPCITEMIDLIST pidl)
2036 {
2037 /* TRACE(pidl,"(pidl%p)\n", pidl); */
2038
2039 LPPIDLDATA pdata = _ILGetDataPointer(pidl);
2040
2041 if (!pdata)
2042 return NULL;
2043
2044 switch (pdata->type)
2045 {
2046 case PT_FOLDER:
2047 case PT_VALUE:
2048 case PT_IESPECIAL1:
2049 case PT_IESPECIAL2:
2050 return pdata->u.file.szNames + strlen (pdata->u.file.szNames) + 1;
2051
2052 case PT_WORKGRP:
2053 return pdata->u.network.szNames + strlen (pdata->u.network.szNames) + 1;
2054 }
2055 return NULL;
2056 }
2057
2058 /**************************************************************************
2059 * _ILGetGUIDPointer()
2060 *
2061 * returns reference to guid stored in some pidls
2062 */
2063 IID* _ILGetGUIDPointer(LPCITEMIDLIST pidl)
2064 {
2065 LPPIDLDATA pdata = _ILGetDataPointer(pidl);
2066
2067 TRACE("%p\n", pidl);
2068
2069 if (!pdata)
2070 return NULL;
2071
2072 TRACE("pdata->type 0x%04x\n", pdata->type);
2073 switch (pdata->type)
2074 {
2075 case PT_SHELLEXT:
2076 case PT_GUID:
2077 case PT_YAGUID:
2078 return &(pdata->u.guid.guid);
2079
2080 default:
2081 TRACE("Unknown pidl type 0x%04x\n", pdata->type);
2082 break;
2083 }
2084 return NULL;
2085 }
2086
2087 /******************************************************************************
2088 * _ILGetFileStructW [Internal]
2089 *
2090 * Get pointer the a SHITEMID's FileStructW field if present
2091 *
2092 * PARAMS
2093 * pidl [I] The SHITEMID
2094 *
2095 * RETURNS
2096 * Success: Pointer to pidl's FileStructW field.
2097 * Failure: NULL
2098 */
2099 FileStructW* _ILGetFileStructW(LPCITEMIDLIST pidl) {
2100 FileStructW *pFileStructW;
2101 WORD cbOffset;
2102
2103 if (!(_ILIsValue(pidl) || _ILIsFolder(pidl)))
2104 return NULL;
2105
2106 cbOffset = *(const WORD *)((const BYTE *)pidl + pidl->mkid.cb - sizeof(WORD));
2107 pFileStructW = (FileStructW*)((LPBYTE)pidl + cbOffset);
2108
2109 /* Currently I don't see a fool prove way to figure out if a pidl is for sure of WinXP
2110 * style with a FileStructW member. If we switch all our shellfolder-implementations to
2111 * the new format, this won't be a problem. For now, we do as many sanity checks as possible. */
2112 if (cbOffset & 0x1 || /* FileStructW member is word aligned in the pidl */
2113 /* FileStructW is positioned after FileStruct */
2114 cbOffset < sizeof(pidl->mkid.cb) + sizeof(PIDLTYPE) + sizeof(FileStruct) ||
2115 /* There has to be enough space at cbOffset in the pidl to hold FileStructW and cbOffset */
2116 cbOffset > pidl->mkid.cb - sizeof(cbOffset) - sizeof(FileStructW) ||
2117 pidl->mkid.cb != cbOffset + pFileStructW->cbLen)
2118 {
2119 ERR("Invalid pidl format (cbOffset = %d)!\n", cbOffset);
2120 return NULL;
2121 }
2122
2123 return pFileStructW;
2124 }
2125
2126 /*************************************************************************
2127 * _ILGetFileDateTime
2128 *
2129 * Given the ItemIdList, get the FileTime
2130 *
2131 * PARAMS
2132 * pidl [I] The ItemIDList
2133 * pFt [I] the resulted FILETIME of the file
2134 *
2135 * RETURNS
2136 * True if Successful
2137 *
2138 * NOTES
2139 *
2140 */
2141 BOOL _ILGetFileDateTime(LPCITEMIDLIST pidl, FILETIME *pFt)
2142 {
2143 LPPIDLDATA pdata = _ILGetDataPointer(pidl);
2144
2145 if (!pdata)
2146 return FALSE;
2147
2148 switch (pdata->type)
2149 {
2150 case PT_FOLDER:
2151 case PT_VALUE:
2152 DosDateTimeToFileTime(pdata->u.file.uFileDate, pdata->u.file.uFileTime, pFt);
2153 break;
2154 default:
2155 return FALSE;
2156 }
2157 return TRUE;
2158 }
2159
2160 BOOL _ILGetFileDate (LPCITEMIDLIST pidl, LPSTR pOut, UINT uOutSize)
2161 {
2162 FILETIME ft, lft;
2163 SYSTEMTIME time;
2164 BOOL ret;
2165
2166 if (_ILGetFileDateTime( pidl, &ft ))
2167 {
2168 FileTimeToLocalFileTime(&ft, &lft);
2169 FileTimeToSystemTime (&lft, &time);
2170
2171 ret = GetDateFormatA(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &time, NULL, pOut, uOutSize);
2172 if (ret)
2173 {
2174 /* Append space + time without seconds */
2175 pOut[ret-1] = ' ';
2176 GetTimeFormatA(LOCALE_USER_DEFAULT, TIME_NOSECONDS, &time, NULL, &pOut[ret], uOutSize - ret);
2177 }
2178 }
2179 else
2180 {
2181 pOut[0] = '\0';
2182 ret = FALSE;
2183 }
2184 return ret;
2185 }
2186
2187 /*************************************************************************
2188 * _ILGetFileSize
2189 *
2190 * Given the ItemIdList, get the FileSize
2191 *
2192 * PARAMS
2193 * pidl [I] The ItemIDList
2194 * pOut [I] The buffer to save the result
2195 * uOutsize [I] The size of the buffer
2196 *
2197 * RETURNS
2198 * The FileSize
2199 *
2200 * NOTES
2201 * pOut can be null when no string is needed
2202 *
2203 */
2204 DWORD _ILGetFileSize (LPCITEMIDLIST pidl, LPSTR pOut, UINT uOutSize)
2205 {
2206 LPPIDLDATA pdata = _ILGetDataPointer(pidl);
2207 DWORD dwSize;
2208
2209 if (!pdata)
2210 return 0;
2211
2212 switch (pdata->type)
2213 {
2214 case PT_VALUE:
2215 dwSize = pdata->u.file.dwFileSize;
2216 if (pOut)
2217 StrFormatKBSizeA(dwSize, pOut, uOutSize);
2218 return dwSize;
2219 }
2220 if (pOut)
2221 *pOut = 0x00;
2222 return 0;
2223 }
2224
2225 BOOL _ILGetExtension (LPCITEMIDLIST pidl, LPSTR pOut, UINT uOutSize)
2226 {
2227 char szTemp[MAX_PATH];
2228 const char * pPoint;
2229 LPCITEMIDLIST pidlTemp = pidl;
2230
2231 TRACE("pidl=%p\n", pidl);
2232
2233 if (!pidl)
2234 return FALSE;
2235
2236 pidlTemp = ILFindLastID(pidl);
2237
2238 if (!_ILIsValue(pidlTemp))
2239 return FALSE;
2240 if (!_ILSimpleGetText(pidlTemp, szTemp, MAX_PATH))
2241 return FALSE;
2242
2243 pPoint = PathFindExtensionA(szTemp);
2244
2245 if (!*pPoint)
2246 return FALSE;
2247
2248 pPoint++;
2249 lstrcpynA(pOut, pPoint, uOutSize);
2250 TRACE("%s\n", pOut);
2251
2252 return TRUE;
2253 }
2254
2255 /*************************************************************************
2256 * _ILGetFileType
2257 *
2258 * Given the ItemIdList, get the file type description
2259 *
2260 * PARAMS
2261 * pidl [I] The ItemIDList (simple)
2262 * pOut [I] The buffer to save the result
2263 * uOutsize [I] The size of the buffer
2264 *
2265 * RETURNS
2266 * nothing
2267 *
2268 * NOTES
2269 * This function copies as much as possible into the buffer.
2270 */
2271 void _ILGetFileType(LPCITEMIDLIST pidl, LPSTR pOut, UINT uOutSize)
2272 {
2273 char sType[64];
2274
2275 if(_ILIsValue(pidl))
2276 {
2277 char sTemp[64];
2278
2279 if(uOutSize > 0)
2280 pOut[0] = 0;
2281 if (_ILGetExtension (pidl, sType, 64))
2282 {
2283 if (HCR_MapTypeToValueA(sType, sTemp, 64, TRUE))
2284 {
2285 /* retrieve description */
2286 if(HCR_MapTypeToValueA(sTemp, pOut, uOutSize, FALSE ))
2287 return;
2288 }
2289 /* display Ext-file as description */
2290 strcpy(pOut, sType);
2291 _strupr(pOut);
2292 /* load localized file string */
2293 sTemp[0] = '\0';
2294 if(LoadStringA(shell32_hInstance, IDS_SHV_COLUMN1, sTemp, 64))
2295 {
2296 sTemp[63] = '\0';
2297 strcat(pOut, "-");
2298 strcat(pOut, sTemp);
2299 }
2300 }
2301 }
2302 else
2303 {
2304 pOut[0] = '\0';
2305 LoadStringA(shell32_hInstance, IDS_DIRECTORY, pOut, uOutSize);
2306 /* make sure its null terminated */
2307 pOut[uOutSize-1] = '\0';
2308 }
2309 }
2310
2311 /*************************************************************************
2312 * _ILGetFileAttributes
2313 *
2314 * Given the ItemIdList, get the Attrib string format
2315 *
2316 * PARAMS
2317 * pidl [I] The ItemIDList
2318 * pOut [I] The buffer to save the result
2319 * uOutsize [I] The size of the Buffer
2320 *
2321 * RETURNS
2322 * Attributes
2323 *
2324 * FIXME
2325 * return value 0 in case of error is a valid return value
2326 *
2327 */
2328 DWORD _ILGetFileAttributes(LPCITEMIDLIST pidl, LPSTR pOut, UINT uOutSize)
2329 {
2330 LPPIDLDATA pData = _ILGetDataPointer(pidl);
2331 WORD wAttrib = 0;
2332 int i;
2333
2334 if (!pData)
2335 return 0;
2336
2337 switch(pData->type)
2338 {
2339 case PT_FOLDER:
2340 case PT_VALUE:
2341 wAttrib = pData->u.file.uFileAttribs;
2342 break;
2343 }
2344
2345 if(uOutSize >= 6)
2346 {
2347 i = 0;
2348 if(wAttrib & FILE_ATTRIBUTE_READONLY)
2349 pOut[i++] = 'R';
2350 if(wAttrib & FILE_ATTRIBUTE_HIDDEN)
2351 pOut[i++] = 'H';
2352 if(wAttrib & FILE_ATTRIBUTE_SYSTEM)
2353 pOut[i++] = 'S';
2354 if(wAttrib & FILE_ATTRIBUTE_ARCHIVE)
2355 pOut[i++] = 'A';
2356 if(wAttrib & FILE_ATTRIBUTE_COMPRESSED)
2357 pOut[i++] = 'C';
2358 pOut[i] = 0x00;
2359 }
2360 return wAttrib;
2361 }
2362
2363 /*************************************************************************
2364 * ILFreeaPidl
2365 *
2366 * free a aPidl struct
2367 */
2368 void _ILFreeaPidl(LPITEMIDLIST * apidl, UINT cidl)
2369 {
2370 UINT i;
2371
2372 if (apidl)
2373 {
2374 for (i = 0; i < cidl; i++)
2375 SHFree(apidl[i]);
2376 SHFree(apidl);
2377 }
2378 }
2379
2380 /*************************************************************************
2381 * ILCopyaPidl
2382 *
2383 * copies an aPidl struct
2384 */
2385 LPITEMIDLIST* _ILCopyaPidl(const LPCITEMIDLIST * apidlsrc, UINT cidl)
2386 {
2387 UINT i;
2388 LPITEMIDLIST *apidldest;
2389
2390 apidldest = (LPITEMIDLIST *)SHAlloc(cidl * sizeof(LPITEMIDLIST));
2391 if (!apidlsrc)
2392 return NULL;
2393
2394 for (i = 0; i < cidl; i++)
2395 apidldest[i] = ILClone(apidlsrc[i]);
2396
2397 return apidldest;
2398 }
2399
2400 /*************************************************************************
2401 * _ILCopyCidaToaPidl
2402 *
2403 * creates aPidl from CIDA
2404 */
2405 LPITEMIDLIST* _ILCopyCidaToaPidl(LPITEMIDLIST* pidl, const CIDA * cida)
2406 {
2407 UINT i;
2408 LPITEMIDLIST *dst;
2409
2410 dst = (LPITEMIDLIST *)SHAlloc(cida->cidl * sizeof(LPITEMIDLIST));
2411 if (!dst)
2412 return NULL;
2413
2414 if (pidl)
2415 *pidl = ILClone((LPCITEMIDLIST)(&((const BYTE*)cida)[cida->aoffset[0]]));
2416
2417 for (i = 0; i < cida->cidl; i++)
2418 dst[i] = ILClone((LPCITEMIDLIST)(&((const BYTE*)cida)[cida->aoffset[i + 1]]));
2419
2420 return dst;
2421 }