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