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