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