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