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