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