xref: /reactos/dll/win32/shell32/shell32.cpp (revision 3a49e26f)
1 /*
2  *                 Shell basics
3  *
4  * Copyright 1998 Marcus Meissner
5  * Copyright 1998 Juergen Schmied (jsch)  *  <juergen.schmied@metronet.de>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21 
22 #include "precomp.h"
23 
24 #include "shell32_version.h"
25 
26 WINE_DEFAULT_DEBUG_CHANNEL(shell);
27 
28 /*
29  * Implemented
30  */
31 EXTERN_C LPWSTR
32 WINAPI
33 AddCommasW(DWORD lValue, LPWSTR lpNumber)
34 {
35     WCHAR szValue[MAX_PATH], szSeparator[8 + 1];
36     NUMBERFMTW numFormat;
37 
38     GetLocaleInfoW(LOCALE_USER_DEFAULT,
39                    LOCALE_STHOUSAND,
40                    szSeparator,
41                    _countof(szSeparator));
42 
43     numFormat.NumDigits     = 0;
44     numFormat.LeadingZero   = 0;
45     numFormat.Grouping      = 3; // FIXME! Use GetLocaleInfoW with LOCALE_SGROUPING and interpret the result.
46     numFormat.lpDecimalSep  = szSeparator;
47     numFormat.lpThousandSep = szSeparator;
48     numFormat.NegativeOrder = 0;
49 
50     swprintf(szValue, L"%lu", lValue);
51 
52     if (GetNumberFormatW(LOCALE_USER_DEFAULT,
53                          0,
54                          szValue,
55                          &numFormat,
56                          lpNumber,
57                          MAX_PATH) != 0)
58     {
59         return lpNumber;
60     }
61 
62     wcscpy(lpNumber, szValue);
63     return lpNumber;
64 }
65 
66 /*
67  * Implemented
68  */
69 EXTERN_C BOOL
70 WINAPI
71 RegenerateUserEnvironment(LPVOID *lpEnvironment, BOOL bUpdateSelf)
72 {
73     HANDLE hUserToken;
74     if (!OpenProcessToken(GetCurrentProcess(), TOKEN_READ | TOKEN_WRITE, &hUserToken))
75         return FALSE;
76 
77     BOOL bResult = CreateEnvironmentBlock(lpEnvironment, hUserToken, TRUE);
78     if (!bResult || !lpEnvironment)
79     {
80         CloseHandle(hUserToken);
81         return FALSE;
82     }
83 
84     if (bUpdateSelf)
85     {
86         LPWSTR pszz = (LPWSTR)*lpEnvironment;
87         if (!pszz)
88             return FALSE;
89 
90         while (*pszz)
91         {
92             size_t cch = wcslen(pszz);
93             LPWSTR pchEqual = wcschr(pszz, L'=');
94             if (pchEqual)
95             {
96                 CStringW strName(pszz, pchEqual - pszz);
97                 SetEnvironmentVariableW(strName, pchEqual + 1);
98             }
99             pszz += cch + 1;
100         }
101     }
102 
103     CloseHandle(hUserToken);
104 
105     return bResult;
106 }
107 
108 /**************************************************************************
109  * Default ClassFactory types
110  */
111 typedef HRESULT (CALLBACK *LPFNCREATEINSTANCE)(IUnknown* pUnkOuter, REFIID riid, LPVOID* ppvObject);
112 HRESULT IDefClF_fnConstructor(LPFNCREATEINSTANCE lpfnCI, PLONG pcRefDll, const IID *riidInst, IClassFactory **theFactory);
113 
114 
115 /**************************************************************************
116  * Default ClassFactory Implementation
117  *
118  * SHCreateDefClassObject
119  *
120  * NOTES
121  *  Helper function for dlls without their own classfactory.
122  *  A generic classfactory is returned.
123  *  When the CreateInstance of the cf is called the callback is executed.
124  */
125 
126 class IDefClFImpl :
127     public CComObjectRootEx<CComMultiThreadModelNoCS>,
128     public IClassFactory
129 {
130 private:
131     CLSID                    *rclsid;
132     LPFNCREATEINSTANCE        lpfnCI;
133     const IID                *riidInst;
134     LONG                    *pcRefDll;        /* pointer to refcounter in external dll (ugrrr...) */
135 public:
136     IDefClFImpl();
137     HRESULT Initialize(LPFNCREATEINSTANCE lpfnCI, PLONG pcRefDll, const IID *riidInstx);
138 
139     // IClassFactory
140     STDMETHOD(CreateInstance)(IUnknown * pUnkOuter, REFIID riid, LPVOID *ppvObject) override;
141     STDMETHOD(LockServer)(BOOL fLock) override;
142 
143 BEGIN_COM_MAP(IDefClFImpl)
144     COM_INTERFACE_ENTRY_IID(IID_IClassFactory, IClassFactory)
145 END_COM_MAP()
146 };
147 
148 IDefClFImpl::IDefClFImpl()
149 {
150     lpfnCI = NULL;
151     riidInst = NULL;
152     pcRefDll = NULL;
153     rclsid = NULL;
154 }
155 
156 HRESULT IDefClFImpl::Initialize(LPFNCREATEINSTANCE lpfnCIx, PLONG pcRefDllx, const IID *riidInstx)
157 {
158     lpfnCI = lpfnCIx;
159     riidInst = riidInstx;
160     pcRefDll = pcRefDllx;
161 
162     if (pcRefDll)
163         InterlockedIncrement(pcRefDll);
164 
165     TRACE("(%p)%s\n", this, shdebugstr_guid(riidInst));
166     return S_OK;
167 }
168 
169 /******************************************************************************
170  * IDefClF_fnCreateInstance
171  */
172 HRESULT WINAPI IDefClFImpl::CreateInstance(IUnknown * pUnkOuter, REFIID riid, LPVOID *ppvObject)
173 {
174     TRACE("%p->(%p,%s,%p)\n", this, pUnkOuter, shdebugstr_guid(&riid), ppvObject);
175 
176     *ppvObject = NULL;
177 
178     if (riidInst == NULL || IsEqualCLSID(riid, *riidInst) || IsEqualCLSID(riid, IID_IUnknown))
179     {
180         return lpfnCI(pUnkOuter, riid, ppvObject);
181     }
182 
183     ERR("unknown IID requested %s\n", shdebugstr_guid(&riid));
184     return E_NOINTERFACE;
185 }
186 
187 /******************************************************************************
188  * IDefClF_fnLockServer
189  */
190 HRESULT WINAPI IDefClFImpl::LockServer(BOOL fLock)
191 {
192     TRACE("%p->(0x%x), not implemented\n", this, fLock);
193     return E_NOTIMPL;
194 }
195 
196 /**************************************************************************
197  *  IDefClF_fnConstructor
198  */
199 
200 HRESULT IDefClF_fnConstructor(LPFNCREATEINSTANCE lpfnCI, PLONG pcRefDll, const IID *riidInst, IClassFactory **theFactory)
201 {
202     return ShellObjectCreatorInit<IDefClFImpl>(lpfnCI, pcRefDll, riidInst, IID_PPV_ARG(IClassFactory, theFactory));
203 }
204 
205 /******************************************************************************
206  * SHCreateDefClassObject            [SHELL32.70]
207  */
208 HRESULT WINAPI SHCreateDefClassObject(
209     REFIID    riid,
210     LPVOID*    ppv,
211     LPFNCREATEINSTANCE lpfnCI,    /* [in] create instance callback entry */
212     LPDWORD    pcRefDll,        /* [in/out] ref count of the dll */
213     REFIID    riidInst)        /* [in] optional interface to the instance */
214 {
215     IClassFactory                *pcf;
216     HRESULT                        hResult;
217 
218     TRACE("%s %p %p %p %s\n", shdebugstr_guid(&riid), ppv, lpfnCI, pcRefDll, shdebugstr_guid(&riidInst));
219 
220     if (!IsEqualCLSID(riid, IID_IClassFactory))
221         return E_NOINTERFACE;
222     hResult = IDefClF_fnConstructor(lpfnCI, (PLONG)pcRefDll, &riidInst, &pcf);
223     if (FAILED(hResult))
224         return hResult;
225     *ppv = pcf;
226     return S_OK;
227 }
228 
229 /**************************************************************************
230  *  CStartMenuDummy
231  */
232 class CStartMenuDummy :
233     public CComCoClass<CStartMenuDummy, &CLSID_StartMenu>,
234     public CComObjectRootEx<CComMultiThreadModelNoCS>
235 {
236 private:
237     CStartMenuDummy();
238     virtual ~CStartMenuDummy();
239 
240 public:
241     DECLARE_REGISTRY_RESOURCEID(IDR_STARTMENU)
242 
243     class _CreatorClass
244     {
245     public:
246         static STDMETHODIMP CreateInstance(void *pv, REFIID riid, LPVOID *ppv)
247         {
248             if (ppv == NULL)
249                 return E_POINTER;
250             *ppv = NULL;
251             if (pv != NULL)
252                 return CLASS_E_NOAGGREGATION;
253             return RSHELL_CStartMenu_CreateInstance(riid, ppv);
254         }
255     };
256 };
257 
258 /**************************************************************************
259  *  CShell32Module
260  */
261 class CShell32Module : public CComModule
262 {
263 public:
264     void Term()
265     {
266         CComCreatorCentralInstance< ATL::CComObject< CDrivesFolder > >::Term();
267         CComCreatorCentralInstance< ATL::CComObject< CDesktopFolder > >::Term();
268         CComModule::Term();
269     }
270 };
271 
272 
273 BEGIN_OBJECT_MAP(ObjectMap)
274     OBJECT_ENTRY(CLSID_ActiveDesktop, CActiveDesktop)
275     OBJECT_ENTRY(CLSID_ShellFSFolder, CFSFolder)
276     OBJECT_ENTRY(CLSID_MyComputer, CDrivesFolder)
277     OBJECT_ENTRY(CLSID_ShellDesktop, CDesktopFolder)
278     OBJECT_ENTRY(CLSID_ShellItem, CShellItem)
279     OBJECT_ENTRY(CLSID_ShellLink, CShellLink)
280     OBJECT_ENTRY(CLSID_Shell, CShellDispatch)
281     OBJECT_ENTRY(CLSID_DragDropHelper, CDropTargetHelper)
282     OBJECT_ENTRY(CLSID_ControlPanel, CControlPanelFolder)
283     OBJECT_ENTRY(CLSID_OpenControlPanel, COpenControlPanel)
284     OBJECT_ENTRY(CLSID_MyDocuments, CMyDocsFolder)
285     OBJECT_ENTRY(CLSID_NetworkPlaces, CNetFolder)
286     OBJECT_ENTRY(CLSID_FontsFolderShortcut, CFontsFolder)
287     OBJECT_ENTRY(CLSID_Printers, CPrinterFolder)
288     OBJECT_ENTRY(CLSID_AdminFolderShortcut, CAdminToolsFolder)
289     OBJECT_ENTRY(CLSID_ShellFldSetExt, CFolderOptions)
290     OBJECT_ENTRY(CLSID_RecycleBin, CRecycleBin)
291     OBJECT_ENTRY(CLSID_OpenWithMenu, COpenWithMenu)
292     OBJECT_ENTRY(CLSID_NewMenu, CNewMenu)
293     OBJECT_ENTRY(CLSID_SendToMenu, CSendToMenu)
294     OBJECT_ENTRY(CLSID_CopyToMenu, CCopyToMenu)
295     OBJECT_ENTRY(CLSID_MoveToMenu, CMoveToMenu)
296     OBJECT_ENTRY(CLSID_StartMenu, CStartMenuDummy)
297     OBJECT_ENTRY(CLSID_MenuBandSite, CMenuSite)
298     OBJECT_ENTRY(CLSID_MenuBand, CMenuBand)
299     OBJECT_ENTRY(CLSID_MenuDeskBar, CMenuDeskBar)
300     OBJECT_ENTRY(CLSID_MergedFolder, CMergedFolder)
301     OBJECT_ENTRY(CLSID_ExeDropHandler, CExeDropHandler)
302     OBJECT_ENTRY(CLSID_QueryAssociations, CQueryAssociations)
303     OBJECT_ENTRY(CLSID_UserNotification, CUserNotification)
304 END_OBJECT_MAP()
305 
306 CShell32Module  gModule;
307 
308 
309 /***********************************************************************
310  * DllGetVersion [SHELL32.@]
311  *
312  * Retrieves version information of the 'SHELL32.DLL'
313  *
314  * PARAMS
315  *     pdvi [O] pointer to version information structure.
316  *
317  * RETURNS
318  *     Success: S_OK
319  *     Failure: E_INVALIDARG
320  *
321  * NOTES
322  *     Returns version of a shell32.dll from IE4.01 SP1.
323  */
324 
325 STDAPI DllGetVersion(DLLVERSIONINFO *pdvi)
326 {
327     /* FIXME: shouldn't these values come from the version resource? */
328     if (pdvi->cbSize == sizeof(DLLVERSIONINFO) ||
329         pdvi->cbSize == sizeof(DLLVERSIONINFO2))
330     {
331         pdvi->dwMajorVersion = WINE_FILEVERSION_MAJOR;
332         pdvi->dwMinorVersion = WINE_FILEVERSION_MINOR;
333         pdvi->dwBuildNumber = WINE_FILEVERSION_BUILD;
334         pdvi->dwPlatformID = WINE_FILEVERSION_PLATFORMID;
335         if (pdvi->cbSize == sizeof(DLLVERSIONINFO2))
336         {
337             DLLVERSIONINFO2 *pdvi2 = (DLLVERSIONINFO2 *)pdvi;
338 
339             pdvi2->dwFlags = 0;
340             pdvi2->ullVersion = MAKEDLLVERULL(WINE_FILEVERSION_MAJOR,
341                                               WINE_FILEVERSION_MINOR,
342                                               WINE_FILEVERSION_BUILD,
343                                               WINE_FILEVERSION_PLATFORMID);
344         }
345         TRACE("%u.%u.%u.%u\n",
346               pdvi->dwMajorVersion, pdvi->dwMinorVersion,
347               pdvi->dwBuildNumber, pdvi->dwPlatformID);
348         return S_OK;
349     }
350     else
351     {
352         WARN("wrong DLLVERSIONINFO size from app\n");
353         return E_INVALIDARG;
354     }
355 }
356 
357 /*************************************************************************
358  * global variables of the shell32.dll
359  * all are once per process
360  *
361  */
362 HINSTANCE   shell32_hInstance;
363 
364 /*************************************************************************
365  * SHELL32 DllMain
366  *
367  * NOTES
368  *  calling oleinitialize here breaks sone apps.
369  */
370 STDAPI_(BOOL) DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID fImpLoad)
371 {
372     TRACE("%p 0x%x %p\n", hInstance, dwReason, fImpLoad);
373     if (dwReason == DLL_PROCESS_ATTACH)
374     {
375         shell32_hInstance = hInstance;
376         gModule.Init(ObjectMap, hInstance, &LIBID_Shell32);
377 
378         DisableThreadLibraryCalls (hInstance);
379 
380         /* get full path to this DLL for IExtractIconW_fnGetIconLocation() */
381         GetModuleFileNameW(hInstance, swShell32Name, MAX_PATH);
382         swShell32Name[MAX_PATH - 1] = '\0';
383 
384         /* Initialize comctl32 */
385         INITCOMMONCONTROLSEX InitCtrls;
386         InitCtrls.dwSize = sizeof(INITCOMMONCONTROLSEX);
387         InitCtrls.dwICC = ICC_WIN95_CLASSES | ICC_DATE_CLASSES | ICC_USEREX_CLASSES;
388         InitCommonControlsEx(&InitCtrls);
389 
390         /* Bad idea, initialization in DllMain! */
391         InitChangeNotifications();
392     }
393     else if (dwReason == DLL_PROCESS_DETACH)
394     {
395         shell32_hInstance = NULL;
396         SIC_Destroy();
397         FreeChangeNotifications();
398         gModule.Term();
399     }
400     return TRUE;
401 }
402 
403 /***********************************************************************
404  *              DllCanUnloadNow (SHELL32.@)
405  */
406 STDAPI DllCanUnloadNow()
407 {
408     return gModule.DllCanUnloadNow();
409 }
410 
411 /*************************************************************************
412  *              DllGetClassObject     [SHELL32.@]
413  *              SHDllGetClassObject   [SHELL32.128]
414  */
415 STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
416 {
417     HRESULT                                hResult;
418 
419     TRACE("CLSID:%s,IID:%s\n", shdebugstr_guid(&rclsid), shdebugstr_guid(&riid));
420 
421     hResult = gModule.DllGetClassObject(rclsid, riid, ppv);
422     TRACE("-- pointer to class factory: %p\n", *ppv);
423     return hResult;
424 }
425 
426 /***********************************************************************
427  *              DllRegisterServer (SHELL32.@)
428  */
429 STDAPI DllRegisterServer()
430 {
431     HRESULT hr;
432 
433     hr = gModule.DllRegisterServer(TRUE);
434     if (FAILED(hr))
435         return hr;
436 
437     hr = gModule.UpdateRegistryFromResource(IDR_FOLDEROPTIONS, TRUE, NULL);
438     if (FAILED(hr))
439         return hr;
440 
441     hr = SHELL_RegisterShellFolders();
442     if (FAILED(hr))
443         return hr;
444 
445     return S_OK;
446 }
447 
448 /***********************************************************************
449  *              DllUnregisterServer (SHELL32.@)
450  */
451 STDAPI DllUnregisterServer()
452 {
453     HRESULT hr;
454 
455     hr = gModule.DllUnregisterServer(TRUE);
456     if (FAILED(hr))
457         return hr;
458 
459     hr = gModule.UpdateRegistryFromResource(IDR_FOLDEROPTIONS, FALSE, NULL);
460     if (FAILED(hr))
461         return hr;
462 
463     return S_OK;
464 }
465 
466 /*************************************************************************
467  * DllInstall         [SHELL32.@]
468  *
469  * PARAMETERS
470  *
471  *    BOOL bInstall - TRUE for install, FALSE for uninstall
472  *    LPCWSTR pszCmdLine - command line (unused by shell32?)
473  */
474 
475 HRESULT WINAPI DllInstall(BOOL bInstall, LPCWSTR cmdline)
476 {
477     FIXME("%s %s: stub\n", bInstall ? "TRUE":"FALSE", debugstr_w(cmdline));
478     return S_OK;        /* indicate success */
479 }
480