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