msvcrt: Mark some fall-throughs in a switch statement.
[wine] / dlls / shell32 / cpanelfolder.c
1 /*
2  * Control panel folder
3  *
4  * Copyright 2003 Martin Fuchs
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <stdlib.h>
25 #include <string.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28
29 #define COBJMACROS
30 #define NONAMELESSUNION
31 #define NONAMELESSSTRUCT
32
33 #include "winerror.h"
34 #include "windef.h"
35 #include "winbase.h"
36 #include "winreg.h"
37 #include "wingdi.h"
38 #include "winuser.h"
39
40 #include "ole2.h"
41 #include "shlguid.h"
42
43 #include "commctrl.h"
44 #include "cpanel.h"
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 "wine/debug.h"
52 #include "debughlp.h"
53 #include "shfldr.h"
54
55 WINE_DEFAULT_DEBUG_CHANNEL(shell);
56
57 /***********************************************************************
58 *   control panel implementation in shell namespace
59 */
60
61 typedef struct {
62     IShellFolder2      IShellFolder2_iface;
63     IPersistFolder2    IPersistFolder2_iface;
64     IShellExecuteHookW IShellExecuteHookW_iface;
65     IShellExecuteHookA IShellExecuteHookA_iface;
66     LONG               ref;
67
68     IUnknown *pUnkOuter;        /* used for aggregation */
69
70     /* both paths are parsible from the desktop */
71     LPITEMIDLIST pidlRoot;      /* absolute pidl */
72     int dwAttributes;           /* attributes returned by GetAttributesOf FIXME: use it */
73 } ICPanelImpl;
74
75 static const IShellFolder2Vtbl vt_ShellFolder2;
76 static const IPersistFolder2Vtbl vt_PersistFolder2;
77 static const IShellExecuteHookWVtbl vt_ShellExecuteHookW;
78 static const IShellExecuteHookAVtbl vt_ShellExecuteHookA;
79
80 static inline ICPanelImpl *impl_from_IShellFolder2(IShellFolder2 *iface)
81 {
82     return CONTAINING_RECORD(iface, ICPanelImpl, IShellFolder2_iface);
83 }
84
85 static inline ICPanelImpl *impl_from_IPersistFolder2(IPersistFolder2 *iface)
86 {
87     return CONTAINING_RECORD(iface, ICPanelImpl, IPersistFolder2_iface);
88 }
89
90 static inline ICPanelImpl *impl_from_IShellExecuteHookW(IShellExecuteHookW *iface)
91 {
92     return CONTAINING_RECORD(iface, ICPanelImpl, IShellExecuteHookW_iface);
93 }
94
95 static inline ICPanelImpl *impl_from_IShellExecuteHookA(IShellExecuteHookA *iface)
96 {
97     return CONTAINING_RECORD(iface, ICPanelImpl, IShellExecuteHookA_iface);
98 }
99
100
101 /***********************************************************************
102 *   IShellFolder [ControlPanel] implementation
103 */
104
105 static const shvheader ControlPanelSFHeader[] = {
106     {IDS_SHV_COLUMN8, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 15},/*FIXME*/
107     {IDS_SHV_COLUMN9, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_LEFT, 80},/*FIXME*/
108 };
109
110 #define CONROLPANELSHELLVIEWCOLUMNS 2
111
112 /**************************************************************************
113 *       IControlPanel_Constructor
114 */
115 HRESULT WINAPI IControlPanel_Constructor(IUnknown* pUnkOuter, REFIID riid, LPVOID * ppv)
116 {
117     ICPanelImpl *sf;
118
119     TRACE("unkOut=%p %s\n", pUnkOuter, shdebugstr_guid(riid));
120
121     if (!ppv)
122         return E_POINTER;
123     if (pUnkOuter && !IsEqualIID (riid, &IID_IUnknown))
124         return CLASS_E_NOAGGREGATION;
125
126     sf = LocalAlloc(LMEM_ZEROINIT, sizeof(ICPanelImpl));
127     if (!sf)
128         return E_OUTOFMEMORY;
129
130     sf->ref = 0;
131     sf->IShellFolder2_iface.lpVtbl = &vt_ShellFolder2;
132     sf->IPersistFolder2_iface.lpVtbl = &vt_PersistFolder2;
133     sf->IShellExecuteHookW_iface.lpVtbl = &vt_ShellExecuteHookW;
134     sf->IShellExecuteHookA_iface.lpVtbl = &vt_ShellExecuteHookA;
135     sf->pidlRoot = _ILCreateControlPanel();     /* my qualified pidl */
136     sf->pUnkOuter = pUnkOuter ? pUnkOuter : (IUnknown *)&sf->IShellFolder2_iface;
137
138     if (FAILED(IShellFolder2_QueryInterface(&sf->IShellFolder2_iface, riid, ppv))) {
139         IShellFolder2_Release(&sf->IShellFolder2_iface);
140         return E_NOINTERFACE;
141     }
142
143     TRACE("--(%p)\n", sf);
144     return S_OK;
145 }
146
147 /**************************************************************************
148  *      ISF_ControlPanel_fnQueryInterface
149  *
150  * NOTES supports not IPersist/IPersistFolder
151  */
152 static HRESULT WINAPI ISF_ControlPanel_fnQueryInterface(IShellFolder2 *iface, REFIID riid,
153         void **ppvObject)
154 {
155     ICPanelImpl *This = impl_from_IShellFolder2(iface);
156
157     TRACE("(%p)->(%s,%p)\n", This, shdebugstr_guid(riid), ppvObject);
158
159     *ppvObject = NULL;
160
161     if (IsEqualIID(riid, &IID_IUnknown) ||
162         IsEqualIID(riid, &IID_IShellFolder) || IsEqualIID(riid, &IID_IShellFolder2))
163         *ppvObject = This;
164     else if (IsEqualIID(riid, &IID_IPersist) ||
165                IsEqualIID(riid, &IID_IPersistFolder) || IsEqualIID(riid, &IID_IPersistFolder2))
166         *ppvObject = &This->IPersistFolder2_iface;
167     else if (IsEqualIID(riid, &IID_IShellExecuteHookW))
168         *ppvObject = &This->IShellExecuteHookW_iface;
169     else if (IsEqualIID(riid, &IID_IShellExecuteHookA))
170         *ppvObject = &This->IShellExecuteHookA_iface;
171
172     if (*ppvObject) {
173         IUnknown_AddRef((IUnknown *)(*ppvObject));
174         TRACE("-- Interface:(%p)->(%p)\n", ppvObject, *ppvObject);
175         return S_OK;
176     }
177     TRACE("-- Interface: E_NOINTERFACE\n");
178     return E_NOINTERFACE;
179 }
180
181 static ULONG WINAPI ISF_ControlPanel_fnAddRef(IShellFolder2 *iface)
182 {
183     ICPanelImpl *This = impl_from_IShellFolder2(iface);
184     ULONG refCount = InterlockedIncrement(&This->ref);
185
186     TRACE("(%p)->(count=%u)\n", This, refCount - 1);
187
188     return refCount;
189 }
190
191 static ULONG WINAPI ISF_ControlPanel_fnRelease(IShellFolder2 *iface)
192 {
193     ICPanelImpl *This = impl_from_IShellFolder2(iface);
194     ULONG refCount = InterlockedDecrement(&This->ref);
195
196     TRACE("(%p)->(count=%u)\n", This, refCount + 1);
197
198     if (!refCount) {
199         TRACE("-- destroying IShellFolder(%p)\n", This);
200         SHFree(This->pidlRoot);
201         LocalFree(This);
202     }
203     return refCount;
204 }
205
206 /**************************************************************************
207 *       ISF_ControlPanel_fnParseDisplayName
208 */
209 static HRESULT WINAPI ISF_ControlPanel_fnParseDisplayName(IShellFolder2 *iface, HWND hwndOwner,
210         LPBC pbc, LPOLESTR lpszDisplayName, DWORD *pchEaten, LPITEMIDLIST *ppidl,
211         DWORD *pdwAttributes)
212 {
213     ICPanelImpl *This = impl_from_IShellFolder2(iface);
214
215     HRESULT hr = E_INVALIDARG;
216
217     FIXME("(%p)->(HWND=%p,%p,%p=%s,%p,pidl=%p,%p)\n",
218            This, hwndOwner, pbc, lpszDisplayName, debugstr_w(lpszDisplayName), pchEaten, ppidl, pdwAttributes);
219
220     *ppidl = 0;
221     if (pchEaten)
222         *pchEaten = 0;
223
224     TRACE("(%p)->(-- ret=0x%08x)\n", This, hr);
225
226     return hr;
227 }
228
229 static LPITEMIDLIST _ILCreateCPanelApplet(LPCSTR name, LPCSTR displayName,
230  LPCSTR comment, int iconIdx)
231 {
232     PIDLCPanelStruct *p;
233     LPITEMIDLIST pidl;
234     PIDLDATA tmp;
235     int size0 = (char*)tmp.u.cpanel.szName-(char*)&tmp.u.cpanel;
236     int size = size0;
237     int l;
238
239     tmp.type = PT_CPLAPPLET;
240     tmp.u.cpanel.dummy = 0;
241     tmp.u.cpanel.iconIdx = iconIdx;
242
243     l = strlen(name);
244     size += l+1;
245
246     tmp.u.cpanel.offsDispName = l+1;
247     l = strlen(displayName);
248     size += l+1;
249
250     tmp.u.cpanel.offsComment = tmp.u.cpanel.offsDispName+1+l;
251     l = strlen(comment);
252     size += l+1;
253
254     pidl = SHAlloc(size+4);
255     if (!pidl)
256         return NULL;
257
258     pidl->mkid.cb = size+2;
259     memcpy(pidl->mkid.abID, &tmp, 2+size0);
260
261     p = &((PIDLDATA*)pidl->mkid.abID)->u.cpanel;
262     strcpy(p->szName, name);
263     strcpy(p->szName+tmp.u.cpanel.offsDispName, displayName);
264     strcpy(p->szName+tmp.u.cpanel.offsComment, comment);
265
266     *(WORD*)((char*)pidl+(size+2)) = 0;
267
268     pcheck(pidl);
269
270     return pidl;
271 }
272
273 /**************************************************************************
274  *  _ILGetCPanelPointer()
275  * gets a pointer to the control panel struct stored in the pidl
276  */
277 static PIDLCPanelStruct* _ILGetCPanelPointer(LPCITEMIDLIST pidl)
278 {
279     LPPIDLDATA pdata = _ILGetDataPointer(pidl);
280
281     if (pdata && pdata->type==PT_CPLAPPLET)
282         return &pdata->u.cpanel;
283
284     return NULL;
285 }
286
287  /**************************************************************************
288  *              ISF_ControlPanel_fnEnumObjects
289  */
290 static BOOL SHELL_RegisterCPanelApp(IEnumIDList* list, LPCSTR path)
291 {
292     LPITEMIDLIST pidl;
293     CPlApplet* applet;
294     CPanel panel;
295     CPLINFO info;
296     unsigned i;
297     int iconIdx;
298
299     char displayName[MAX_PATH];
300     char comment[MAX_PATH];
301
302     WCHAR wpath[MAX_PATH];
303
304     MultiByteToWideChar(CP_ACP, 0, path, -1, wpath, MAX_PATH);
305
306     panel.first = NULL;
307     applet = Control_LoadApplet(0, wpath, &panel);
308
309     if (applet)
310     {
311         for(i=0; i<applet->count; ++i)
312         {
313             WideCharToMultiByte(CP_ACP, 0, applet->info[i].szName, -1, displayName, MAX_PATH, 0, 0);
314             WideCharToMultiByte(CP_ACP, 0, applet->info[i].szInfo, -1, comment, MAX_PATH, 0, 0);
315
316             applet->proc(0, CPL_INQUIRE, i, (LPARAM)&info);
317
318             if (info.idIcon > 0)
319                 iconIdx = -info.idIcon; /* negative icon index instead of icon number */
320             else
321                 iconIdx = 0;
322
323             pidl = _ILCreateCPanelApplet(path, displayName, comment, iconIdx);
324
325             if (pidl)
326                 AddToEnumList(list, pidl);
327         }
328         Control_UnloadApplet(applet);
329     }
330     return TRUE;
331 }
332
333 static int SHELL_RegisterRegistryCPanelApps(IEnumIDList* list, HKEY hkey_root, LPCSTR szRepPath)
334 {
335     char name[MAX_PATH];
336     char value[MAX_PATH];
337     HKEY hkey;
338
339     int cnt = 0;
340
341     if (RegOpenKeyA(hkey_root, szRepPath, &hkey) == ERROR_SUCCESS)
342     {
343         int idx = 0;
344
345         for(;; ++idx)
346         {
347             DWORD nameLen = MAX_PATH;
348             DWORD valueLen = MAX_PATH;
349
350             if (RegEnumValueA(hkey, idx, name, &nameLen, NULL, NULL, (LPBYTE)value, &valueLen) != ERROR_SUCCESS)
351                 break;
352
353             if (SHELL_RegisterCPanelApp(list, value))
354                 ++cnt;
355         }
356         RegCloseKey(hkey);
357     }
358
359     return cnt;
360 }
361
362 static int SHELL_RegisterCPanelFolders(IEnumIDList* list, HKEY hkey_root, LPCSTR szRepPath)
363 {
364     char name[MAX_PATH];
365     HKEY hkey;
366
367     int cnt = 0;
368
369     if (RegOpenKeyA(hkey_root, szRepPath, &hkey) == ERROR_SUCCESS)
370     {
371         int idx = 0;
372         for(;; ++idx)
373         {
374             if (RegEnumKeyA(hkey, idx, name, MAX_PATH) != ERROR_SUCCESS)
375                 break;
376
377             if (*name == '{')
378             {
379                 LPITEMIDLIST pidl = _ILCreateGuidFromStrA(name);
380
381                 if (pidl && AddToEnumList(list, pidl))
382                     ++cnt;
383             }
384         }
385
386         RegCloseKey(hkey);
387     }
388
389   return cnt;
390 }
391
392 /**************************************************************************
393  *  CreateCPanelEnumList()
394  */
395 static BOOL CreateCPanelEnumList(
396     IEnumIDList * iface,
397     DWORD dwFlags)
398 {
399     CHAR szPath[MAX_PATH];
400     WIN32_FIND_DATAA wfd;
401     HANDLE hFile;
402
403     TRACE("(%p)->(flags=0x%08x)\n", iface, dwFlags);
404
405     /* enumerate control panel folders */
406     if (dwFlags & SHCONTF_FOLDERS)
407         SHELL_RegisterCPanelFolders(iface, HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\ControlPanel\\NameSpace");
408
409     /* enumerate the control panel applets */
410     if (dwFlags & SHCONTF_NONFOLDERS)
411     {
412         LPSTR p;
413
414         GetSystemDirectoryA(szPath, MAX_PATH);
415         p = PathAddBackslashA(szPath);
416         strcpy(p, "*.cpl");
417
418         TRACE("-- (%p)-> enumerate SHCONTF_NONFOLDERS of %s\n",iface,debugstr_a(szPath));
419         hFile = FindFirstFileA(szPath, &wfd);
420
421         if (hFile != INVALID_HANDLE_VALUE)
422         {
423             do
424             {
425                 if (!(dwFlags & SHCONTF_INCLUDEHIDDEN) && (wfd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN))
426                     continue;
427
428                 if (!(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
429                     strcpy(p, wfd.cFileName);
430                     SHELL_RegisterCPanelApp(iface, szPath);
431                 }
432             } while(FindNextFileA(hFile, &wfd));
433             FindClose(hFile);
434         }
435
436         SHELL_RegisterRegistryCPanelApps(iface, HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Control Panel\\Cpls");
437         SHELL_RegisterRegistryCPanelApps(iface, HKEY_CURRENT_USER, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Control Panel\\Cpls");
438     }
439     return TRUE;
440 }
441
442 /**************************************************************************
443 *               ISF_ControlPanel_fnEnumObjects
444 */
445 static HRESULT WINAPI ISF_ControlPanel_fnEnumObjects(IShellFolder2 *iface, HWND hwndOwner,
446         DWORD dwFlags, LPENUMIDLIST *ppEnumIDList)
447 {
448     ICPanelImpl *This = impl_from_IShellFolder2(iface);
449
450     TRACE("(%p)->(HWND=%p flags=0x%08x pplist=%p)\n", This, hwndOwner, dwFlags, ppEnumIDList);
451
452     *ppEnumIDList = IEnumIDList_Constructor();
453     if (*ppEnumIDList)
454         CreateCPanelEnumList(*ppEnumIDList, dwFlags);
455
456     TRACE("--(%p)->(new ID List: %p)\n", This, *ppEnumIDList);
457
458     return(*ppEnumIDList) ? S_OK : E_OUTOFMEMORY;
459 }
460
461 /**************************************************************************
462 *               ISF_ControlPanel_fnBindToObject
463 */
464 static HRESULT WINAPI ISF_ControlPanel_fnBindToObject(IShellFolder2 *iface, LPCITEMIDLIST pidl,
465         LPBC pbcReserved, REFIID riid, void **ppvOut)
466 {
467     ICPanelImpl *This = impl_from_IShellFolder2(iface);
468
469     TRACE("(%p)->(pidl=%p,%p,%s,%p)\n", This, pidl, pbcReserved, shdebugstr_guid(riid), ppvOut);
470
471     return SHELL32_BindToChild(This->pidlRoot, NULL, pidl, riid, ppvOut);
472 }
473
474 /**************************************************************************
475 *       ISF_ControlPanel_fnBindToStorage
476 */
477 static HRESULT WINAPI ISF_ControlPanel_fnBindToStorage(IShellFolder2 *iface, LPCITEMIDLIST pidl,
478         LPBC pbcReserved, REFIID riid, void **ppvOut)
479 {
480     ICPanelImpl *This = impl_from_IShellFolder2(iface);
481
482     FIXME("(%p)->(pidl=%p,%p,%s,%p) stub\n", This, pidl, pbcReserved, shdebugstr_guid(riid), ppvOut);
483
484     *ppvOut = NULL;
485     return E_NOTIMPL;
486 }
487
488 /**************************************************************************
489 *       ISF_ControlPanel_fnCompareIDs
490 */
491
492 static HRESULT WINAPI ISF_ControlPanel_fnCompareIDs(IShellFolder2 *iface, LPARAM lParam,
493         LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2)
494 {
495     ICPanelImpl *This = impl_from_IShellFolder2(iface);
496
497     int nReturn;
498
499     TRACE("(%p)->(0x%08lx,pidl1=%p,pidl2=%p)\n", This, lParam, pidl1, pidl2);
500     nReturn = SHELL32_CompareIDs((IShellFolder *)&This->IShellFolder2_iface, lParam, pidl1, pidl2);
501     TRACE("-- %i\n", nReturn);
502     return nReturn;
503 }
504
505 /**************************************************************************
506 *       ISF_ControlPanel_fnCreateViewObject
507 */
508 static HRESULT WINAPI ISF_ControlPanel_fnCreateViewObject(IShellFolder2 *iface, HWND hwndOwner,
509         REFIID riid, void **ppvOut)
510 {
511     ICPanelImpl *This = impl_from_IShellFolder2(iface);
512
513     LPSHELLVIEW pShellView;
514     HRESULT hr = E_INVALIDARG;
515
516     TRACE("(%p)->(hwnd=%p,%s,%p)\n", This, hwndOwner, shdebugstr_guid(riid), ppvOut);
517
518     if (ppvOut) {
519         *ppvOut = NULL;
520
521         if (IsEqualIID(riid, &IID_IDropTarget)) {
522             WARN("IDropTarget not implemented\n");
523             hr = E_NOTIMPL;
524         } else if (IsEqualIID(riid, &IID_IContextMenu)) {
525             WARN("IContextMenu not implemented\n");
526             hr = E_NOTIMPL;
527         } else if (IsEqualIID(riid, &IID_IShellView)) {
528             pShellView = IShellView_Constructor((IShellFolder *) iface);
529             if (pShellView) {
530                 hr = IShellView_QueryInterface(pShellView, riid, ppvOut);
531                 IShellView_Release(pShellView);
532             }
533         }
534     }
535     TRACE("--(%p)->(interface=%p)\n", This, ppvOut);
536     return hr;
537 }
538
539 /**************************************************************************
540 *  ISF_ControlPanel_fnGetAttributesOf
541 */
542 static HRESULT WINAPI ISF_ControlPanel_fnGetAttributesOf(IShellFolder2 *iface, UINT cidl,
543         LPCITEMIDLIST *apidl, DWORD *rgfInOut)
544 {
545     ICPanelImpl *This = impl_from_IShellFolder2(iface);
546
547     HRESULT hr = S_OK;
548
549     TRACE("(%p)->(cidl=%d apidl=%p mask=%p (0x%08x))\n",
550           This, cidl, apidl, rgfInOut, rgfInOut ? *rgfInOut : 0);
551
552     if (!rgfInOut)
553         return E_INVALIDARG;
554     if (cidl && !apidl)
555         return E_INVALIDARG;
556
557     if (*rgfInOut == 0)
558         *rgfInOut = ~0;
559
560     while(cidl > 0 && *apidl) {
561         pdump(*apidl);
562         SHELL32_GetItemAttributes((IShellFolder *)&This->IShellFolder2_iface, *apidl, rgfInOut);
563         apidl++;
564         cidl--;
565     }
566     /* make sure SFGAO_VALIDATE is cleared, some apps depend on that */
567     *rgfInOut &= ~SFGAO_VALIDATE;
568
569     TRACE("-- result=0x%08x\n", *rgfInOut);
570     return hr;
571 }
572
573 /**************************************************************************
574 *       ISF_ControlPanel_fnGetUIObjectOf
575 *
576 * PARAMETERS
577 *  HWND           hwndOwner, //[in ] Parent window for any output
578 *  UINT           cidl,      //[in ] array size
579 *  LPCITEMIDLIST* apidl,     //[in ] simple pidl array
580 *  REFIID         riid,      //[in ] Requested Interface
581 *  UINT*          prgfInOut, //[   ] reserved
582 *  LPVOID*        ppvObject) //[out] Resulting Interface
583 *
584 */
585 static HRESULT WINAPI ISF_ControlPanel_fnGetUIObjectOf(IShellFolder2 *iface, HWND hwndOwner,
586         UINT cidl, LPCITEMIDLIST *apidl, REFIID riid, UINT *prgfInOut, void **ppvOut)
587 {
588     ICPanelImpl *This = impl_from_IShellFolder2(iface);
589
590     LPITEMIDLIST pidl;
591     IUnknown *pObj = NULL;
592     HRESULT hr = E_INVALIDARG;
593
594     TRACE("(%p)->(%p,%u,apidl=%p,%s,%p,%p)\n",
595            This, hwndOwner, cidl, apidl, shdebugstr_guid(riid), prgfInOut, ppvOut);
596
597     if (ppvOut) {
598         *ppvOut = NULL;
599
600         if (IsEqualIID(riid, &IID_IContextMenu) &&(cidl >= 1)) {
601             pObj = (LPUNKNOWN) ISvItemCm_Constructor((IShellFolder *) iface, This->pidlRoot, apidl, cidl);
602             hr = S_OK;
603         } else if (IsEqualIID(riid, &IID_IDataObject) &&(cidl >= 1)) {
604             pObj = (LPUNKNOWN) IDataObject_Constructor(hwndOwner, This->pidlRoot, apidl, cidl);
605             hr = S_OK;
606         } else if (IsEqualIID(riid, &IID_IExtractIconA) &&(cidl == 1)) {
607             pidl = ILCombine(This->pidlRoot, apidl[0]);
608             pObj = (LPUNKNOWN) IExtractIconA_Constructor(pidl);
609             SHFree(pidl);
610             hr = S_OK;
611         } else if (IsEqualIID(riid, &IID_IExtractIconW) &&(cidl == 1)) {
612             pidl = ILCombine(This->pidlRoot, apidl[0]);
613             pObj = (LPUNKNOWN) IExtractIconW_Constructor(pidl);
614             SHFree(pidl);
615             hr = S_OK;
616         } else if ((IsEqualIID(riid,&IID_IShellLinkW) || IsEqualIID(riid,&IID_IShellLinkA))
617                                 && (cidl == 1)) {
618             pidl = ILCombine(This->pidlRoot, apidl[0]);
619             hr = IShellLink_ConstructFromFile(NULL, riid, pidl,(LPVOID*)&pObj);
620             SHFree(pidl);
621         } else {
622             hr = E_NOINTERFACE;
623         }
624
625         if (SUCCEEDED(hr) && !pObj)
626             hr = E_OUTOFMEMORY;
627
628         *ppvOut = pObj;
629     }
630     TRACE("(%p)->hr=0x%08x\n", This, hr);
631     return hr;
632 }
633
634 /**************************************************************************
635 *       ISF_ControlPanel_fnGetDisplayNameOf
636 */
637 static HRESULT WINAPI ISF_ControlPanel_fnGetDisplayNameOf(IShellFolder2 *iface, LPCITEMIDLIST pidl,
638         DWORD dwFlags, LPSTRRET strRet)
639 {
640     ICPanelImpl *This = impl_from_IShellFolder2(iface);
641
642     CHAR szPath[MAX_PATH];
643     WCHAR wszPath[MAX_PATH+1]; /* +1 for potential backslash */
644     PIDLCPanelStruct* pcpanel;
645
646     *szPath = '\0';
647
648     TRACE("(%p)->(pidl=%p,0x%08x,%p)\n", This, pidl, dwFlags, strRet);
649     pdump(pidl);
650
651     if (!pidl || !strRet)
652         return E_INVALIDARG;
653
654     pcpanel = _ILGetCPanelPointer(pidl);
655
656     if (pcpanel) {
657         lstrcpyA(szPath, pcpanel->szName+pcpanel->offsDispName);
658
659         if (!(dwFlags & SHGDN_FORPARSING))
660             FIXME("retrieve display name from control panel app\n");
661     }
662     /* take names of special folders only if it's only this folder */
663     else if (_ILIsSpecialFolder(pidl)) {
664         BOOL bSimplePidl = _ILIsPidlSimple(pidl);
665
666         if (bSimplePidl) {
667             _ILSimpleGetTextW(pidl, wszPath, MAX_PATH); /* append my own path */
668         } else {
669             FIXME("special pidl\n");
670         }
671
672         if ((dwFlags & SHGDN_FORPARSING) && !bSimplePidl) { /* go deeper if needed */
673             int len = 0;
674
675             PathAddBackslashW(wszPath);
676             len = lstrlenW(wszPath);
677
678             if (FAILED(SHELL32_GetDisplayNameOfChild(iface, pidl, dwFlags | SHGDN_INFOLDER, wszPath + len, MAX_PATH + 1 - len)))
679                 return E_OUTOFMEMORY;
680             if (!WideCharToMultiByte(CP_ACP, 0, wszPath, -1, szPath, MAX_PATH, NULL, NULL))
681                 wszPath[0] = '\0';
682         }
683     }
684
685     strRet->uType = STRRET_CSTR;
686     lstrcpynA(strRet->u.cStr, szPath, MAX_PATH);
687
688     TRACE("--(%p)->(%s)\n", This, szPath);
689     return S_OK;
690 }
691
692 /**************************************************************************
693 *  ISF_ControlPanel_fnSetNameOf
694 *  Changes the name of a file object or subfolder, possibly changing its item
695 *  identifier in the process.
696 *
697 * PARAMETERS
698 *  HWND          hwndOwner,  //[in ] Owner window for output
699 *  LPCITEMIDLIST pidl,       //[in ] simple pidl of item to change
700 *  LPCOLESTR     lpszName,   //[in ] the items new display name
701 *  DWORD         dwFlags,    //[in ] SHGNO formatting flags
702 *  LPITEMIDLIST* ppidlOut)   //[out] simple pidl returned
703 */
704 static HRESULT WINAPI ISF_ControlPanel_fnSetNameOf(IShellFolder2 *iface, HWND hwndOwner,
705         LPCITEMIDLIST pidl, LPCOLESTR lpName, DWORD dwFlags, LPITEMIDLIST *pPidlOut)
706 {
707     ICPanelImpl *This = impl_from_IShellFolder2(iface);
708     FIXME("(%p)->(%p,pidl=%p,%s,%u,%p)\n", This, hwndOwner, pidl, debugstr_w(lpName), dwFlags, pPidlOut);
709     return E_FAIL;
710 }
711
712 static HRESULT WINAPI ISF_ControlPanel_fnGetDefaultSearchGUID(IShellFolder2 *iface, GUID *pguid)
713 {
714     ICPanelImpl *This = impl_from_IShellFolder2(iface);
715     FIXME("(%p)\n", This);
716     return E_NOTIMPL;
717 }
718 static HRESULT WINAPI ISF_ControlPanel_fnEnumSearches(IShellFolder2 *iface,
719         IEnumExtraSearch **ppenum)
720 {
721     ICPanelImpl *This = impl_from_IShellFolder2(iface);
722     FIXME("(%p)\n", This);
723     return E_NOTIMPL;
724 }
725 static HRESULT WINAPI ISF_ControlPanel_fnGetDefaultColumn(IShellFolder2 *iface, DWORD dwRes,
726         ULONG *pSort, ULONG *pDisplay)
727 {
728     ICPanelImpl *This = impl_from_IShellFolder2(iface);
729
730     TRACE("(%p)\n", This);
731
732     if (pSort) *pSort = 0;
733     if (pDisplay) *pDisplay = 0;
734     return S_OK;
735 }
736 static HRESULT WINAPI ISF_ControlPanel_fnGetDefaultColumnState(IShellFolder2 *iface, UINT iColumn,
737         DWORD *pcsFlags)
738 {
739     ICPanelImpl *This = impl_from_IShellFolder2(iface);
740
741     TRACE("(%p)\n", This);
742
743     if (!pcsFlags || iColumn >= CONROLPANELSHELLVIEWCOLUMNS) return E_INVALIDARG;
744     *pcsFlags = ControlPanelSFHeader[iColumn].pcsFlags;
745     return S_OK;
746 }
747 static HRESULT WINAPI ISF_ControlPanel_fnGetDetailsEx(IShellFolder2 *iface, LPCITEMIDLIST pidl,
748         const SHCOLUMNID *pscid, VARIANT *pv)
749 {
750     ICPanelImpl *This = impl_from_IShellFolder2(iface);
751     FIXME("(%p)\n", This);
752     return E_NOTIMPL;
753 }
754
755 static HRESULT WINAPI ISF_ControlPanel_fnGetDetailsOf(IShellFolder2 *iface, LPCITEMIDLIST pidl,
756         UINT iColumn, SHELLDETAILS *psd)
757 {
758     ICPanelImpl *This = impl_from_IShellFolder2(iface);
759     PIDLCPanelStruct* pcpanel;
760     HRESULT hr;
761
762     TRACE("(%p)->(%p %i %p)\n", This, pidl, iColumn, psd);
763
764     if (!psd || iColumn >= CONROLPANELSHELLVIEWCOLUMNS)
765         return E_INVALIDARG;
766
767     if (!pidl) {
768         psd->fmt = ControlPanelSFHeader[iColumn].fmt;
769         psd->cxChar = ControlPanelSFHeader[iColumn].cxChar;
770         psd->str.uType = STRRET_CSTR;
771         LoadStringA(shell32_hInstance, ControlPanelSFHeader[iColumn].colnameid, psd->str.u.cStr, MAX_PATH);
772         return S_OK;
773     } else {
774         psd->str.u.cStr[0] = 0x00;
775         psd->str.uType = STRRET_CSTR;
776         switch(iColumn) {
777         case 0:         /* name */
778             hr = IShellFolder_GetDisplayNameOf(iface, pidl, SHGDN_NORMAL | SHGDN_INFOLDER, &psd->str);
779             break;
780         case 1:         /* comment */
781             pcpanel = _ILGetCPanelPointer(pidl);
782
783             if (pcpanel)
784                 lstrcpyA(psd->str.u.cStr, pcpanel->szName+pcpanel->offsComment);
785             else
786                 _ILGetFileType(pidl, psd->str.u.cStr, MAX_PATH);
787
788             break;
789         }
790         hr = S_OK;
791     }
792
793     return hr;
794 }
795 static HRESULT WINAPI ISF_ControlPanel_fnMapColumnToSCID(IShellFolder2 *iface, UINT column,
796         SHCOLUMNID *pscid)
797 {
798     ICPanelImpl *This = impl_from_IShellFolder2(iface);
799     FIXME("(%p)\n", This);
800     return E_NOTIMPL;
801 }
802
803 static const IShellFolder2Vtbl vt_ShellFolder2 =
804 {
805
806     ISF_ControlPanel_fnQueryInterface,
807     ISF_ControlPanel_fnAddRef,
808     ISF_ControlPanel_fnRelease,
809     ISF_ControlPanel_fnParseDisplayName,
810     ISF_ControlPanel_fnEnumObjects,
811     ISF_ControlPanel_fnBindToObject,
812     ISF_ControlPanel_fnBindToStorage,
813     ISF_ControlPanel_fnCompareIDs,
814     ISF_ControlPanel_fnCreateViewObject,
815     ISF_ControlPanel_fnGetAttributesOf,
816     ISF_ControlPanel_fnGetUIObjectOf,
817     ISF_ControlPanel_fnGetDisplayNameOf,
818     ISF_ControlPanel_fnSetNameOf,
819
820     /* ShellFolder2 */
821     ISF_ControlPanel_fnGetDefaultSearchGUID,
822     ISF_ControlPanel_fnEnumSearches,
823     ISF_ControlPanel_fnGetDefaultColumn,
824     ISF_ControlPanel_fnGetDefaultColumnState,
825     ISF_ControlPanel_fnGetDetailsEx,
826     ISF_ControlPanel_fnGetDetailsOf,
827     ISF_ControlPanel_fnMapColumnToSCID
828 };
829
830 /************************************************************************
831  *      ICPanel_PersistFolder2_QueryInterface
832  */
833 static HRESULT WINAPI ICPanel_PersistFolder2_QueryInterface(IPersistFolder2 * iface, REFIID iid, LPVOID * ppvObject)
834 {
835     ICPanelImpl *This = impl_from_IPersistFolder2(iface);
836
837     TRACE("(%p)\n", This);
838
839     return IShellFolder2_QueryInterface(&This->IShellFolder2_iface, iid, ppvObject);
840 }
841
842 /************************************************************************
843  *      ICPanel_PersistFolder2_AddRef
844  */
845 static ULONG WINAPI ICPanel_PersistFolder2_AddRef(IPersistFolder2 * iface)
846 {
847     ICPanelImpl *This = impl_from_IPersistFolder2(iface);
848
849     TRACE("(%p)->(count=%u)\n", This, This->ref);
850
851     return IShellFolder2_AddRef(&This->IShellFolder2_iface);
852 }
853
854 /************************************************************************
855  *      ISFPersistFolder_Release
856  */
857 static ULONG WINAPI ICPanel_PersistFolder2_Release(IPersistFolder2 * iface)
858 {
859     ICPanelImpl *This = impl_from_IPersistFolder2(iface);
860
861     TRACE("(%p)->(count=%u)\n", This, This->ref);
862
863     return IShellFolder2_Release(&This->IShellFolder2_iface);
864 }
865
866 /************************************************************************
867  *      ICPanel_PersistFolder2_GetClassID
868  */
869 static HRESULT WINAPI ICPanel_PersistFolder2_GetClassID(IPersistFolder2 * iface, CLSID * lpClassId)
870 {
871     ICPanelImpl *This = impl_from_IPersistFolder2(iface);
872
873     TRACE("(%p)\n", This);
874
875     if (!lpClassId)
876         return E_POINTER;
877     *lpClassId = CLSID_ControlPanel;
878
879     return S_OK;
880 }
881
882 /************************************************************************
883  *      ICPanel_PersistFolder2_Initialize
884  *
885  * NOTES: it makes no sense to change the pidl
886  */
887 static HRESULT WINAPI ICPanel_PersistFolder2_Initialize(IPersistFolder2 * iface, LPCITEMIDLIST pidl)
888 {
889     ICPanelImpl *This = impl_from_IPersistFolder2(iface);
890     TRACE("(%p)->(%p)\n", This, pidl);
891     return E_NOTIMPL;
892 }
893
894 /**************************************************************************
895  *      IPersistFolder2_fnGetCurFolder
896  */
897 static HRESULT WINAPI ICPanel_PersistFolder2_GetCurFolder(IPersistFolder2 * iface, LPITEMIDLIST * pidl)
898 {
899     ICPanelImpl *This = impl_from_IPersistFolder2(iface);
900
901     TRACE("(%p)->(%p)\n", This, pidl);
902
903     if (!pidl)
904         return E_POINTER;
905     *pidl = ILClone(This->pidlRoot);
906     return S_OK;
907 }
908
909 static const IPersistFolder2Vtbl vt_PersistFolder2 =
910 {
911
912     ICPanel_PersistFolder2_QueryInterface,
913     ICPanel_PersistFolder2_AddRef,
914     ICPanel_PersistFolder2_Release,
915     ICPanel_PersistFolder2_GetClassID,
916     ICPanel_PersistFolder2_Initialize,
917     ICPanel_PersistFolder2_GetCurFolder
918 };
919
920 HRESULT CPanel_GetIconLocationW(LPCITEMIDLIST pidl,
921                LPWSTR szIconFile, UINT cchMax, int* piIndex)
922 {
923     PIDLCPanelStruct* pcpanel = _ILGetCPanelPointer(pidl);
924
925     if (!pcpanel)
926         return E_INVALIDARG;
927
928     MultiByteToWideChar(CP_ACP, 0, pcpanel->szName, -1, szIconFile, cchMax);
929     *piIndex = pcpanel->iconIdx!=-1? pcpanel->iconIdx: 0;
930
931     return S_OK;
932 }
933
934
935 /**************************************************************************
936 * IShellExecuteHookW Implementation
937 */
938
939 static HRESULT WINAPI IShellExecuteHookW_fnQueryInterface(
940                IShellExecuteHookW* iface, REFIID riid, void** ppvObject)
941 {
942     ICPanelImpl *This = impl_from_IShellExecuteHookW(iface);
943
944     TRACE("(%p)->(count=%u)\n", This, This->ref);
945
946     return IUnknown_QueryInterface(This->pUnkOuter, riid, ppvObject);
947 }
948
949 static ULONG STDMETHODCALLTYPE IShellExecuteHookW_fnAddRef(IShellExecuteHookW* iface)
950 {
951     ICPanelImpl *This = impl_from_IShellExecuteHookW(iface);
952
953     TRACE("(%p)->(count=%u)\n", This, This->ref);
954
955     return IUnknown_AddRef(This->pUnkOuter);
956 }
957
958 static ULONG STDMETHODCALLTYPE IShellExecuteHookW_fnRelease(IShellExecuteHookW* iface)
959 {
960     ICPanelImpl *This = impl_from_IShellExecuteHookW(iface);
961
962     TRACE("(%p)\n", This);
963
964     return IUnknown_Release(This->pUnkOuter);
965 }
966
967 static HRESULT WINAPI IShellExecuteHookW_fnExecute(IShellExecuteHookW *iface,
968         LPSHELLEXECUTEINFOW psei)
969 {
970     ICPanelImpl *This = impl_from_IShellExecuteHookW(iface);
971     static const WCHAR wCplopen[] = {'c','p','l','o','p','e','n','\0'};
972
973     SHELLEXECUTEINFOW sei_tmp;
974     PIDLCPanelStruct* pcpanel;
975     WCHAR path[MAX_PATH];
976     WCHAR params[MAX_PATH];
977     BOOL ret;
978     int l;
979
980     TRACE("(%p)->execute(%p)\n", This, psei);
981
982     if (!psei)
983         return E_INVALIDARG;
984
985     pcpanel = _ILGetCPanelPointer(ILFindLastID(psei->lpIDList));
986
987     if (!pcpanel)
988         return E_INVALIDARG;
989
990     path[0] = '\"';
991     /* Return value from MultiByteToWideChar includes terminating NUL, which
992      * compensates for the starting double quote we just put in */
993     l = MultiByteToWideChar(CP_ACP, 0, pcpanel->szName, -1, path+1, MAX_PATH-1);
994
995     /* pass applet name to Control_RunDLL to distinguish between applets in one .cpl file */
996     path[l++] = '"';
997     path[l] = '\0';
998
999     MultiByteToWideChar(CP_ACP, 0, pcpanel->szName+pcpanel->offsDispName, -1, params, MAX_PATH);
1000
1001     sei_tmp = *psei;
1002     sei_tmp.lpFile = path;
1003     sei_tmp.lpParameters = params;
1004     sei_tmp.fMask &= ~SEE_MASK_INVOKEIDLIST;
1005     sei_tmp.lpVerb = wCplopen;
1006
1007     ret = ShellExecuteExW(&sei_tmp);
1008     if (ret)
1009         return S_OK;
1010     else
1011         return S_FALSE;
1012 }
1013
1014 static const IShellExecuteHookWVtbl vt_ShellExecuteHookW =
1015 {
1016
1017     IShellExecuteHookW_fnQueryInterface,
1018     IShellExecuteHookW_fnAddRef,
1019     IShellExecuteHookW_fnRelease,
1020
1021     IShellExecuteHookW_fnExecute
1022 };
1023
1024
1025 /**************************************************************************
1026 * IShellExecuteHookA Implementation
1027 */
1028
1029 static HRESULT WINAPI IShellExecuteHookA_fnQueryInterface(IShellExecuteHookA* iface, REFIID riid, void** ppvObject)
1030 {
1031     ICPanelImpl *This = impl_from_IShellExecuteHookA(iface);
1032
1033     TRACE("(%p)->(count=%u)\n", This, This->ref);
1034
1035     return IUnknown_QueryInterface(This->pUnkOuter, riid, ppvObject);
1036 }
1037
1038 static ULONG STDMETHODCALLTYPE IShellExecuteHookA_fnAddRef(IShellExecuteHookA* iface)
1039 {
1040     ICPanelImpl *This = impl_from_IShellExecuteHookA(iface);
1041
1042     TRACE("(%p)->(count=%u)\n", This, This->ref);
1043
1044     return IUnknown_AddRef(This->pUnkOuter);
1045 }
1046
1047 static ULONG STDMETHODCALLTYPE IShellExecuteHookA_fnRelease(IShellExecuteHookA* iface)
1048 {
1049     ICPanelImpl *This = impl_from_IShellExecuteHookA(iface);
1050
1051     TRACE("(%p)\n", This);
1052
1053     return IUnknown_Release(This->pUnkOuter);
1054 }
1055
1056 static HRESULT WINAPI IShellExecuteHookA_fnExecute(IShellExecuteHookA *iface,
1057         LPSHELLEXECUTEINFOA psei)
1058 {
1059     ICPanelImpl *This = impl_from_IShellExecuteHookA(iface);
1060
1061     SHELLEXECUTEINFOA sei_tmp;
1062     PIDLCPanelStruct* pcpanel;
1063     char path[MAX_PATH];
1064     BOOL ret;
1065
1066     TRACE("(%p)->execute(%p)\n", This, psei);
1067
1068     if (!psei)
1069         return E_INVALIDARG;
1070
1071     pcpanel = _ILGetCPanelPointer(ILFindLastID(psei->lpIDList));
1072
1073     if (!pcpanel)
1074         return E_INVALIDARG;
1075
1076     path[0] = '\"';
1077     lstrcpyA(path+1, pcpanel->szName);
1078
1079     /* pass applet name to Control_RunDLL to distinguish between applets in one .cpl file */
1080     lstrcatA(path, "\" ");
1081     lstrcatA(path, pcpanel->szName+pcpanel->offsDispName);
1082
1083     sei_tmp = *psei;
1084     sei_tmp.lpFile = path;
1085     sei_tmp.fMask &= ~SEE_MASK_INVOKEIDLIST;
1086
1087     ret = ShellExecuteExA(&sei_tmp);
1088     if (ret)
1089         return S_OK;
1090     else
1091         return S_FALSE;
1092 }
1093
1094 static const IShellExecuteHookAVtbl vt_ShellExecuteHookA =
1095 {
1096     IShellExecuteHookA_fnQueryInterface,
1097     IShellExecuteHookA_fnAddRef,
1098     IShellExecuteHookA_fnRelease,
1099     IShellExecuteHookA_fnExecute
1100 };