shell32: Add a note about a possible crash on NULL input.
[wine] / dlls / shell32 / shfldr_desktop.c
1
2 /*
3  *    Virtual Desktop Folder
4  *
5  *    Copyright 1997            Marcus Meissner
6  *    Copyright 1998, 1999, 2002    Juergen Schmied
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 #include "config.h"
24 #include "wine/port.h"
25
26 #include <stdlib.h>
27 #include <string.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30
31 #define COBJMACROS
32 #define NONAMELESSUNION
33 #define NONAMELESSSTRUCT
34
35 #include "winerror.h"
36 #include "windef.h"
37 #include "winbase.h"
38 #include "winreg.h"
39 #include "wingdi.h"
40 #include "winuser.h"
41
42 #include "ole2.h"
43 #include "shlguid.h"
44
45 #include "enumidlist.h"
46 #include "pidl.h"
47 #include "undocshell.h"
48 #include "shell32_main.h"
49 #include "shresdef.h"
50 #include "shlwapi.h"
51 #include "shellfolder.h"
52 #include "wine/debug.h"
53 #include "debughlp.h"
54 #include "shfldr.h"
55
56 WINE_DEFAULT_DEBUG_CHANNEL (shell);
57
58 /* Undocumented functions from shdocvw */
59 extern HRESULT WINAPI IEParseDisplayNameWithBCW(DWORD codepage, LPCWSTR lpszDisplayName, LPBC pbc, LPITEMIDLIST *ppidl);
60
61
62 /***********************************************************************
63 *     Desktopfolder implementation
64 */
65
66 typedef struct {
67     const IShellFolder2Vtbl *lpVtbl;
68     const IPersistVtbl *lpVtblIPersist;
69     LONG ref;
70
71     /* both paths are parsible from the desktop */
72     LPWSTR sPathTarget;     /* complete path to target used for enumeration and ChangeNotify */
73     LPITEMIDLIST pidlRoot;  /* absolute pidl */
74
75     UINT cfShellIDList;        /* clipboardformat for IDropTarget */
76     BOOL fAcceptFmt;        /* flag for pending Drop */
77 } IGenericSFImpl;
78
79 #define _IUnknown_(This)    (IShellFolder*)&(This->lpVtbl)
80 #define _IShellFolder_(This)    (IShellFolder*)&(This->lpVtbl)
81
82 static inline IGenericSFImpl *impl_from_IPersist( IPersist *iface )
83 {
84     return (IGenericSFImpl *)((char*)iface - FIELD_OFFSET(IGenericSFImpl, lpVtblIPersist));
85 }
86
87 static const shvheader DesktopSFHeader[] = {
88     {IDS_SHV_COLUMN1, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 15},
89     {IDS_SHV_COLUMN2, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 10},
90     {IDS_SHV_COLUMN3, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 10},
91     {IDS_SHV_COLUMN4, SHCOLSTATE_TYPE_DATE | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 12},
92     {IDS_SHV_COLUMN5, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 5}
93 };
94
95 #define DESKTOPSHELLVIEWCOLUMNS 5
96
97 /**************************************************************************
98  *    ISF_Desktop_fnQueryInterface
99  *
100  * NOTES supports not IPersistFolder
101  */
102 static HRESULT WINAPI ISF_Desktop_fnQueryInterface(
103                 IShellFolder2 * iface, REFIID riid, LPVOID * ppvObj)
104 {
105     IGenericSFImpl *This = (IGenericSFImpl *)iface;
106
107     TRACE ("(%p)->(%s,%p)\n", This, shdebugstr_guid (riid), ppvObj);
108
109     *ppvObj = NULL;
110
111     if (IsEqualIID (riid, &IID_IUnknown) ||
112         IsEqualIID (riid, &IID_IShellFolder) ||
113         IsEqualIID (riid, &IID_IShellFolder2))
114     {
115         *ppvObj = This;
116     }
117     else if (IsEqualIID (riid, &IID_IPersist))
118     {
119         *ppvObj = &This->lpVtblIPersist;
120     }
121
122     if (*ppvObj)
123     {
124         IUnknown_AddRef ((IUnknown *) (*ppvObj));
125         TRACE ("-- Interface: (%p)->(%p)\n", ppvObj, *ppvObj);
126         return S_OK;
127     }
128     TRACE ("-- Interface: E_NOINTERFACE\n");
129     return E_NOINTERFACE;
130 }
131
132 static ULONG WINAPI ISF_Desktop_fnAddRef (IShellFolder2 * iface)
133 {
134     return 2; /* non-heap based object */
135 }
136
137 static ULONG WINAPI ISF_Desktop_fnRelease (IShellFolder2 * iface)
138 {
139     return 1; /* non-heap based object */
140 }
141
142 /**************************************************************************
143  *    ISF_Desktop_fnParseDisplayName
144  *
145  * NOTES
146  *    "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}" and "" binds
147  *    to MyComputer
148  */
149 static HRESULT WINAPI ISF_Desktop_fnParseDisplayName (IShellFolder2 * iface,
150                 HWND hwndOwner, LPBC pbc, LPOLESTR lpszDisplayName,
151                 DWORD * pchEaten, LPITEMIDLIST * ppidl, DWORD * pdwAttributes)
152 {
153     IGenericSFImpl *This = (IGenericSFImpl *)iface;
154     WCHAR szElement[MAX_PATH];
155     LPCWSTR szNext = NULL;
156     LPITEMIDLIST pidlTemp = NULL;
157     HRESULT hr = S_OK;
158     CLSID clsid;
159
160     TRACE ("(%p)->(HWND=%p,%p,%p=%s,%p,pidl=%p,%p)\n",
161            This, hwndOwner, pbc, lpszDisplayName, debugstr_w(lpszDisplayName),
162            pchEaten, ppidl, pdwAttributes);
163
164     if (!ppidl) return E_INVALIDARG;
165     *ppidl = 0;
166
167     if (!lpszDisplayName) return E_INVALIDARG;
168
169     if (pchEaten)
170         *pchEaten = 0;        /* strange but like the original */
171
172     if (lpszDisplayName[0] == ':' && lpszDisplayName[1] == ':')
173     {
174         szNext = GetNextElementW (lpszDisplayName, szElement, MAX_PATH);
175         TRACE ("-- element: %s\n", debugstr_w (szElement));
176         SHCLSIDFromStringW (szElement + 2, &clsid);
177         pidlTemp = _ILCreateGuid (PT_GUID, &clsid);
178     }
179     else if (PathGetDriveNumberW (lpszDisplayName) >= 0)
180     {
181         /* it's a filesystem path with a drive. Let MyComputer/UnixDosFolder parse it */
182         if (UNIXFS_is_rooted_at_desktop()) 
183             pidlTemp = _ILCreateGuid(PT_GUID, &CLSID_UnixDosFolder);
184         else
185             pidlTemp = _ILCreateMyComputer ();
186         szNext = lpszDisplayName;
187     }
188     else if (PathIsUNCW(lpszDisplayName))
189     {
190         pidlTemp = _ILCreateNetwork();
191         szNext = lpszDisplayName;
192     }
193     else if( (pidlTemp = SHELL32_CreatePidlFromBindCtx(pbc, lpszDisplayName)) )
194     {
195         *ppidl = pidlTemp;
196         return S_OK;
197     }
198     else if (strchrW(lpszDisplayName,':'))
199     {
200         PARSEDURLW urldata;
201
202         urldata.cbSize = sizeof(urldata);
203         ParseURLW(lpszDisplayName,&urldata);
204
205         if (urldata.nScheme == URL_SCHEME_SHELL) /* handle shell: urls */
206         {
207             TRACE ("-- shell url: %s\n", debugstr_w(urldata.pszSuffix));
208             SHCLSIDFromStringW (urldata.pszSuffix+2, &clsid);
209             pidlTemp = _ILCreateGuid (PT_GUID, &clsid);
210         }
211         else
212             return IEParseDisplayNameWithBCW(CP_ACP,lpszDisplayName,pbc,ppidl);
213     }
214     else
215     {
216         /* it's a filesystem path on the desktop. Let a FSFolder parse it */
217
218         if (*lpszDisplayName)
219         {
220             WCHAR szPath[MAX_PATH];
221             LPWSTR pathPtr;
222
223             /* build a complete path to create a simple pidl */
224             lstrcpynW(szPath, This->sPathTarget, MAX_PATH);
225             pathPtr = PathAddBackslashW(szPath);
226             if (pathPtr)
227             {
228                 lstrcpynW(pathPtr, lpszDisplayName, MAX_PATH - (pathPtr - szPath));
229                 hr = _ILCreateFromPathW(szPath, &pidlTemp);
230             }
231             else
232             {
233                 /* should never reach here, but for completeness */
234                 hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
235             }
236         }
237         else
238             pidlTemp = _ILCreateMyComputer();
239
240         szNext = NULL;
241     }
242
243     if (SUCCEEDED(hr) && pidlTemp)
244     {
245         if (szNext && *szNext)
246         {
247             hr = SHELL32_ParseNextElement(iface, hwndOwner, pbc,
248                     &pidlTemp, (LPOLESTR) szNext, pchEaten, pdwAttributes);
249         }
250         else
251         {
252             if (pdwAttributes && *pdwAttributes)
253                 hr = SHELL32_GetItemAttributes(_IShellFolder_ (This),
254                                                pidlTemp, pdwAttributes);
255         }
256     }
257
258     *ppidl = pidlTemp;
259
260     TRACE ("(%p)->(-- ret=0x%08x)\n", This, hr);
261
262     return hr;
263 }
264
265 /**************************************************************************
266  *  CreateDesktopEnumList()
267  */
268 static const WCHAR Desktop_NameSpaceW[] = { 'S','O','F','T','W','A','R','E',
269  '\\','M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s','\\',
270  'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\','E','x','p','l',
271  'o','r','e','r','\\','D','e','s','k','t','o','p','\\','N','a','m','e','s','p',
272  'a','c','e','\0' };
273
274 static BOOL CreateDesktopEnumList(IEnumIDList *list, DWORD dwFlags)
275 {
276     BOOL ret = TRUE;
277     WCHAR szPath[MAX_PATH];
278
279     TRACE("(%p)->(flags=0x%08x)\n", list, dwFlags);
280
281     /* enumerate the root folders */
282     if (dwFlags & SHCONTF_FOLDERS)
283     {
284         HKEY hkey;
285         UINT i;
286
287         /* create the pidl for This item */
288         ret = AddToEnumList(list, _ILCreateMyComputer());
289
290         for (i=0; i<2; i++) {
291             if (ret && !RegOpenKeyExW(i == 0 ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER,
292                                       Desktop_NameSpaceW, 0, KEY_READ, &hkey))
293             {
294                 WCHAR iid[50];
295                 int i=0;
296
297                 while (ret)
298                 {
299                     DWORD size;
300                     LONG r;
301
302                     size = sizeof (iid) / sizeof (iid[0]);
303                     r = RegEnumKeyExW(hkey, i, iid, &size, 0, NULL, NULL, NULL);
304                     if (ERROR_SUCCESS == r)
305                     {
306                         ret = AddToEnumList(list, _ILCreateGuidFromStrW(iid));
307                         i++;
308                     }
309                     else if (ERROR_NO_MORE_ITEMS == r)
310                         break;
311                     else
312                         ret = FALSE;
313                 }
314                 RegCloseKey(hkey);
315             }
316         }
317     }
318
319     /* enumerate the elements in %windir%\desktop */
320     SHGetSpecialFolderPathW(0, szPath, CSIDL_DESKTOPDIRECTORY, FALSE);
321     ret = ret && CreateFolderEnumList(list, szPath, dwFlags);
322
323     return ret;
324 }
325
326 /**************************************************************************
327  *        ISF_Desktop_fnEnumObjects
328  */
329 static HRESULT WINAPI ISF_Desktop_fnEnumObjects (IShellFolder2 * iface,
330                 HWND hwndOwner, DWORD dwFlags, LPENUMIDLIST * ppEnumIDList)
331 {
332     IGenericSFImpl *This = (IGenericSFImpl *)iface;
333
334     TRACE ("(%p)->(HWND=%p flags=0x%08x pplist=%p)\n",
335            This, hwndOwner, dwFlags, ppEnumIDList);
336
337     *ppEnumIDList = IEnumIDList_Constructor();
338     if (*ppEnumIDList)
339         CreateDesktopEnumList(*ppEnumIDList, dwFlags);
340
341     TRACE ("-- (%p)->(new ID List: %p)\n", This, *ppEnumIDList);
342
343     return *ppEnumIDList ? S_OK : E_OUTOFMEMORY;
344 }
345
346 /**************************************************************************
347  *        ISF_Desktop_fnBindToObject
348  */
349 static HRESULT WINAPI ISF_Desktop_fnBindToObject (IShellFolder2 * iface,
350                 LPCITEMIDLIST pidl, LPBC pbcReserved, REFIID riid, LPVOID * ppvOut)
351 {
352     IGenericSFImpl *This = (IGenericSFImpl *)iface;
353
354     TRACE ("(%p)->(pidl=%p,%p,%s,%p)\n",
355            This, pidl, pbcReserved, shdebugstr_guid (riid), ppvOut);
356
357     return SHELL32_BindToChild( This->pidlRoot, This->sPathTarget, pidl, riid, ppvOut );
358 }
359
360 /**************************************************************************
361  *    ISF_Desktop_fnBindToStorage
362  */
363 static HRESULT WINAPI ISF_Desktop_fnBindToStorage (IShellFolder2 * iface,
364                 LPCITEMIDLIST pidl, LPBC pbcReserved, REFIID riid, LPVOID * ppvOut)
365 {
366     IGenericSFImpl *This = (IGenericSFImpl *)iface;
367
368     FIXME ("(%p)->(pidl=%p,%p,%s,%p) stub\n",
369            This, pidl, pbcReserved, shdebugstr_guid (riid), ppvOut);
370
371     *ppvOut = NULL;
372     return E_NOTIMPL;
373 }
374
375 /**************************************************************************
376  *     ISF_Desktop_fnCompareIDs
377  */
378 static HRESULT WINAPI ISF_Desktop_fnCompareIDs (IShellFolder2 * iface,
379                         LPARAM lParam, LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2)
380 {
381     IGenericSFImpl *This = (IGenericSFImpl *)iface;
382     int nReturn;
383
384     TRACE ("(%p)->(0x%08lx,pidl1=%p,pidl2=%p)\n", This, lParam, pidl1, pidl2);
385     nReturn = SHELL32_CompareIDs (_IShellFolder_ (This), lParam, pidl1, pidl2);
386     TRACE ("-- %i\n", nReturn);
387     return nReturn;
388 }
389
390 /**************************************************************************
391  *    ISF_Desktop_fnCreateViewObject
392  */
393 static HRESULT WINAPI ISF_Desktop_fnCreateViewObject (IShellFolder2 * iface,
394                               HWND hwndOwner, REFIID riid, LPVOID * ppvOut)
395 {
396     IGenericSFImpl *This = (IGenericSFImpl *)iface;
397     LPSHELLVIEW pShellView;
398     HRESULT hr = E_INVALIDARG;
399
400     TRACE ("(%p)->(hwnd=%p,%s,%p)\n",
401            This, hwndOwner, shdebugstr_guid (riid), ppvOut);
402
403     if (!ppvOut)
404         return hr;
405
406     *ppvOut = NULL;
407
408     if (IsEqualIID (riid, &IID_IDropTarget))
409     {
410         WARN ("IDropTarget not implemented\n");
411         hr = E_NOTIMPL;
412     }
413     else if (IsEqualIID (riid, &IID_IContextMenu))
414     {
415         WARN ("IContextMenu not implemented\n");
416         hr = E_NOTIMPL;
417     }
418     else if (IsEqualIID (riid, &IID_IShellView))
419     {
420         pShellView = IShellView_Constructor ((IShellFolder *) iface);
421         if (pShellView)
422         {
423             hr = IShellView_QueryInterface (pShellView, riid, ppvOut);
424             IShellView_Release (pShellView);
425         }
426     }
427     TRACE ("-- (%p)->(interface=%p)\n", This, ppvOut);
428     return hr;
429 }
430
431 /**************************************************************************
432  *  ISF_Desktop_fnGetAttributesOf
433  */
434 static HRESULT WINAPI ISF_Desktop_fnGetAttributesOf (IShellFolder2 * iface,
435                 UINT cidl, LPCITEMIDLIST * apidl, DWORD * rgfInOut)
436 {
437     IGenericSFImpl *This = (IGenericSFImpl *)iface;
438     HRESULT hr = S_OK;
439     static const DWORD dwDesktopAttributes = 
440         SFGAO_STORAGE | SFGAO_HASPROPSHEET | SFGAO_STORAGEANCESTOR |
441         SFGAO_FILESYSANCESTOR | SFGAO_FOLDER | SFGAO_FILESYSTEM | SFGAO_HASSUBFOLDER;
442     static const DWORD dwMyComputerAttributes = 
443         SFGAO_CANRENAME | SFGAO_CANDELETE | SFGAO_HASPROPSHEET |
444         SFGAO_DROPTARGET | SFGAO_FILESYSANCESTOR | SFGAO_FOLDER | SFGAO_HASSUBFOLDER;
445
446     TRACE ("(%p)->(cidl=%d apidl=%p mask=%p (0x%08x))\n",
447            This, cidl, apidl, rgfInOut, rgfInOut ? *rgfInOut : 0);
448
449     if (!rgfInOut)
450         return E_INVALIDARG;
451     if (cidl && !apidl)
452         return E_INVALIDARG;
453
454     if (*rgfInOut == 0)
455         *rgfInOut = ~0;
456     
457     if(cidl == 0) {
458         *rgfInOut &= dwDesktopAttributes; 
459     } else {
460         while (cidl > 0 && *apidl) {
461             pdump (*apidl);
462             if (_ILIsDesktop(*apidl)) { 
463                 *rgfInOut &= dwDesktopAttributes;
464             } else if (_ILIsMyComputer(*apidl)) {
465                 *rgfInOut &= dwMyComputerAttributes;
466             } else {
467                 SHELL32_GetItemAttributes (_IShellFolder_ (This), *apidl, rgfInOut);
468             }
469             apidl++;
470             cidl--;
471         }
472     }
473     /* make sure SFGAO_VALIDATE is cleared, some apps depend on that */
474     *rgfInOut &= ~SFGAO_VALIDATE;
475
476     TRACE ("-- result=0x%08x\n", *rgfInOut);
477
478     return hr;
479 }
480
481 /**************************************************************************
482  *    ISF_Desktop_fnGetUIObjectOf
483  *
484  * PARAMETERS
485  *  HWND           hwndOwner, //[in ] Parent window for any output
486  *  UINT           cidl,      //[in ] array size
487  *  LPCITEMIDLIST* apidl,     //[in ] simple pidl array
488  *  REFIID         riid,      //[in ] Requested Interface
489  *  UINT*          prgfInOut, //[   ] reserved
490  *  LPVOID*        ppvObject) //[out] Resulting Interface
491  *
492  */
493 static HRESULT WINAPI ISF_Desktop_fnGetUIObjectOf (IShellFolder2 * iface,
494                 HWND hwndOwner, UINT cidl, LPCITEMIDLIST * apidl,
495                 REFIID riid, UINT * prgfInOut, LPVOID * ppvOut)
496 {
497     IGenericSFImpl *This = (IGenericSFImpl *)iface;
498
499     LPITEMIDLIST pidl;
500     IUnknown *pObj = NULL;
501     HRESULT hr = E_INVALIDARG;
502
503     TRACE ("(%p)->(%p,%u,apidl=%p,%s,%p,%p)\n",
504        This, hwndOwner, cidl, apidl, shdebugstr_guid (riid), prgfInOut, ppvOut);
505
506     if (!ppvOut)
507         return hr;
508
509     *ppvOut = NULL;
510
511     if (IsEqualIID (riid, &IID_IContextMenu))
512     {
513         if (cidl > 0)
514             pObj = (LPUNKNOWN) ISvItemCm_Constructor( (IShellFolder *) iface, This->pidlRoot, apidl, cidl);
515         else
516             pObj = (LPUNKNOWN) ISvBgCm_Constructor( (IShellFolder *) iface, TRUE);
517         hr = S_OK;
518     }
519     else if (IsEqualIID (riid, &IID_IDataObject) && (cidl >= 1))
520     {
521         pObj = (LPUNKNOWN) IDataObject_Constructor( hwndOwner,
522                                                   This->pidlRoot, apidl, cidl);
523         hr = S_OK;
524     }
525     else if (IsEqualIID (riid, &IID_IExtractIconA) && (cidl == 1))
526     {
527         pidl = ILCombine (This->pidlRoot, apidl[0]);
528         pObj = (LPUNKNOWN) IExtractIconA_Constructor (pidl);
529         SHFree (pidl);
530         hr = S_OK;
531     }
532     else if (IsEqualIID (riid, &IID_IExtractIconW) && (cidl == 1))
533     {
534         pidl = ILCombine (This->pidlRoot, apidl[0]);
535         pObj = (LPUNKNOWN) IExtractIconW_Constructor (pidl);
536         SHFree (pidl);
537         hr = S_OK;
538     }
539     else if (IsEqualIID (riid, &IID_IDropTarget) && (cidl >= 1))
540     {
541         hr = IShellFolder_QueryInterface (iface,
542                                           &IID_IDropTarget, (LPVOID *) & pObj);
543     }
544     else if ((IsEqualIID(riid,&IID_IShellLinkW) ||
545               IsEqualIID(riid,&IID_IShellLinkA)) && (cidl == 1))
546     {
547         pidl = ILCombine (This->pidlRoot, apidl[0]);
548         hr = IShellLink_ConstructFromFile(NULL, riid, pidl, (LPVOID*)&pObj);
549         SHFree (pidl);
550     }
551     else
552         hr = E_NOINTERFACE;
553
554     if (SUCCEEDED(hr) && !pObj)
555         hr = E_OUTOFMEMORY;
556
557     *ppvOut = pObj;
558     TRACE ("(%p)->hr=0x%08x\n", This, hr);
559     return hr;
560 }
561
562 /**************************************************************************
563  *    ISF_Desktop_fnGetDisplayNameOf
564  *
565  * NOTES
566  *    special case: pidl = null gives desktop-name back
567  */
568 static HRESULT WINAPI ISF_Desktop_fnGetDisplayNameOf (IShellFolder2 * iface,
569                 LPCITEMIDLIST pidl, DWORD dwFlags, LPSTRRET strRet)
570 {
571     IGenericSFImpl *This = (IGenericSFImpl *)iface;
572     HRESULT hr = S_OK;
573     LPWSTR pszPath;
574
575     TRACE ("(%p)->(pidl=%p,0x%08x,%p)\n", This, pidl, dwFlags, strRet);
576     pdump (pidl);
577
578     if (!strRet)
579         return E_INVALIDARG;
580
581     pszPath = CoTaskMemAlloc((MAX_PATH +1) * sizeof(WCHAR));
582     if (!pszPath)
583         return E_OUTOFMEMORY;
584
585     if (_ILIsDesktop (pidl))
586     {
587         if ((GET_SHGDN_RELATION (dwFlags) == SHGDN_NORMAL) &&
588             (GET_SHGDN_FOR (dwFlags) & SHGDN_FORPARSING))
589             strcpyW(pszPath, This->sPathTarget);
590         else
591             HCR_GetClassNameW(&CLSID_ShellDesktop, pszPath, MAX_PATH);
592     }
593     else if (_ILIsPidlSimple (pidl))
594     {
595         GUID const *clsid;
596
597         if ((clsid = _ILGetGUIDPointer (pidl)))
598         {
599             if (GET_SHGDN_FOR (dwFlags) & SHGDN_FORPARSING)
600             {
601                 int bWantsForParsing;
602
603                 /*
604                  * We can only get a filesystem path from a shellfolder if the
605                  *  value WantsFORPARSING in CLSID\\{...}\\shellfolder exists.
606                  *
607                  * Exception: The MyComputer folder doesn't have this key,
608                  *   but any other filesystem backed folder it needs it.
609                  */
610                 if (IsEqualIID (clsid, &CLSID_MyComputer))
611                 {
612                     bWantsForParsing = TRUE;
613                 }
614                 else
615                 {
616                     /* get the "WantsFORPARSING" flag from the registry */
617                     static const WCHAR clsidW[] =
618                      { 'C','L','S','I','D','\\',0 };
619                     static const WCHAR shellfolderW[] =
620                      { '\\','s','h','e','l','l','f','o','l','d','e','r',0 };
621                     static const WCHAR wantsForParsingW[] =
622                      { 'W','a','n','t','s','F','o','r','P','a','r','s','i','n',
623                      'g',0 };
624                     WCHAR szRegPath[100];
625                     LONG r;
626
627                     lstrcpyW (szRegPath, clsidW);
628                     SHELL32_GUIDToStringW (clsid, &szRegPath[6]);
629                     lstrcatW (szRegPath, shellfolderW);
630                     r = SHGetValueW(HKEY_CLASSES_ROOT, szRegPath,
631                                     wantsForParsingW, NULL, NULL, NULL);
632                     if (r == ERROR_SUCCESS)
633                         bWantsForParsing = TRUE;
634                     else
635                         bWantsForParsing = FALSE;
636                 }
637
638                 if ((GET_SHGDN_RELATION (dwFlags) == SHGDN_NORMAL) &&
639                      bWantsForParsing)
640                 {
641                     /*
642                      * we need the filesystem path to the destination folder.
643                      * Only the folder itself can know it
644                      */
645                     hr = SHELL32_GetDisplayNameOfChild (iface, pidl, dwFlags,
646                                                         pszPath,
647                                                         MAX_PATH);
648                 }
649                 else
650                 {
651                     /* parsing name like ::{...} */
652                     pszPath[0] = ':';
653                     pszPath[1] = ':';
654                     SHELL32_GUIDToStringW (clsid, &pszPath[2]);
655                 }
656             }
657             else
658             {
659                 /* user friendly name */
660                 HCR_GetClassNameW (clsid, pszPath, MAX_PATH);
661             }
662         }
663         else
664         {
665             int cLen = 0;
666
667             /* file system folder or file rooted at the desktop */
668             if ((GET_SHGDN_FOR(dwFlags) == SHGDN_FORPARSING) &&
669                 (GET_SHGDN_RELATION(dwFlags) != SHGDN_INFOLDER))
670             {
671                 lstrcpynW(pszPath, This->sPathTarget, MAX_PATH - 1);
672                 PathAddBackslashW(pszPath);
673                 cLen = lstrlenW(pszPath);
674             }
675
676             _ILSimpleGetTextW(pidl, pszPath + cLen, MAX_PATH - cLen);
677
678             if (!_ILIsFolder(pidl))
679                 SHELL_FS_ProcessDisplayFilename(pszPath, dwFlags);
680         }
681     }
682     else
683     {
684         /* a complex pidl, let the subfolder do the work */
685         hr = SHELL32_GetDisplayNameOfChild (iface, pidl, dwFlags,
686                                             pszPath, MAX_PATH);
687     }
688
689     if (SUCCEEDED(hr))
690     {
691         /* Win9x always returns ANSI strings, NT always returns Unicode strings */
692         if (GetVersion() & 0x80000000)
693         {
694             strRet->uType = STRRET_CSTR;
695             if (!WideCharToMultiByte(CP_ACP, 0, pszPath, -1, strRet->u.cStr, MAX_PATH,
696                                      NULL, NULL))
697                 strRet->u.cStr[0] = '\0';
698             CoTaskMemFree(pszPath);
699         }
700         else
701         {
702             strRet->uType = STRRET_WSTR;
703             strRet->u.pOleStr = pszPath;
704         }
705     }
706     else
707         CoTaskMemFree(pszPath);
708
709     TRACE ("-- (%p)->(%s,0x%08x)\n", This,
710      strRet->uType == STRRET_CSTR ? strRet->u.cStr :
711      debugstr_w(strRet->u.pOleStr), hr);
712     return hr;
713 }
714
715 /**************************************************************************
716  *  ISF_Desktop_fnSetNameOf
717  *  Changes the name of a file object or subfolder, possibly changing its item
718  *  identifier in the process.
719  *
720  * PARAMETERS
721  *  HWND          hwndOwner,  //[in ] Owner window for output
722  *  LPCITEMIDLIST pidl,       //[in ] simple pidl of item to change
723  *  LPCOLESTR     lpszName,   //[in ] the items new display name
724  *  DWORD         dwFlags,    //[in ] SHGNO formatting flags
725  *  LPITEMIDLIST* ppidlOut)   //[out] simple pidl returned
726  */
727 static HRESULT WINAPI ISF_Desktop_fnSetNameOf (IShellFolder2 * iface,
728                 HWND hwndOwner, LPCITEMIDLIST pidl,    /* simple pidl */
729                 LPCOLESTR lpName, DWORD dwFlags, LPITEMIDLIST * pPidlOut)
730 {
731     IGenericSFImpl *This = (IGenericSFImpl *)iface;
732
733     FIXME ("(%p)->(%p,pidl=%p,%s,%u,%p)\n", This, hwndOwner, pidl,
734            debugstr_w (lpName), dwFlags, pPidlOut);
735
736     return E_FAIL;
737 }
738
739 static HRESULT WINAPI ISF_Desktop_fnGetDefaultSearchGUID(IShellFolder2 *iface,
740                 GUID * pguid)
741 {
742     IGenericSFImpl *This = (IGenericSFImpl *)iface;
743
744     FIXME ("(%p)\n", This);
745     return E_NOTIMPL;
746 }
747
748 static HRESULT WINAPI ISF_Desktop_fnEnumSearches (IShellFolder2 *iface,
749                 IEnumExtraSearch ** ppenum)
750 {
751     IGenericSFImpl *This = (IGenericSFImpl *)iface;
752     FIXME ("(%p)\n", This);
753     return E_NOTIMPL;
754 }
755
756 static HRESULT WINAPI ISF_Desktop_fnGetDefaultColumn (IShellFolder2 * iface,
757                 DWORD dwRes, ULONG * pSort, ULONG * pDisplay)
758 {
759     IGenericSFImpl *This = (IGenericSFImpl *)iface;
760
761     TRACE ("(%p)\n", This);
762
763     if (pSort)
764         *pSort = 0;
765     if (pDisplay)
766         *pDisplay = 0;
767
768     return S_OK;
769 }
770 static HRESULT WINAPI ISF_Desktop_fnGetDefaultColumnState (
771                 IShellFolder2 * iface, UINT iColumn, DWORD * pcsFlags)
772 {
773     IGenericSFImpl *This = (IGenericSFImpl *)iface;
774
775     TRACE ("(%p)\n", This);
776
777     if (!pcsFlags || iColumn >= DESKTOPSHELLVIEWCOLUMNS)
778     return E_INVALIDARG;
779
780     *pcsFlags = DesktopSFHeader[iColumn].pcsFlags;
781
782     return S_OK;
783 }
784
785 static HRESULT WINAPI ISF_Desktop_fnGetDetailsEx (IShellFolder2 * iface,
786                 LPCITEMIDLIST pidl, const SHCOLUMNID * pscid, VARIANT * pv)
787 {
788     IGenericSFImpl *This = (IGenericSFImpl *)iface;
789     FIXME ("(%p)\n", This);
790
791     return E_NOTIMPL;
792 }
793
794 static HRESULT WINAPI ISF_Desktop_fnGetDetailsOf (IShellFolder2 * iface,
795                 LPCITEMIDLIST pidl, UINT iColumn, SHELLDETAILS * psd)
796 {
797     IGenericSFImpl *This = (IGenericSFImpl *)iface;
798
799     HRESULT hr = S_OK;
800
801     TRACE ("(%p)->(%p %i %p)\n", This, pidl, iColumn, psd);
802
803     if (!psd || iColumn >= DESKTOPSHELLVIEWCOLUMNS)
804         return E_INVALIDARG;
805
806     if (!pidl)
807     {
808         psd->fmt = DesktopSFHeader[iColumn].fmt;
809         psd->cxChar = DesktopSFHeader[iColumn].cxChar;
810         psd->str.uType = STRRET_CSTR;
811         LoadStringA (shell32_hInstance, DesktopSFHeader[iColumn].colnameid,
812                      psd->str.u.cStr, MAX_PATH);
813         return S_OK;
814     }
815
816     /* the data from the pidl */
817     psd->str.uType = STRRET_CSTR;
818     switch (iColumn)
819     {
820     case 0:        /* name */
821         hr = IShellFolder_GetDisplayNameOf(iface, pidl,
822                    SHGDN_NORMAL | SHGDN_INFOLDER, &psd->str);
823         break;
824     case 1:        /* size */
825         _ILGetFileSize (pidl, psd->str.u.cStr, MAX_PATH);
826         break;
827     case 2:        /* type */
828         _ILGetFileType (pidl, psd->str.u.cStr, MAX_PATH);
829         break;
830     case 3:        /* date */
831         _ILGetFileDate (pidl, psd->str.u.cStr, MAX_PATH);
832         break;
833     case 4:        /* attributes */
834         _ILGetFileAttributes (pidl, psd->str.u.cStr, MAX_PATH);
835         break;
836     }
837
838     return hr;
839 }
840
841 static HRESULT WINAPI ISF_Desktop_fnMapColumnToSCID (
842                 IShellFolder2 * iface, UINT column, SHCOLUMNID * pscid)
843 {
844     IGenericSFImpl *This = (IGenericSFImpl *)iface;
845     FIXME ("(%p)\n", This);
846     return E_NOTIMPL;
847 }
848
849 static const IShellFolder2Vtbl vt_MCFldr_ShellFolder2 =
850 {
851     ISF_Desktop_fnQueryInterface,
852     ISF_Desktop_fnAddRef,
853     ISF_Desktop_fnRelease,
854     ISF_Desktop_fnParseDisplayName,
855     ISF_Desktop_fnEnumObjects,
856     ISF_Desktop_fnBindToObject,
857     ISF_Desktop_fnBindToStorage,
858     ISF_Desktop_fnCompareIDs,
859     ISF_Desktop_fnCreateViewObject,
860     ISF_Desktop_fnGetAttributesOf,
861     ISF_Desktop_fnGetUIObjectOf,
862     ISF_Desktop_fnGetDisplayNameOf,
863     ISF_Desktop_fnSetNameOf,
864     /* ShellFolder2 */
865     ISF_Desktop_fnGetDefaultSearchGUID,
866     ISF_Desktop_fnEnumSearches,
867     ISF_Desktop_fnGetDefaultColumn,
868     ISF_Desktop_fnGetDefaultColumnState,
869     ISF_Desktop_fnGetDetailsEx,
870     ISF_Desktop_fnGetDetailsOf,
871     ISF_Desktop_fnMapColumnToSCID
872 };
873
874 /**************************************************************************
875  *    IPersist
876  */
877 static HRESULT WINAPI ISF_Desktop_IPersist_fnQueryInterface(
878     IPersist *iface, REFIID riid, LPVOID *ppvObj)
879 {
880     IGenericSFImpl *This = impl_from_IPersist( iface );
881     return IShellFolder2_QueryInterface((IShellFolder2*)This, riid, ppvObj);
882 }
883
884 static ULONG WINAPI ISF_Desktop_IPersist_fnAddRef(IPersist *iface)
885 {
886     IGenericSFImpl *This = impl_from_IPersist( iface );
887     return IShellFolder2_AddRef((IShellFolder2*)This);
888 }
889
890 static ULONG WINAPI ISF_Desktop_IPersist_fnRelease(IPersist *iface)
891 {
892     IGenericSFImpl *This = impl_from_IPersist( iface );
893     return IShellFolder2_Release((IShellFolder2*)This);
894 }
895
896 static HRESULT WINAPI ISF_Desktop_IPersist_fnGetClassID(IPersist *iface, CLSID *clsid)
897 {
898     *clsid = CLSID_ShellDesktop;
899     return S_OK;
900 }
901
902 static const IPersistVtbl vt_IPersist =
903 {
904     ISF_Desktop_IPersist_fnQueryInterface,
905     ISF_Desktop_IPersist_fnAddRef,
906     ISF_Desktop_IPersist_fnRelease,
907     ISF_Desktop_IPersist_fnGetClassID
908 };
909
910 /**************************************************************************
911  *    ISF_Desktop_Constructor
912  */
913 HRESULT WINAPI ISF_Desktop_Constructor (
914                 IUnknown * pUnkOuter, REFIID riid, LPVOID * ppv)
915 {
916     static IGenericSFImpl *cached_sf;
917     WCHAR szMyPath[MAX_PATH];
918
919     TRACE ("unkOut=%p %s\n", pUnkOuter, shdebugstr_guid (riid));
920
921     if (!ppv)
922         return E_POINTER;
923     if (pUnkOuter)
924         return CLASS_E_NOAGGREGATION;
925
926     if (!cached_sf)
927     {
928         IGenericSFImpl *sf;
929
930         if (!SHGetSpecialFolderPathW( 0, szMyPath, CSIDL_DESKTOPDIRECTORY, TRUE ))
931             return E_UNEXPECTED;
932
933         sf = LocalAlloc( LMEM_ZEROINIT, sizeof (IGenericSFImpl) );
934         if (!sf)
935             return E_OUTOFMEMORY;
936
937         sf->ref = 1;
938         sf->lpVtbl = &vt_MCFldr_ShellFolder2;
939         sf->lpVtblIPersist = &vt_IPersist;
940         sf->pidlRoot = _ILCreateDesktop();    /* my qualified pidl */
941         sf->sPathTarget = SHAlloc( (lstrlenW(szMyPath) + 1)*sizeof(WCHAR) );
942         lstrcpyW( sf->sPathTarget, szMyPath );
943
944         if (InterlockedCompareExchangePointer((void *)&cached_sf, sf, NULL) != NULL)
945         {
946             /* some other thread already been here */
947             SHFree( sf->pidlRoot );
948             SHFree( sf->sPathTarget );
949             LocalFree( sf );
950         }
951     }
952
953     return IUnknown_QueryInterface( _IUnknown_(cached_sf), riid, ppv );
954 }