7f877b99ba2231e33cf626e3681fb0f876ae14ec
[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;
757 UINT len = 0;
758
759 if (pidl)
760 {
761 si = &(pidl->mkid);
762
763 while (si->cb)
764 {
765 len += si->cb;
766 si = (LPCSHITEMID)(((const BYTE*)si) + si->cb);
767 }
768 len += 2;
769 }
770 TRACE("pidl=%p size=%u\n", pidl, len);
771 return len;
772 }
773
774 /*************************************************************************
775 * ILGetNext [SHELL32.153]
776 *
777 * Gets the next ItemID of an ItemIDList
778 *
779 * PARAMS
780 * pidl [I] ItemIDList
781 *
782 * RETURNS
783 * null -> null
784 * desktop -> null
785 * simple pidl -> pointer to 0x0000 element
786 *
787 * NOTES
788 * exported by ordinal.
789 */
790 LPITEMIDLIST WINAPI ILGetNext(LPCITEMIDLIST pidl)
791 {
792 WORD len;
793
794 TRACE("%p\n", pidl);
795
796 if (pidl)
797 {
798 len = pidl->mkid.cb;
799 if (len)
800 {
801 pidl = (LPCITEMIDLIST) (((const BYTE*)pidl) + len);
802 TRACE("-- %p\n", pidl);
803 return (LPITEMIDLIST)pidl;
804 }
805 }
806 return NULL;
807 }
808
809 /*************************************************************************
810 * ILAppend [SHELL32.154]
811 *
812 * Adds the single ItemID item to the ItemIDList indicated by pidl.
813 * If bEnd is FALSE, inserts the item in the front of the list,
814 * otherwise it adds the item to the end. (???)
815 *
816 * PARAMS
817 * pidl [I] ItemIDList to extend
818 * item [I] ItemID to prepend/append
819 * bEnd [I] Indicates if the item should be appended
820 *
821 * NOTES
822 * Destroys the passed in idlist! (???)
823 */
824 EXTERN_C LPITEMIDLIST WINAPI ILAppend(LPITEMIDLIST pidl, LPCITEMIDLIST item, BOOL bEnd)
825 {
826 LPITEMIDLIST idlRet;
827
828 WARN("(pidl=%p,pidl=%p,%08u)semi-stub\n", pidl, item, bEnd);
829
830 pdump (pidl);
831 pdump (item);
832
833 if (_ILIsDesktop(pidl))
834 {
835 idlRet = ILClone(item);
836 SHFree (pidl);
837 return idlRet;
838 }
839
840 if (bEnd)
841 idlRet = ILCombine(pidl, item);
842 else
843 idlRet = ILCombine(item, pidl);
844
845 SHFree(pidl);
846 return idlRet;
847 }
848
849 /*************************************************************************
850 * ILFree [SHELL32.155]
851 *
852 * Frees memory (if not NULL) allocated by SHMalloc allocator
853 *
854 * PARAMS
855 * pidl [I]
856 *
857 * RETURNS
858 * Nothing
859 *
860 * NOTES
861 * exported by ordinal
862 */
863 void WINAPI ILFree(LPITEMIDLIST pidl)
864 {
865 TRACE("(pidl=%p)\n", pidl);
866 SHFree(pidl);
867 }
868
869 /*************************************************************************
870 * ILGlobalFree [SHELL32.156]
871 *
872 * Frees memory (if not NULL) allocated by Alloc allocator
873 *
874 * PARAMS
875 * pidl [I]
876 *
877 * RETURNS
878 * Nothing
879 *
880 * NOTES
881 * exported by ordinal.
882 */
883 void WINAPI ILGlobalFree( LPITEMIDLIST pidl)
884 {
885 TRACE("%p\n", pidl);
886
887 Free(pidl);
888 }
889
890 /*************************************************************************
891 * ILCreateFromPathA [SHELL32.189]
892 *
893 * Creates a complex ItemIDList from a path and returns it.
894 *
895 * PARAMS
896 * path [I]
897 *
898 * RETURNS
899 * the newly created complex ItemIDList or NULL if failed
900 *
901 * NOTES
902 * exported by ordinal.
903 */
904 LPITEMIDLIST WINAPI ILCreateFromPathA (LPCSTR path)
905 {
906 LPITEMIDLIST pidlnew = NULL;
907
908 TRACE_(shell)("%s\n", debugstr_a(path));
909
910 if (SUCCEEDED(SHILCreateFromPathA(path, &pidlnew, NULL)))
911 return pidlnew;
912 return NULL;
913 }
914
915 /*************************************************************************
916 * ILCreateFromPathW [SHELL32.190]
917 *
918 * See ILCreateFromPathA.
919 */
920 LPITEMIDLIST WINAPI ILCreateFromPathW (LPCWSTR path)
921 {
922 LPITEMIDLIST pidlnew = NULL;
923
924 TRACE_(shell)("%s\n", debugstr_w(path));
925
926 if (SUCCEEDED(SHILCreateFromPathW(path, &pidlnew, NULL)))
927 return pidlnew;
928 return NULL;
929 }
930
931 /*************************************************************************
932 * ILCreateFromPath [SHELL32.157]
933 */
934 EXTERN_C LPITEMIDLIST WINAPI ILCreateFromPathAW (LPCVOID path)
935 {
936 if ( SHELL_OsIsUnicode())
937 return ILCreateFromPathW ((LPCWSTR)path);
938 return ILCreateFromPathA ((LPCSTR)path);
939 }
940
941 /*************************************************************************
942 * _ILParsePathW [internal]
943 *
944 * Creates an ItemIDList from a path and returns it.
945 *
946 * PARAMS
947 * path [I] path to parse and convert into an ItemIDList
948 * lpFindFile [I] pointer to buffer to initialize the FileSystem
949 * Bind Data object with
950 * bBindCtx [I] indicates to create a BindContext and assign a
951 * FileSystem Bind Data object
952 * ppidl [O] the newly create ItemIDList
953 * prgfInOut [I/O] requested attributes on input and actual
954 * attributes on return
955 *
956 * RETURNS
957 * NO_ERROR on success or an OLE error code
958 *
959 * NOTES
960 * If either lpFindFile is non-NULL or bBindCtx is TRUE, this function
961 * creates a BindContext object and assigns a FileSystem Bind Data object
962 * to it, passing the BindContext to IShellFolder_ParseDisplayName. Each
963 * IShellFolder uses that FileSystem Bind Data object of the BindContext
964 * to pass data about the current path element to the next object. This
965 * is used to avoid having to verify the current path element on disk, so
966 * that creating an ItemIDList from a nonexistent path still can work.
967 */
968 static HRESULT _ILParsePathW(LPCWSTR path, LPWIN32_FIND_DATAW lpFindFile,
969 BOOL bBindCtx, LPITEMIDLIST *ppidl, LPDWORD prgfInOut)
970 {
971 CComPtr<IShellFolder> pSF;
972 CComPtr<IBindCtx> pBC;
973 HRESULT ret;
974
975 TRACE("%s %p %d (%p)->%p (%p)->0x%x\n", debugstr_w(path), lpFindFile, bBindCtx,
976 ppidl, ppidl ? *ppidl : NULL,
977 prgfInOut, prgfInOut ? *prgfInOut : 0);
978
979 ret = SHGetDesktopFolder(&pSF);
980 if (FAILED(ret))
981 return ret;
982
983 if (lpFindFile || bBindCtx)
984 ret = IFileSystemBindData_Constructor(lpFindFile, &pBC);
985
986 if (SUCCEEDED(ret))
987 {
988 ret = pSF->ParseDisplayName(0, pBC, (LPOLESTR)path, NULL, ppidl, prgfInOut);
989 }
990
991 if (FAILED(ret) && ppidl)
992 *ppidl = NULL;
993
994 TRACE("%s %p 0x%x\n", debugstr_w(path), ppidl ? *ppidl : NULL, prgfInOut ? *prgfInOut : 0);
995
996 return ret;
997 }
998
999 /*************************************************************************
1000 * SHSimpleIDListFromPath [SHELL32.162]
1001 *
1002 * Creates a simple ItemIDList from a path and returns it. This function
1003 * does not fail on nonexistent paths.
1004 *
1005 * PARAMS
1006 * path [I] path to parse and convert into an ItemIDList
1007 *
1008 * RETURNS
1009 * the newly created simple ItemIDList
1010 *
1011 * NOTES
1012 * Simple in the name does not mean a relative ItemIDList but rather a
1013 * fully qualified list, where only the file name is filled in and the
1014 * directory flag for those ItemID elements this is known about, eg.
1015 * it is not the last element in the ItemIDList or the actual directory
1016 * exists on disk.
1017 * exported by ordinal.
1018 */
1019 LPITEMIDLIST WINAPI SHSimpleIDListFromPathA(LPCSTR lpszPath)
1020 {
1021 LPITEMIDLIST pidl = NULL;
1022 LPWSTR wPath = NULL;
1023 int len;
1024
1025 TRACE("%s\n", debugstr_a(lpszPath));
1026
1027 if (lpszPath)
1028 {
1029 len = MultiByteToWideChar(CP_ACP, 0, lpszPath, -1, NULL, 0);
1030 wPath = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1031 MultiByteToWideChar(CP_ACP, 0, lpszPath, -1, wPath, len);
1032 }
1033
1034 _ILParsePathW(wPath, NULL, TRUE, &pidl, NULL);
1035
1036 HeapFree(GetProcessHeap(), 0, wPath);
1037 TRACE("%s %p\n", debugstr_a(lpszPath), pidl);
1038 return pidl;
1039 }
1040
1041 LPITEMIDLIST WINAPI SHSimpleIDListFromPathW(LPCWSTR lpszPath)
1042 {
1043 LPITEMIDLIST pidl = NULL;
1044
1045 TRACE("%s\n", debugstr_w(lpszPath));
1046
1047 _ILParsePathW(lpszPath, NULL, TRUE, &pidl, NULL);
1048 TRACE("%s %p\n", debugstr_w(lpszPath), pidl);
1049 return pidl;
1050 }
1051
1052 EXTERN_C LPITEMIDLIST WINAPI SHSimpleIDListFromPathAW(LPCVOID lpszPath)
1053 {
1054 if ( SHELL_OsIsUnicode())
1055 return SHSimpleIDListFromPathW ((LPCWSTR)lpszPath);
1056 return SHSimpleIDListFromPathA ((LPCSTR)lpszPath);
1057 }
1058
1059 /*************************************************************************
1060 * SHGetDataFromIDListA [SHELL32.247]
1061 *
1062 * NOTES
1063 * the pidl can be a simple one. since we can't get the path out of the pidl
1064 * we have to take all data from the pidl
1065 */
1066 HRESULT WINAPI SHGetDataFromIDListA(LPSHELLFOLDER psf, LPCITEMIDLIST pidl,
1067 int nFormat, LPVOID dest, int len)
1068 {
1069 LPSTR filename, shortname;
1070 WIN32_FIND_DATAA * pfd;
1071
1072 TRACE_(shell)("sf=%p pidl=%p 0x%04x %p 0x%04x stub\n", psf, pidl, nFormat, dest, len);
1073
1074 pdump(pidl);
1075 if (!psf || !dest)
1076 return E_INVALIDARG;
1077
1078 switch (nFormat)
1079 {
1080 case SHGDFIL_FINDDATA:
1081 pfd = (WIN32_FIND_DATAA *)dest;
1082
1083 if (_ILIsDrive(pidl) || _ILIsSpecialFolder(pidl))
1084 return E_INVALIDARG;
1085
1086 if (len < (int)sizeof(WIN32_FIND_DATAA))
1087 return E_INVALIDARG;
1088
1089 ZeroMemory(pfd, sizeof (WIN32_FIND_DATAA));
1090 _ILGetFileDateTime( pidl, &(pfd->ftLastWriteTime));
1091 pfd->dwFileAttributes = _ILGetFileAttributes(pidl, NULL, 0);
1092 pfd->nFileSizeLow = _ILGetFileSize ( pidl, NULL, 0);
1093
1094 filename = _ILGetTextPointer(pidl);
1095 shortname = _ILGetSTextPointer(pidl);
1096
1097 if (filename)
1098 lstrcpynA(pfd->cFileName, filename, sizeof(pfd->cFileName));
1099 else
1100 pfd->cFileName[0] = '\0';
1101
1102 if (shortname)
1103 lstrcpynA(pfd->cAlternateFileName, shortname, sizeof(pfd->cAlternateFileName));
1104 else
1105 pfd->cAlternateFileName[0] = '\0';
1106 return S_OK;
1107
1108 case SHGDFIL_NETRESOURCE:
1109 case SHGDFIL_DESCRIPTIONID:
1110 FIXME_(shell)("SHGDFIL %i stub\n", nFormat);
1111 break;
1112
1113 default:
1114 ERR_(shell)("Unknown SHGDFIL %i, please report\n", nFormat);
1115 }
1116
1117 return E_INVALIDARG;
1118 }
1119
1120 /*************************************************************************
1121 * SHGetDataFromIDListW [SHELL32.248]
1122 *
1123 */
1124 HRESULT WINAPI SHGetDataFromIDListW(LPSHELLFOLDER psf, LPCITEMIDLIST pidl,
1125 int nFormat, LPVOID dest, int len)
1126 {
1127 LPSTR filename, shortname;
1128 WIN32_FIND_DATAW * pfd = (WIN32_FIND_DATAW *)dest;
1129
1130 TRACE_(shell)("sf=%p pidl=%p 0x%04x %p 0x%04x stub\n", psf, pidl, nFormat, dest, len);
1131
1132 pdump(pidl);
1133
1134 if (!psf || !dest)
1135 return E_INVALIDARG;
1136
1137 switch (nFormat)
1138 {
1139 case SHGDFIL_FINDDATA:
1140 pfd = (WIN32_FIND_DATAW *)dest;
1141
1142 if (_ILIsDrive(pidl))
1143 return E_INVALIDARG;
1144
1145 if (len < (int)sizeof(WIN32_FIND_DATAW))
1146 return E_INVALIDARG;
1147
1148 ZeroMemory(pfd, sizeof (WIN32_FIND_DATAW));
1149 _ILGetFileDateTime( pidl, &(pfd->ftLastWriteTime));
1150 pfd->dwFileAttributes = _ILGetFileAttributes(pidl, NULL, 0);
1151 pfd->nFileSizeLow = _ILGetFileSize ( pidl, NULL, 0);
1152
1153 filename = _ILGetTextPointer(pidl);
1154 shortname = _ILGetSTextPointer(pidl);
1155
1156 if (!filename)
1157 pfd->cFileName[0] = '\0';
1158 else if (!MultiByteToWideChar(CP_ACP, 0, filename, -1, pfd->cFileName, MAX_PATH))
1159 pfd->cFileName[MAX_PATH-1] = 0;
1160
1161 if (!shortname)
1162 pfd->cAlternateFileName[0] = '\0';
1163 else if (!MultiByteToWideChar(CP_ACP, 0, shortname, -1, pfd->cAlternateFileName, 14))
1164 pfd->cAlternateFileName[13] = 0;
1165 return S_OK;
1166
1167 case SHGDFIL_NETRESOURCE:
1168 case SHGDFIL_DESCRIPTIONID:
1169 FIXME_(shell)("SHGDFIL %i stub\n", nFormat);
1170 break;
1171
1172 default:
1173 ERR_(shell)("Unknown SHGDFIL %i, please report\n", nFormat);
1174 }
1175
1176 return E_INVALIDARG;
1177 }
1178
1179 /*************************************************************************
1180 * SHGetPathFromIDListA [SHELL32.@][NT 4.0: SHELL32.220]
1181 *
1182 * PARAMETERS
1183 * pidl, [IN] pidl
1184 * pszPath [OUT] path
1185 *
1186 * RETURNS
1187 * path from a passed PIDL.
1188 *
1189 * NOTES
1190 * NULL returns FALSE
1191 * desktop pidl gives path to desktop directory back
1192 * special pidls returning FALSE
1193 */
1194 BOOL WINAPI SHGetPathFromIDListA(LPCITEMIDLIST pidl, LPSTR pszPath)
1195 {
1196 WCHAR wszPath[MAX_PATH];
1197 BOOL bSuccess;
1198
1199 bSuccess = SHGetPathFromIDListW(pidl, wszPath);
1200 WideCharToMultiByte(CP_ACP, 0, wszPath, -1, pszPath, MAX_PATH, NULL, NULL);
1201
1202 return bSuccess;
1203 }
1204
1205 /*************************************************************************
1206 * SHGetPathFromIDListW [SHELL32.@]
1207 *
1208 * See SHGetPathFromIDListA.
1209 */
1210 BOOL WINAPI SHGetPathFromIDListW(LPCITEMIDLIST pidl, LPWSTR pszPath)
1211 {
1212 HRESULT hr;
1213 LPCITEMIDLIST pidlLast;
1214 CComPtr<IShellFolder> psfFolder;
1215 DWORD dwAttributes;
1216 STRRET strret;
1217
1218 TRACE_(shell)("(pidl=%p,%p)\n", pidl, pszPath);
1219 pdump(pidl);
1220
1221 *pszPath = '\0';
1222 if (!pidl)
1223 return FALSE;
1224
1225 hr = SHBindToParent(pidl, IID_PPV_ARG(IShellFolder, &psfFolder), &pidlLast);
1226 if (FAILED(hr))
1227 {
1228 ERR("SHBindToParent failed: %x\n", hr);
1229 return FALSE;
1230 }
1231
1232 dwAttributes = SFGAO_FILESYSTEM;
1233 hr = psfFolder->GetAttributesOf(1, &pidlLast, &dwAttributes);
1234 if (FAILED(hr) || !(dwAttributes & SFGAO_FILESYSTEM))
1235 {
1236 WARN("Wrong dwAttributes or GetAttributesOf failed: %x\n", hr);
1237 return FALSE;
1238 }
1239
1240 hr = psfFolder->GetDisplayNameOf(pidlLast, SHGDN_FORPARSING, &strret);
1241 if (FAILED(hr)) return FALSE;
1242
1243 hr = StrRetToBufW(&strret, pidlLast, pszPath, MAX_PATH);
1244
1245 TRACE_(shell)("-- %s, 0x%08x\n", debugstr_w(pszPath), hr);
1246 return SUCCEEDED(hr);
1247 }
1248
1249 /*************************************************************************
1250 * SHBindToParent [shell version 5.0]
1251 */
1252 HRESULT WINAPI SHBindToParent(LPCITEMIDLIST pidl, REFIID riid, LPVOID *ppv, LPCITEMIDLIST *ppidlLast)
1253 {
1254 CComPtr<IShellFolder> psfDesktop;
1255 HRESULT hr = E_FAIL;
1256
1257 TRACE_(shell)("pidl=%p\n", pidl);
1258 pdump(pidl);
1259
1260 if (!pidl || !ppv)
1261 return E_INVALIDARG;
1262
1263 *ppv = NULL;
1264 if (ppidlLast)
1265 *ppidlLast = NULL;
1266
1267 hr = SHGetDesktopFolder(&psfDesktop);
1268 if (FAILED(hr))
1269 return hr;
1270
1271 if (_ILIsPidlSimple(pidl))
1272 {
1273 /* we are on desktop level */
1274 hr = psfDesktop->QueryInterface(riid, ppv);
1275 }
1276 else
1277 {
1278 LPITEMIDLIST pidlParent = ILClone(pidl);
1279 ILRemoveLastID(pidlParent);
1280 hr = psfDesktop->BindToObject(pidlParent, NULL, riid, ppv);
1281 SHFree (pidlParent);
1282 }
1283
1284 if (SUCCEEDED(hr) && ppidlLast)
1285 *ppidlLast = ILFindLastID(pidl);
1286
1287 TRACE_(shell)("-- psf=%p pidl=%p ret=0x%08x\n", *ppv, (ppidlLast) ? *ppidlLast : NULL, hr);
1288 return hr;
1289 }
1290
1291 /**************************************************************************
1292 *
1293 * internal functions
1294 *
1295 * ### 1. section creating pidls ###
1296 *
1297 *************************************************************************
1298 */
1299
1300 /* Basic PIDL constructor. Allocates size + 5 bytes, where:
1301 * - two bytes are SHITEMID.cb
1302 * - one byte is PIDLDATA.type
1303 * - two bytes are the NULL PIDL terminator
1304 * Sets type of the returned PIDL to type.
1305 */
1306 static LPITEMIDLIST _ILAlloc(PIDLTYPE type, unsigned int size)
1307 {
1308 LPITEMIDLIST pidlOut = NULL;
1309
1310 pidlOut = (LPITEMIDLIST)SHAlloc(size + 5);
1311 if(pidlOut)
1312 {
1313 LPPIDLDATA pData;
1314 LPITEMIDLIST pidlNext;
1315
1316 ZeroMemory(pidlOut, size + 5);
1317 pidlOut->mkid.cb = size + 3;
1318
1319 pData = _ILGetDataPointer(pidlOut);
1320 if (pData)
1321 pData->type = type;
1322
1323 pidlNext = ILGetNext(pidlOut);
1324 if (pidlNext)
1325 pidlNext->mkid.cb = 0x00;
1326 TRACE("-- (pidl=%p, size=%u)\n", pidlOut, size);
1327 }
1328
1329 return pidlOut;
1330 }
1331
1332 LPITEMIDLIST _ILCreateDesktop(void)
1333 {
1334 LPITEMIDLIST ret;
1335
1336 TRACE("()\n");
1337 ret = (LPITEMIDLIST)SHAlloc(2);
1338 if (ret)
1339 ret->mkid.cb = 0;
1340 return ret;
1341 }
1342
1343 LPITEMIDLIST _ILCreateMyComputer(void)
1344 {
1345 TRACE("()\n");
1346 return _ILCreateGuid(PT_GUID, CLSID_MyComputer);
1347 }
1348
1349 LPITEMIDLIST _ILCreateMyDocuments(void)
1350 {
1351 TRACE("()\n");
1352 return _ILCreateGuid(PT_GUID, CLSID_MyDocuments);
1353 }
1354
1355 LPITEMIDLIST _ILCreateIExplore(void)
1356 {
1357 TRACE("()\n");
1358 return _ILCreateGuid(PT_GUID, CLSID_Internet);
1359 }
1360
1361 LPITEMIDLIST _ILCreateControlPanel(void)
1362 {
1363 LPITEMIDLIST parent = _ILCreateGuid(PT_GUID, CLSID_MyComputer), ret = NULL;
1364
1365 TRACE("()\n");
1366 if (parent)
1367 {
1368 LPITEMIDLIST cpl = _ILCreateGuid(PT_SHELLEXT, CLSID_ControlPanel);
1369
1370 if (cpl)
1371 {
1372 ret = ILCombine(parent, cpl);
1373 SHFree(cpl);
1374 }
1375 SHFree(parent);
1376 }
1377 return ret;
1378 }
1379
1380 LPITEMIDLIST _ILCreatePrinters(void)
1381 {
1382 LPITEMIDLIST parent = _ILCreateGuid(PT_GUID, CLSID_MyComputer), ret = NULL;
1383
1384 TRACE("()\n");
1385 if (parent)
1386 {
1387 LPITEMIDLIST printers = _ILCreateGuid(PT_YAGUID, CLSID_Printers);
1388
1389 if (printers)
1390 {
1391 ret = ILCombine(parent, printers);
1392 SHFree(printers);
1393 }
1394 SHFree(parent);
1395 }
1396 return ret;
1397 }
1398
1399 LPITEMIDLIST _ILCreateNetwork(void)
1400 {
1401 TRACE("()\n");
1402 return _ILCreateGuid(PT_GUID, CLSID_NetworkPlaces);
1403 }
1404
1405 LPITEMIDLIST _ILCreateBitBucket(void)
1406 {
1407 TRACE("()\n");
1408 return _ILCreateGuid(PT_GUID, CLSID_RecycleBin);
1409 }
1410
1411 LPITEMIDLIST _ILCreateAdminTools(void)
1412 {
1413 TRACE("()\n");
1414 return _ILCreateGuid(PT_GUID, CLSID_AdminFolderShortcut); //FIXME
1415 }
1416
1417 LPITEMIDLIST _ILCreateGuid(PIDLTYPE type, REFIID guid)
1418 {
1419 LPITEMIDLIST pidlOut;
1420
1421 if (type == PT_SHELLEXT || type == PT_GUID || type == PT_YAGUID)
1422 {
1423 pidlOut = _ILAlloc(type, sizeof(GUIDStruct));
1424 if (pidlOut)
1425 {
1426 LPPIDLDATA pData = _ILGetDataPointer(pidlOut);
1427
1428 pData->u.guid.guid = guid;
1429 TRACE("-- create GUID-pidl %s\n",
1430 debugstr_guid(&(pData->u.guid.guid)));
1431 }
1432 }
1433 else
1434 {
1435 WARN("%d: invalid type for GUID\n", type);
1436 pidlOut = NULL;
1437 }
1438 return pidlOut;
1439 }
1440
1441 LPITEMIDLIST _ILCreateGuidFromStrA(LPCSTR szGUID)
1442 {
1443 IID iid;
1444
1445 if (FAILED(SHCLSIDFromStringA(szGUID, &iid)))
1446 {
1447 ERR("%s is not a GUID\n", szGUID);
1448 return NULL;
1449 }
1450 return _ILCreateGuid(PT_GUID, iid);
1451 }
1452
1453 LPITEMIDLIST _ILCreateGuidFromStrW(LPCWSTR szGUID)
1454 {
1455 IID iid;
1456
1457 if (FAILED(CLSIDFromString((LPOLESTR)szGUID, &iid)))
1458 {
1459 ERR("%s is not a GUID\n", debugstr_w(szGUID));
1460 return NULL;
1461 }
1462 return _ILCreateGuid(PT_GUID, iid);
1463 }
1464
1465 LPITEMIDLIST _ILCreateFromFindDataW( const WIN32_FIND_DATAW *wfd )
1466 {
1467 char buff[MAX_PATH + 14 +1]; /* see WIN32_FIND_DATA */
1468 DWORD len, len1, wlen, alen, cbData;
1469 LPITEMIDLIST pidl;
1470 PIDLTYPE type;
1471
1472 if (!wfd)
1473 return NULL;
1474
1475 TRACE("(%s, %s)\n", debugstr_w(wfd->cAlternateFileName), debugstr_w(wfd->cFileName));
1476
1477 /* prepare buffer with both names */
1478 len = WideCharToMultiByte(CP_ACP, 0, wfd->cFileName, -1, buff, MAX_PATH, NULL, NULL);
1479 len1 = WideCharToMultiByte(CP_ACP, 0, wfd->cAlternateFileName, -1, buff + len, sizeof(buff) - len, NULL, NULL);
1480 alen = len + len1;
1481
1482 type = (wfd->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? PT_FOLDER : PT_VALUE;
1483
1484 wlen = wcslen(wfd->cFileName) + 1;
1485 cbData = sizeof(FileStruct) - 1 + (alen + (alen & 1)); // Note: szNames field is initially 1 byte long
1486 cbData += sizeof(FileStructW) - 1 + wlen * sizeof(WCHAR); // Note: wszName field is initially 1 byte long
1487 cbData += sizeof(WORD); // offset to FileStructW
1488 pidl = _ILAlloc(type, cbData);
1489 if (pidl)
1490 {
1491 LPPIDLDATA pData = _ILGetDataPointer(pidl);
1492 FileStruct *fs = &pData->u.file;
1493 FileStructW *fsw;
1494 WORD *pOffsetW;
1495
1496 FileTimeToDosDateTime( &wfd->ftLastWriteTime, &fs->uFileDate, &fs->uFileTime);
1497 fs->dwFileSize = wfd->nFileSizeLow;
1498 fs->uFileAttribs = wfd->dwFileAttributes;
1499 memcpy(fs->szNames, buff, alen);
1500
1501 fsw = (FileStructW*)(pData->u.file.szNames + alen + (alen & 0x1));
1502 fsw->cbLen = sizeof(FileStructW) - 1 + wlen * sizeof(WCHAR) + sizeof(WORD);
1503 FileTimeToDosDateTime( &wfd->ftCreationTime, &fsw->uCreationDate, &fsw->uCreationTime);
1504 FileTimeToDosDateTime( &wfd->ftLastAccessTime, &fsw->uLastAccessDate, &fsw->uLastAccessTime);
1505 memcpy(fsw->wszName, wfd->cFileName, wlen * sizeof(WCHAR));
1506
1507 pOffsetW = (WORD*)((LPBYTE)pidl + pidl->mkid.cb - sizeof(WORD));
1508 *pOffsetW = (LPBYTE)fsw - (LPBYTE)pidl;
1509 TRACE("-- Set Value: %s\n", debugstr_w(fsw->wszName));
1510 }
1511 return pidl;
1512
1513 }
1514
1515 HRESULT _ILCreateFromPathW(LPCWSTR szPath, LPITEMIDLIST* ppidl)
1516 {
1517 HANDLE hFile;
1518 WIN32_FIND_DATAW stffile;
1519
1520 if (!ppidl)
1521 return E_INVALIDARG;
1522
1523 hFile = FindFirstFileW(szPath, &stffile);
1524 if (hFile == INVALID_HANDLE_VALUE)
1525 return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
1526
1527 FindClose(hFile);
1528
1529 *ppidl = _ILCreateFromFindDataW(&stffile);
1530
1531 return *ppidl ? S_OK : E_OUTOFMEMORY;
1532 }
1533
1534 LPITEMIDLIST _ILCreateDrive(LPCWSTR lpszNew)
1535 {
1536 LPITEMIDLIST pidlOut;
1537
1538 TRACE("(%s)\n", debugstr_w(lpszNew));
1539
1540 pidlOut = _ILAlloc(PT_DRIVE, sizeof(DriveStruct));
1541 if (pidlOut)
1542 {
1543 LPSTR pszDest;
1544
1545 pszDest = _ILGetTextPointer(pidlOut);
1546 if (pszDest)
1547 {
1548 strcpy(pszDest, "x:\\");
1549 pszDest[0] = towupper(lpszNew[0]);
1550 TRACE("-- create Drive: %s\n", debugstr_a(pszDest));
1551 }
1552 }
1553 return pidlOut;
1554 }
1555
1556 /**************************************************************************
1557 * _ILGetDrive()
1558 *
1559 * Gets the text for the drive eg. 'c:\'
1560 *
1561 * RETURNS
1562 * strlen (lpszText)
1563 */
1564 DWORD _ILGetDrive(LPCITEMIDLIST pidl, LPSTR pOut, UINT uSize)
1565 {
1566 TRACE("(%p,%p,%u)\n", pidl, pOut, uSize);
1567
1568 if(_ILIsMyComputer(pidl))
1569 pidl = ILGetNext(pidl);
1570
1571 if (pidl && _ILIsDrive(pidl))
1572 return _ILSimpleGetText(pidl, pOut, uSize);
1573
1574 return 0;
1575 }
1576
1577 /**************************************************************************
1578 *
1579 * ### 2. section testing pidls ###
1580 *
1581 **************************************************************************
1582 * _ILIsUnicode()
1583 * _ILIsDesktop()
1584 * _ILIsMyComputer()
1585 * _ILIsSpecialFolder()
1586 * _ILIsDrive()
1587 * _ILIsFolder()
1588 * _ILIsValue()
1589 * _ILIsPidlSimple()
1590 */
1591 BOOL _ILIsUnicode(LPCITEMIDLIST pidl)
1592 {
1593 LPPIDLDATA lpPData = _ILGetDataPointer(pidl);
1594
1595 TRACE("(%p)\n", pidl);
1596
1597 return (pidl && lpPData && PT_VALUEW == lpPData->type);
1598 }
1599
1600 BOOL _ILIsDesktop(LPCITEMIDLIST pidl)
1601 {
1602 TRACE("(%p)\n", pidl);
1603
1604 return pidl && pidl->mkid.cb ? 0 : 1;
1605 }
1606
1607 BOOL _ILIsMyDocuments(LPCITEMIDLIST pidl)
1608 {
1609 IID *iid = _ILGetGUIDPointer(pidl);
1610
1611 TRACE("(%p)\n", pidl);
1612
1613 if (iid)
1614 return IsEqualIID(*iid, CLSID_MyDocuments);
1615 return FALSE;
1616 }
1617
1618 BOOL _ILIsControlPanel(LPCITEMIDLIST pidl)
1619 {
1620 IID *iid = _ILGetGUIDPointer(pidl);
1621
1622 TRACE("(%p)\n", pidl);
1623
1624 if (iid)
1625 return IsEqualIID(*iid, CLSID_ControlPanel);
1626 return FALSE;
1627 }
1628
1629 BOOL _ILIsNetHood(LPCITEMIDLIST pidl)
1630 {
1631 IID *iid = _ILGetGUIDPointer(pidl);
1632
1633 TRACE("(%p)\n", pidl);
1634
1635 if (iid)
1636 return IsEqualIID(*iid, CLSID_NetworkPlaces);
1637 return FALSE;
1638 }
1639
1640
1641 LPITEMIDLIST _ILCreateNetHood(void)
1642 {
1643 return _ILCreateGuid(PT_GUID, CLSID_NetworkPlaces);
1644 }
1645
1646 LPITEMIDLIST _ILCreateFont(void)
1647 {
1648 return _ILCreateGuid(PT_GUID, CLSID_FontsFolderShortcut);
1649 }
1650
1651 BOOL _ILIsMyComputer(LPCITEMIDLIST pidl)
1652 {
1653 IID *iid = _ILGetGUIDPointer(pidl);
1654
1655 TRACE("(%p)\n", pidl);
1656
1657 if (iid)
1658 return IsEqualIID(*iid, CLSID_MyComputer);
1659 return FALSE;
1660 }
1661
1662 BOOL _ILIsPrinter(LPCITEMIDLIST pidl)
1663 {
1664 IID *iid = _ILGetGUIDPointer(pidl);
1665
1666 TRACE("(%p)\n", pidl);
1667
1668 if (iid)
1669 return IsEqualIID(*iid, CLSID_Printers);
1670 return FALSE;
1671 }
1672
1673 BOOL _ILIsBitBucket(LPCITEMIDLIST pidl)
1674 {
1675 IID *iid = _ILGetGUIDPointer(pidl);
1676
1677 TRACE("(%p)\n", pidl);
1678
1679 if (iid)
1680 return IsEqualIID(*iid, CLSID_RecycleBin);
1681 return FALSE;
1682 }
1683
1684 BOOL _ILIsAdminTools(LPCITEMIDLIST pidl)
1685 {
1686 IID *iid = _ILGetGUIDPointer(pidl);
1687
1688 TRACE("(%p)\n", pidl);
1689
1690 if (iid)
1691 return IsEqualIID(*iid, CLSID_AdminFolderShortcut);
1692 else
1693 return FALSE;
1694 }
1695
1696 BOOL _ILIsSpecialFolder (LPCITEMIDLIST pidl)
1697 {
1698 LPPIDLDATA lpPData = _ILGetDataPointer(pidl);
1699
1700 TRACE("(%p)\n", pidl);
1701
1702 return (pidl && ( (lpPData && (PT_GUID == lpPData->type || PT_SHELLEXT == lpPData->type || PT_YAGUID == lpPData->type)) ||
1703 (pidl && pidl->mkid.cb == 0x00)
1704 ));
1705 }
1706
1707 BOOL _ILIsDrive(LPCITEMIDLIST pidl)
1708 {
1709 LPPIDLDATA lpPData = _ILGetDataPointer(pidl);
1710
1711 TRACE("(%p)\n", pidl);
1712
1713 return (pidl && lpPData && (PT_DRIVE == lpPData->type ||
1714 PT_DRIVE1 == lpPData->type ||
1715 PT_DRIVE2 == lpPData->type ||
1716 PT_DRIVE3 == lpPData->type));
1717 }
1718
1719 BOOL _ILIsFolder(LPCITEMIDLIST pidl)
1720 {
1721 LPPIDLDATA lpPData = _ILGetDataPointer(pidl);
1722
1723 TRACE("(%p)\n", pidl);
1724
1725 return (pidl && lpPData && (PT_FOLDER == lpPData->type || PT_FOLDER1 == lpPData->type));
1726 }
1727
1728 BOOL _ILIsValue(LPCITEMIDLIST pidl)
1729 {
1730 LPPIDLDATA lpPData = _ILGetDataPointer(pidl);
1731
1732 TRACE("(%p)\n", pidl);
1733
1734 return (pidl && lpPData && PT_VALUE == lpPData->type);
1735 }
1736
1737 BOOL _ILIsCPanelStruct(LPCITEMIDLIST pidl)
1738 {
1739 LPPIDLDATA lpPData = _ILGetDataPointer(pidl);
1740
1741 TRACE("(%p)\n", pidl);
1742
1743 return (pidl && lpPData && (lpPData->type == 0));
1744 }
1745
1746 /**************************************************************************
1747 * _ILIsPidlSimple
1748 */
1749 BOOL _ILIsPidlSimple(LPCITEMIDLIST pidl)
1750 {
1751 BOOL ret = TRUE;
1752
1753 if(! _ILIsDesktop(pidl)) /* pidl=NULL or mkid.cb=0 */
1754 {
1755 WORD len = pidl->mkid.cb;
1756 LPCITEMIDLIST pidlnext = (LPCITEMIDLIST) (((const BYTE*)pidl) + len );
1757
1758 if (pidlnext->mkid.cb)
1759 ret = FALSE;
1760 }
1761
1762 TRACE("%s\n", ret ? "Yes" : "No");
1763 return ret;
1764 }
1765
1766 /**************************************************************************
1767 *
1768 * ### 3. section getting values from pidls ###
1769 */
1770
1771 /**************************************************************************
1772 * _ILSimpleGetText
1773 *
1774 * gets the text for the first item in the pidl (eg. simple pidl)
1775 *
1776 * returns the length of the string
1777 */
1778 DWORD _ILSimpleGetText (LPCITEMIDLIST pidl, LPSTR szOut, UINT uOutSize)
1779 {
1780 DWORD dwReturn = 0;
1781 LPSTR szSrc;
1782 LPWSTR szSrcW;
1783 GUID const * riid;
1784 char szTemp[MAX_PATH];
1785
1786 TRACE("(%p %p %x)\n", pidl, szOut, uOutSize);
1787
1788 if (!pidl)
1789 return 0;
1790
1791 if (szOut)
1792 *szOut = 0;
1793
1794 if (_ILIsDesktop(pidl))
1795 {
1796 /* desktop */
1797 if (HCR_GetClassNameA(CLSID_ShellDesktop, szTemp, MAX_PATH))
1798 {
1799 if (szOut)
1800 lstrcpynA(szOut, szTemp, uOutSize);
1801
1802 dwReturn = strlen (szTemp);
1803 }
1804 }
1805 else if (( szSrc = _ILGetTextPointer(pidl) ))
1806 {
1807 /* filesystem */
1808 if (szOut)
1809 lstrcpynA(szOut, szSrc, uOutSize);
1810
1811 dwReturn = strlen(szSrc);
1812 }
1813 else if (( szSrcW = _ILGetTextPointerW(pidl) ))
1814 {
1815 /* unicode filesystem */
1816 WideCharToMultiByte(CP_ACP, 0, szSrcW, -1, szTemp, MAX_PATH, NULL, NULL);
1817
1818 if (szOut)
1819 lstrcpynA(szOut, szTemp, uOutSize);
1820
1821 dwReturn = strlen (szTemp);
1822 }
1823 else if (( riid = _ILGetGUIDPointer(pidl) ))
1824 {
1825 /* special folder */
1826 if (HCR_GetClassNameA(*riid, szTemp, MAX_PATH) )
1827 {
1828 if (szOut)
1829 lstrcpynA(szOut, szTemp, uOutSize);
1830
1831 dwReturn = strlen (szTemp);
1832 }
1833 }
1834 else
1835 {
1836 ERR("-- no text\n");
1837 }
1838
1839 TRACE("-- (%p=%s 0x%08x)\n", szOut, debugstr_a(szOut), dwReturn);
1840 return dwReturn;
1841 }
1842
1843 /**************************************************************************
1844 * _ILSimpleGetTextW
1845 *
1846 * gets the text for the first item in the pidl (eg. simple pidl)
1847 *
1848 * returns the length of the string
1849 */
1850 DWORD _ILSimpleGetTextW (LPCITEMIDLIST pidl, LPWSTR szOut, UINT uOutSize)
1851 {
1852 DWORD dwReturn;
1853 FileStructW *pFileStructW = _ILGetFileStructW(pidl);
1854
1855 TRACE("(%p %p %x)\n", pidl, szOut, uOutSize);
1856
1857 if (pFileStructW) {
1858 lstrcpynW(szOut, pFileStructW->wszName, uOutSize);
1859 dwReturn = wcslen(pFileStructW->wszName);
1860 } else {
1861 GUID const * riid;
1862 WCHAR szTemp[MAX_PATH];
1863 LPSTR szSrc;
1864 LPWSTR szSrcW;
1865 dwReturn = 0;
1866
1867 if (!pidl)
1868 return 0;
1869
1870 if (szOut)
1871 *szOut = 0;
1872
1873 if (_ILIsDesktop(pidl))
1874 {
1875 /* desktop */
1876 if (HCR_GetClassNameW(CLSID_ShellDesktop, szTemp, MAX_PATH))
1877 {
1878 if (szOut)
1879 lstrcpynW(szOut, szTemp, uOutSize);
1880
1881 dwReturn = wcslen (szTemp);
1882 }
1883 }
1884 else if (( szSrcW = _ILGetTextPointerW(pidl) ))
1885 {
1886 /* unicode filesystem */
1887 if (szOut)
1888 lstrcpynW(szOut, szSrcW, uOutSize);
1889
1890 dwReturn = wcslen(szSrcW);
1891 }
1892 else if (( szSrc = _ILGetTextPointer(pidl) ))
1893 {
1894 /* filesystem */
1895 MultiByteToWideChar(CP_ACP, 0, szSrc, -1, szTemp, MAX_PATH);
1896
1897 if (szOut)
1898 lstrcpynW(szOut, szTemp, uOutSize);
1899
1900 dwReturn = wcslen (szTemp);
1901 }
1902 else if (( riid = _ILGetGUIDPointer(pidl) ))
1903 {
1904 /* special folder */
1905 if ( HCR_GetClassNameW(*riid, szTemp, MAX_PATH) )
1906 {
1907 if (szOut)
1908 lstrcpynW(szOut, szTemp, uOutSize);
1909
1910 dwReturn = wcslen (szTemp);
1911 }
1912 }
1913 else
1914 {
1915 ERR("-- no text\n");
1916 }
1917 }
1918
1919 TRACE("-- (%p=%s 0x%08x)\n", szOut, debugstr_w(szOut), dwReturn);
1920 return dwReturn;
1921 }
1922
1923 /**************************************************************************
1924 *
1925 * ### 4. getting pointers to parts of pidls ###
1926 *
1927 **************************************************************************
1928 * _ILGetDataPointer()
1929 */
1930 LPPIDLDATA _ILGetDataPointer(LPCITEMIDLIST pidl)
1931 {
1932 if(pidl && pidl->mkid.cb != 0x00)
1933 return (LPPIDLDATA)pidl->mkid.abID;
1934 return NULL;
1935 }
1936
1937 /**************************************************************************
1938 * _ILGetTextPointerW()
1939 * gets a pointer to the unicode long filename string stored in the pidl
1940 */
1941 static LPWSTR _ILGetTextPointerW(LPCITEMIDLIST pidl)
1942 {
1943 /* TRACE(pidl,"(pidl%p)\n", pidl);*/
1944
1945 LPPIDLDATA pdata = _ILGetDataPointer(pidl);
1946
1947 if (!pdata)
1948 return NULL;
1949
1950 switch (pdata->type)
1951 {
1952 case PT_GUID:
1953 case PT_SHELLEXT:
1954 case PT_YAGUID:
1955 return NULL;
1956
1957 case PT_DRIVE:
1958 case PT_DRIVE1:
1959 case PT_DRIVE2:
1960 case PT_DRIVE3:
1961 /*return (LPSTR)&(pdata->u.drive.szDriveName);*/
1962 return NULL;
1963
1964 case PT_FOLDER:
1965 case PT_FOLDER1:
1966 case PT_VALUE:
1967 case PT_IESPECIAL1:
1968 case PT_IESPECIAL2:
1969 /*return (LPSTR)&(pdata->u.file.szNames);*/
1970 return NULL;
1971
1972 case PT_WORKGRP:
1973 case PT_COMP:
1974 case PT_NETWORK:
1975 case PT_NETPROVIDER:
1976 case PT_SHARE:
1977 /*return (LPSTR)&(pdata->u.network.szNames);*/
1978 return NULL;
1979
1980 case PT_VALUEW:
1981 return (LPWSTR)pdata->u.file.szNames;
1982 }
1983 return NULL;
1984 }
1985
1986
1987 /**************************************************************************
1988 * _ILGetTextPointer()
1989 * gets a pointer to the long filename string stored in the pidl
1990 */
1991 LPSTR _ILGetTextPointer(LPCITEMIDLIST pidl)
1992 {
1993 /* TRACE(pidl,"(pidl%p)\n", pidl);*/
1994
1995 LPPIDLDATA pdata = _ILGetDataPointer(pidl);
1996
1997 if (!pdata)
1998 return NULL;
1999
2000 switch (pdata->type)
2001 {
2002 case PT_GUID:
2003 case PT_SHELLEXT:
2004 case PT_YAGUID:
2005 return NULL;
2006
2007 case PT_DRIVE:
2008 case PT_DRIVE1:
2009 case PT_DRIVE2:
2010 case PT_DRIVE3:
2011 return pdata->u.drive.szDriveName;
2012
2013 case PT_FOLDER:
2014 case PT_FOLDER1:
2015 case PT_VALUE:
2016 case PT_IESPECIAL1:
2017 case PT_IESPECIAL2:
2018 return pdata->u.file.szNames;
2019
2020 case PT_WORKGRP:
2021 case PT_COMP:
2022 case PT_NETWORK:
2023 case PT_NETPROVIDER:
2024 case PT_SHARE:
2025 return pdata->u.network.szNames;
2026
2027 case PT_CPLAPPLET:
2028 return pdata->u.cpanel.szName;
2029 }
2030 return NULL;
2031 }
2032
2033 /**************************************************************************
2034 * _ILGetSTextPointer()
2035 * gets a pointer to the short filename string stored in the pidl
2036 */
2037 static LPSTR _ILGetSTextPointer(LPCITEMIDLIST pidl)
2038 {
2039 /* TRACE(pidl,"(pidl%p)\n", pidl); */
2040
2041 LPPIDLDATA pdata = _ILGetDataPointer(pidl);
2042
2043 if (!pdata)
2044 return NULL;
2045
2046 switch (pdata->type)
2047 {
2048 case PT_FOLDER:
2049 case PT_VALUE:
2050 case PT_IESPECIAL1:
2051 case PT_IESPECIAL2:
2052 return pdata->u.file.szNames + strlen (pdata->u.file.szNames) + 1;
2053
2054 case PT_WORKGRP:
2055 return pdata->u.network.szNames + strlen (pdata->u.network.szNames) + 1;
2056 }
2057 return NULL;
2058 }
2059
2060 /**************************************************************************
2061 * _ILGetGUIDPointer()
2062 *
2063 * returns reference to guid stored in some pidls
2064 */
2065 IID* _ILGetGUIDPointer(LPCITEMIDLIST pidl)
2066 {
2067 LPPIDLDATA pdata = _ILGetDataPointer(pidl);
2068
2069 TRACE("%p\n", pidl);
2070
2071 if (!pdata)
2072 return NULL;
2073
2074 TRACE("pdata->type 0x%04x\n", pdata->type);
2075 switch (pdata->type)
2076 {
2077 case PT_SHELLEXT:
2078 case PT_GUID:
2079 case PT_YAGUID:
2080 return &(pdata->u.guid.guid);
2081
2082 default:
2083 TRACE("Unknown pidl type 0x%04x\n", pdata->type);
2084 break;
2085 }
2086 return NULL;
2087 }
2088
2089 /******************************************************************************
2090 * _ILGetFileStructW [Internal]
2091 *
2092 * Get pointer the a SHITEMID's FileStructW field if present
2093 *
2094 * PARAMS
2095 * pidl [I] The SHITEMID
2096 *
2097 * RETURNS
2098 * Success: Pointer to pidl's FileStructW field.
2099 * Failure: NULL
2100 */
2101 FileStructW* _ILGetFileStructW(LPCITEMIDLIST pidl) {
2102 FileStructW *pFileStructW;
2103 WORD cbOffset;
2104
2105 if (!(_ILIsValue(pidl) || _ILIsFolder(pidl)))
2106 return NULL;
2107
2108 cbOffset = *(const WORD *)((const BYTE *)pidl + pidl->mkid.cb - sizeof(WORD));
2109 pFileStructW = (FileStructW*)((LPBYTE)pidl + cbOffset);
2110
2111 /* Currently I don't see a fool prove way to figure out if a pidl is for sure of WinXP
2112 * style with a FileStructW member. If we switch all our shellfolder-implementations to
2113 * the new format, this won't be a problem. For now, we do as many sanity checks as possible. */
2114 if (cbOffset & 0x1 || /* FileStructW member is word aligned in the pidl */
2115 /* FileStructW is positioned after FileStruct */
2116 cbOffset < sizeof(pidl->mkid.cb) + sizeof(PIDLTYPE) + sizeof(FileStruct) ||
2117 /* There has to be enough space at cbOffset in the pidl to hold FileStructW and cbOffset */
2118 cbOffset > pidl->mkid.cb - sizeof(cbOffset) - sizeof(FileStructW) ||
2119 pidl->mkid.cb != cbOffset + pFileStructW->cbLen)
2120 {
2121 ERR("Invalid pidl format (cbOffset = %d)!\n", cbOffset);
2122 return NULL;
2123 }
2124
2125 return pFileStructW;
2126 }
2127
2128 /*************************************************************************
2129 * _ILGetFileDateTime
2130 *
2131 * Given the ItemIdList, get the FileTime
2132 *
2133 * PARAMS
2134 * pidl [I] The ItemIDList
2135 * pFt [I] the resulted FILETIME of the file
2136 *
2137 * RETURNS
2138 * True if Successful
2139 *
2140 * NOTES
2141 *
2142 */
2143 BOOL _ILGetFileDateTime(LPCITEMIDLIST pidl, FILETIME *pFt)
2144 {
2145 LPPIDLDATA pdata = _ILGetDataPointer(pidl);
2146
2147 if (!pdata)
2148 return FALSE;
2149
2150 switch (pdata->type)
2151 {
2152 case PT_FOLDER:
2153 case PT_VALUE:
2154 DosDateTimeToFileTime(pdata->u.file.uFileDate, pdata->u.file.uFileTime, pFt);
2155 break;
2156 default:
2157 return FALSE;
2158 }
2159 return TRUE;
2160 }
2161
2162 BOOL _ILGetFileDate (LPCITEMIDLIST pidl, LPSTR pOut, UINT uOutSize)
2163 {
2164 FILETIME ft, lft;
2165 SYSTEMTIME time;
2166 BOOL ret;
2167
2168 if (_ILGetFileDateTime( pidl, &ft ))
2169 {
2170 FileTimeToLocalFileTime(&ft, &lft);
2171 FileTimeToSystemTime (&lft, &time);
2172
2173 ret = GetDateFormatA(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &time, NULL, pOut, uOutSize);
2174 if (ret)
2175 {
2176 /* Append space + time without seconds */
2177 pOut[ret-1] = ' ';
2178 GetTimeFormatA(LOCALE_USER_DEFAULT, TIME_NOSECONDS, &time, NULL, &pOut[ret], uOutSize - ret);
2179 }
2180 }
2181 else
2182 {
2183 pOut[0] = '\0';
2184 ret = FALSE;
2185 }
2186 return ret;
2187 }
2188
2189 /*************************************************************************
2190 * _ILGetFileSize
2191 *
2192 * Given the ItemIdList, get the FileSize
2193 *
2194 * PARAMS
2195 * pidl [I] The ItemIDList
2196 * pOut [I] The buffer to save the result
2197 * uOutsize [I] The size of the buffer
2198 *
2199 * RETURNS
2200 * The FileSize
2201 *
2202 * NOTES
2203 * pOut can be null when no string is needed
2204 *
2205 */
2206 DWORD _ILGetFileSize (LPCITEMIDLIST pidl, LPSTR pOut, UINT uOutSize)
2207 {
2208 LPPIDLDATA pdata = _ILGetDataPointer(pidl);
2209 DWORD dwSize;
2210
2211 if (!pdata)
2212 return 0;
2213
2214 switch (pdata->type)
2215 {
2216 case PT_VALUE:
2217 dwSize = pdata->u.file.dwFileSize;
2218 if (pOut)
2219 StrFormatKBSizeA(dwSize, pOut, uOutSize);
2220 return dwSize;
2221 }
2222 if (pOut)
2223 *pOut = 0x00;
2224 return 0;
2225 }
2226
2227 BOOL _ILGetExtension (LPCITEMIDLIST pidl, LPSTR pOut, UINT uOutSize)
2228 {
2229 char szTemp[MAX_PATH];
2230 const char * pPoint;
2231 LPCITEMIDLIST pidlTemp = pidl;
2232
2233 TRACE("pidl=%p\n", pidl);
2234
2235 if (!pidl)
2236 return FALSE;
2237
2238 pidlTemp = ILFindLastID(pidl);
2239
2240 if (!_ILIsValue(pidlTemp))
2241 return FALSE;
2242 if (!_ILSimpleGetText(pidlTemp, szTemp, MAX_PATH))
2243 return FALSE;
2244
2245 pPoint = PathFindExtensionA(szTemp);
2246
2247 if (!*pPoint)
2248 return FALSE;
2249
2250 pPoint++;
2251 lstrcpynA(pOut, pPoint, uOutSize);
2252 TRACE("%s\n", pOut);
2253
2254 return TRUE;
2255 }
2256
2257 /*************************************************************************
2258 * _ILGetFileType
2259 *
2260 * Given the ItemIdList, get the file type description
2261 *
2262 * PARAMS
2263 * pidl [I] The ItemIDList (simple)
2264 * pOut [I] The buffer to save the result
2265 * uOutsize [I] The size of the buffer
2266 *
2267 * RETURNS
2268 * nothing
2269 *
2270 * NOTES
2271 * This function copies as much as possible into the buffer.
2272 */
2273 void _ILGetFileType(LPCITEMIDLIST pidl, LPSTR pOut, UINT uOutSize)
2274 {
2275 char sType[64];
2276
2277 if(_ILIsValue(pidl))
2278 {
2279 char sTemp[64];
2280
2281 if(uOutSize > 0)
2282 pOut[0] = 0;
2283 if (_ILGetExtension (pidl, sType, 64))
2284 {
2285 if (HCR_MapTypeToValueA(sType, sTemp, 64, TRUE))
2286 {
2287 /* retrieve description */
2288 if(HCR_MapTypeToValueA(sTemp, pOut, uOutSize, FALSE ))
2289 return;
2290 }
2291 /* display Ext-file as description */
2292 strcpy(pOut, sType);
2293 _strupr(pOut);
2294 /* load localized file string */
2295 sTemp[0] = '\0';
2296 if(LoadStringA(shell32_hInstance, IDS_SHV_COLUMN1, sTemp, 64))
2297 {
2298 sTemp[63] = '\0';
2299 strcat(pOut, "-");
2300 strcat(pOut, sTemp);
2301 }
2302 }
2303 }
2304 else
2305 {
2306 pOut[0] = '\0';
2307 LoadStringA(shell32_hInstance, IDS_DIRECTORY, pOut, uOutSize);
2308 /* make sure its null terminated */
2309 pOut[uOutSize-1] = '\0';
2310 }
2311 }
2312
2313 /*************************************************************************
2314 * _ILGetFileAttributes
2315 *
2316 * Given the ItemIdList, get the Attrib string format
2317 *
2318 * PARAMS
2319 * pidl [I] The ItemIDList
2320 * pOut [I] The buffer to save the result
2321 * uOutsize [I] The size of the Buffer
2322 *
2323 * RETURNS
2324 * Attributes
2325 *
2326 * FIXME
2327 * return value 0 in case of error is a valid return value
2328 *
2329 */
2330 DWORD _ILGetFileAttributes(LPCITEMIDLIST pidl, LPSTR pOut, UINT uOutSize)
2331 {
2332 LPPIDLDATA pData = _ILGetDataPointer(pidl);
2333 WORD wAttrib = 0;
2334 int i;
2335
2336 if (!pData)
2337 return 0;
2338
2339 switch(pData->type)
2340 {
2341 case PT_FOLDER:
2342 case PT_VALUE:
2343 wAttrib = pData->u.file.uFileAttribs;
2344 break;
2345 }
2346
2347 if(uOutSize >= 6)
2348 {
2349 i = 0;
2350 if(wAttrib & FILE_ATTRIBUTE_READONLY)
2351 pOut[i++] = 'R';
2352 if(wAttrib & FILE_ATTRIBUTE_HIDDEN)
2353 pOut[i++] = 'H';
2354 if(wAttrib & FILE_ATTRIBUTE_SYSTEM)
2355 pOut[i++] = 'S';
2356 if(wAttrib & FILE_ATTRIBUTE_ARCHIVE)
2357 pOut[i++] = 'A';
2358 if(wAttrib & FILE_ATTRIBUTE_COMPRESSED)
2359 pOut[i++] = 'C';
2360 pOut[i] = 0x00;
2361 }
2362 return wAttrib;
2363 }
2364
2365 /*************************************************************************
2366 * ILFreeaPidl
2367 *
2368 * free a aPidl struct
2369 */
2370 void _ILFreeaPidl(LPITEMIDLIST * apidl, UINT cidl)
2371 {
2372 UINT i;
2373
2374 if (apidl)
2375 {
2376 for (i = 0; i < cidl; i++)
2377 SHFree(apidl[i]);
2378 SHFree(apidl);
2379 }
2380 }
2381
2382 /*************************************************************************
2383 * ILCopyaPidl
2384 *
2385 * copies an aPidl struct
2386 */
2387 LPITEMIDLIST* _ILCopyaPidl(const LPCITEMIDLIST * apidlsrc, UINT cidl)
2388 {
2389 UINT i;
2390 LPITEMIDLIST *apidldest;
2391
2392 apidldest = (LPITEMIDLIST *)SHAlloc(cidl * sizeof(LPITEMIDLIST));
2393 if (!apidlsrc)
2394 return NULL;
2395
2396 for (i = 0; i < cidl; i++)
2397 apidldest[i] = ILClone(apidlsrc[i]);
2398
2399 return apidldest;
2400 }
2401
2402 /*************************************************************************
2403 * _ILCopyCidaToaPidl
2404 *
2405 * creates aPidl from CIDA
2406 */
2407 LPITEMIDLIST* _ILCopyCidaToaPidl(LPITEMIDLIST* pidl, const CIDA * cida)
2408 {
2409 UINT i;
2410 LPITEMIDLIST *dst;
2411
2412 dst = (LPITEMIDLIST *)SHAlloc(cida->cidl * sizeof(LPITEMIDLIST));
2413 if (!dst)
2414 return NULL;
2415
2416 if (pidl)
2417 *pidl = ILClone((LPCITEMIDLIST)(&((const BYTE*)cida)[cida->aoffset[0]]));
2418
2419 for (i = 0; i < cida->cidl; i++)
2420 dst[i] = ILClone((LPCITEMIDLIST)(&((const BYTE*)cida)[cida->aoffset[i + 1]]));
2421
2422 return dst;
2423 }