[RSHELL] -Link to shellbars and export CBandSite_CreateInstance and CBandSiteMenu_Cre...
[reactos.git] / reactos / base / shell / rshell / misc.cpp
1 /*
2 * ReactOS Explorer
3 *
4 * Copyright 2014 Giannis Adamopoulos
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 Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21
22 #include <stdio.h>
23 #include <tchar.h>
24
25 #define WIN32_NO_STATUS
26 #define _INC_WINDOWS
27 #define COM_NO_WINDOWS_H
28
29 #define COBJMACROS
30
31 #include <windef.h>
32 #include <winbase.h>
33 #include <winreg.h>
34 #include <wingdi.h>
35 #include <winnls.h>
36 #include <wincon.h>
37 #include <shellapi.h>
38 #include <shlobj.h>
39 #include <shlobj_undoc.h>
40 #include <shlwapi.h>
41 #include <shlguid_undoc.h>
42 #include <uxtheme.h>
43 #include <strsafe.h>
44
45 #include <atlbase.h>
46 #include <atlcom.h>
47 #include <atlwin.h>
48 #include <undocshell.h>
49 #include <undocuser.h>
50
51 #include <shellutils.h>
52
53 #include <wine/debug.h>
54
55 extern "C"
56 {
57 extern HINSTANCE shell32_hInstance;
58
59 HRESULT WINAPI CStartMenu_Constructor(REFIID riid, void **ppv);
60 HRESULT WINAPI CMenuDeskBar_Constructor(REFIID riid, LPVOID *ppv);
61 HRESULT WINAPI CMenuSite_Constructor(REFIID riid, LPVOID *ppv);
62 HRESULT WINAPI CMenuBand_Constructor(REFIID riid, LPVOID *ppv);
63 HRESULT WINAPI CMergedFolder_Constructor(REFIID riid, LPVOID *ppv);
64 HRESULT WINAPI CBandSite_CreateInstance(REFIID riid, void **ppv);
65 HRESULT WINAPI CBandSiteMenu_CreateInstance(REFIID riid, void **ppv);
66 }
67
68 DWORD WINAPI WinList_Init(void)
69 {
70 /* do something here (perhaps we may want to add our own implementation fo win8) */
71 return 0;
72 }
73
74 void *operator new (size_t, void *buf)
75 {
76 return buf;
77 }
78
79 class CRShellModule : public CComModule
80 {
81 public:
82 };
83
84 CRShellModule gModule;
85 CAtlWinModule gWinModule;
86
87 HINSTANCE shell32_hInstance = NULL;
88
89 static LSTATUS inline _RegSetStringValueW(HKEY hKey, LPCWSTR lpValueName, LPCWSTR lpStringData)
90 {
91 DWORD dwStringDataLen = lstrlenW(lpStringData);
92
93 return RegSetValueExW(hKey, lpValueName, 0, REG_SZ, (BYTE *) lpStringData, 2 * (dwStringDataLen + 1));
94 }
95
96 static HRESULT RegisterComponent(REFGUID clsid, LPCWSTR szDisplayName)
97 {
98 WCHAR szFilename[MAX_PATH];
99 WCHAR szClsid[MAX_PATH];
100 WCHAR szRoot[MAX_PATH];
101
102 if (!StringFromGUID2(clsid, szClsid, _countof(szClsid)))
103 return E_FAIL;
104
105 if (!GetModuleFileNameW(shell32_hInstance, szFilename, _countof(szFilename)))
106 return E_FAIL;
107
108 HRESULT hr = StringCchPrintfW(szRoot, 0x104u, L"CLSID\\%s", szClsid);
109 if (FAILED(hr))
110 return hr;
111
112 DWORD dwDisposition;
113 HKEY hkRoot;
114 if (RegCreateKeyExW(HKEY_CLASSES_ROOT, szRoot, 0, NULL, 0, KEY_WRITE, 0, &hkRoot, &dwDisposition) != 0)
115 return E_FAIL;
116
117 HKEY hkServer;
118
119 _RegSetStringValueW(hkRoot, NULL, szDisplayName);
120
121 if (RegCreateKeyExW(hkRoot, L"InprocServer32", 0, NULL, 0, KEY_SET_VALUE, 0, &hkServer, &dwDisposition) != 0)
122 {
123 RegCloseKey(hkRoot);
124 return E_FAIL;
125 }
126
127 _RegSetStringValueW(hkServer, NULL, szFilename);
128 _RegSetStringValueW(hkServer, L"ThreadingModel", L"Both");
129
130 RegCloseKey(hkServer);
131 RegCloseKey(hkRoot);
132 return S_OK;
133 }
134
135 static HRESULT UnregisterComponent(REFGUID clsid)
136 {
137 WCHAR szClsid[MAX_PATH];
138 WCHAR szRoot[MAX_PATH];
139 HKEY hkRoot;
140
141 if (!StringFromGUID2(clsid, szClsid, _countof(szClsid)))
142 return E_FAIL;
143
144 HRESULT hr = StringCchPrintfW(szRoot, 0x104u, L"CLSID\\%s", szClsid);
145 if (FAILED(hr))
146 return hr;
147
148 if (RegOpenKeyExW(HKEY_CLASSES_ROOT, szRoot, 0, KEY_WRITE, &hkRoot) != 0)
149 return E_FAIL;
150
151 RegDeleteKeyW(hkRoot, L"InprocServer32");
152 RegCloseKey(hkRoot);
153
154 RegDeleteKeyW(HKEY_CLASSES_ROOT, szRoot);
155
156 return S_OK;
157 }
158
159 STDAPI_(BOOL) DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID fImpLoad)
160 {
161 if (dwReason == DLL_PROCESS_ATTACH)
162 {
163 shell32_hInstance = hInstance;
164
165 gModule.Init(NULL, hInstance, NULL);
166 DisableThreadLibraryCalls(hInstance);
167 }
168 else if (dwReason == DLL_PROCESS_DETACH)
169 {
170 shell32_hInstance = NULL;
171 gModule.Term();
172 }
173 return TRUE;
174 }
175
176 HRESULT
177 WINAPI
178 DllCanUnloadNow(void)
179 {
180 gModule.DllCanUnloadNow();
181 return S_FALSE;
182 }
183
184 STDAPI
185 DllRegisterServer(void)
186 {
187 RegisterComponent(CLSID_StartMenu, L"Shell Start Menu");
188 RegisterComponent(CLSID_MenuDeskBar, L"Shell Menu Desk Bar");
189 RegisterComponent(CLSID_MenuBand, L"Shell Menu Band");
190 RegisterComponent(CLSID_MenuBandSite, L"Shell Menu Band Site");
191 RegisterComponent(CLSID_MergedFolder, L"Merged Shell Folder");
192 RegisterComponent(CLSID_RebarBandSite, L"Shell Rebar Band Site");
193 return S_OK;
194 }
195
196 STDAPI
197 DllUnregisterServer(void)
198 {
199 UnregisterComponent(CLSID_StartMenu);
200 UnregisterComponent(CLSID_MenuDeskBar);
201 UnregisterComponent(CLSID_MenuBand);
202 UnregisterComponent(CLSID_MenuBandSite);
203 UnregisterComponent(CLSID_MergedFolder);
204 UnregisterComponent(CLSID_RebarBandSite);
205 return S_OK;
206 }
207
208 class CRShellClassFactory :
209 public CComObjectRootEx<CComMultiThreadModelNoCS>,
210 public IClassFactory
211 {
212 private:
213 CLSID m_Clsid;
214
215 public:
216 CRShellClassFactory() {}
217 virtual ~CRShellClassFactory() {}
218
219 HRESULT Initialize(REFGUID clsid)
220 {
221 m_Clsid = clsid;
222 return S_OK;
223 }
224
225 /* IClassFactory */
226 virtual HRESULT WINAPI CreateInstance(LPUNKNOWN pUnkOuter, REFIID riid, LPVOID *ppvObject)
227 {
228 *ppvObject = NULL;
229
230 if (IsEqualCLSID(m_Clsid, CLSID_StartMenu))
231 return CStartMenu_Constructor(riid, ppvObject);
232
233 if (IsEqualCLSID(m_Clsid, CLSID_MenuDeskBar))
234 return CMenuDeskBar_Constructor(riid, ppvObject);
235
236 if (IsEqualCLSID(m_Clsid, CLSID_MenuBand))
237 return CMenuBand_Constructor(riid, ppvObject);
238
239 if (IsEqualCLSID(m_Clsid, CLSID_MenuBandSite))
240 return CMenuSite_Constructor(riid, ppvObject);
241
242 if (IsEqualCLSID(m_Clsid, CLSID_MergedFolder))
243 return CMergedFolder_Constructor(riid, ppvObject);
244
245 return E_NOINTERFACE;
246 }
247
248 virtual HRESULT WINAPI LockServer(BOOL fLock)
249 {
250 return E_NOTIMPL;
251 }
252
253 BEGIN_COM_MAP(CRShellClassFactory)
254 COM_INTERFACE_ENTRY_IID(IID_IClassFactory, IClassFactory)
255 END_COM_MAP()
256 };
257
258 STDAPI
259 DllGetClassObject(
260 REFCLSID rclsid,
261 REFIID riid,
262 LPVOID *ppv)
263 {
264 if (!ppv)
265 return E_INVALIDARG;
266
267 *ppv = NULL;
268
269 return ShellObjectCreatorInit<CRShellClassFactory>(rclsid, riid, ppv);
270 }