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