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