e466b54595e0dc652576fcd6d59800d7cf6a73b7
[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 #include "shellmenu.h"
22
23 DWORD WINAPI WinList_Init(void)
24 {
25 /* do something here (perhaps we may want to add our own implementation fo win8) */
26 return 0;
27 }
28
29 void *operator new (size_t, void *buf)
30 {
31 return buf;
32 }
33
34 class CRShellModule : public CComModule
35 {
36 public:
37 };
38
39 CRShellModule gModule;
40 CAtlWinModule gWinModule;
41
42 HINSTANCE shell32_hInstance = NULL;
43
44 static LSTATUS inline _RegSetStringValueW(HKEY hKey, LPCWSTR lpValueName, LPCWSTR lpStringData)
45 {
46 DWORD dwStringDataLen = lstrlenW(lpStringData);
47
48 return RegSetValueExW(hKey, lpValueName, 0, REG_SZ, (BYTE *) lpStringData, 2 * (dwStringDataLen + 1));
49 }
50
51 static HRESULT RegisterComponent(REFGUID clsid, LPCWSTR szDisplayName)
52 {
53 WCHAR szFilename[MAX_PATH];
54 WCHAR szClsid[MAX_PATH];
55 WCHAR szRoot[MAX_PATH];
56
57 if (!StringFromGUID2(clsid, szClsid, _countof(szClsid)))
58 return E_FAIL;
59
60 if (!GetModuleFileNameW(shell32_hInstance, szFilename, _countof(szFilename)))
61 return E_FAIL;
62
63 HRESULT hr = StringCchPrintfW(szRoot, 0x104u, L"CLSID\\%s", szClsid);
64 if (FAILED(hr))
65 return hr;
66
67 DWORD dwDisposition;
68 HKEY hkRoot;
69 if (RegCreateKeyExW(HKEY_CLASSES_ROOT, szRoot, 0, NULL, 0, KEY_WRITE, 0, &hkRoot, &dwDisposition) != 0)
70 return E_FAIL;
71
72 HKEY hkServer;
73
74 _RegSetStringValueW(hkRoot, NULL, szDisplayName);
75
76 if (RegCreateKeyExW(hkRoot, L"InprocServer32", 0, NULL, 0, KEY_SET_VALUE, 0, &hkServer, &dwDisposition) != 0)
77 {
78 RegCloseKey(hkRoot);
79 return E_FAIL;
80 }
81
82 _RegSetStringValueW(hkServer, NULL, szFilename);
83 _RegSetStringValueW(hkServer, L"ThreadingModel", L"Both");
84
85 RegCloseKey(hkServer);
86 RegCloseKey(hkRoot);
87 return S_OK;
88 }
89
90 static HRESULT UnregisterComponent(REFGUID clsid)
91 {
92 WCHAR szClsid[MAX_PATH];
93 WCHAR szRoot[MAX_PATH];
94 HKEY hkRoot;
95
96 if (!StringFromGUID2(clsid, szClsid, _countof(szClsid)))
97 return E_FAIL;
98
99 HRESULT hr = StringCchPrintfW(szRoot, 0x104u, L"CLSID\\%s", szClsid);
100 if (FAILED(hr))
101 return hr;
102
103 if (RegOpenKeyExW(HKEY_CLASSES_ROOT, szRoot, 0, KEY_WRITE, &hkRoot) != 0)
104 return E_FAIL;
105
106 RegDeleteKeyW(hkRoot, L"InprocServer32");
107 RegCloseKey(hkRoot);
108
109 RegDeleteKeyW(HKEY_CLASSES_ROOT, szRoot);
110
111 return S_OK;
112 }
113
114 STDAPI_(BOOL) DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID fImpLoad)
115 {
116 if (dwReason == DLL_PROCESS_ATTACH)
117 {
118 shell32_hInstance = hInstance;
119
120 gModule.Init(NULL, hInstance, NULL);
121 DisableThreadLibraryCalls(hInstance);
122 }
123 else if (dwReason == DLL_PROCESS_DETACH)
124 {
125 shell32_hInstance = NULL;
126 gModule.Term();
127 }
128 return TRUE;
129 }
130
131 HRESULT
132 WINAPI
133 DllCanUnloadNow(void)
134 {
135 gModule.DllCanUnloadNow();
136 return S_FALSE;
137 }
138
139 STDAPI
140 DllRegisterServer(void)
141 {
142 RegisterComponent(CLSID_StartMenu, L"Shell Start Menu");
143 RegisterComponent(CLSID_MenuDeskBar, L"Shell Menu Desk Bar");
144 RegisterComponent(CLSID_MenuBand, L"Shell Menu Band");
145 RegisterComponent(CLSID_MenuBandSite, L"Shell Menu Band Site");
146 RegisterComponent(CLSID_MergedFolder, L"Merged Shell Folder");
147 RegisterComponent(CLSID_RebarBandSite, L"Shell Rebar Band Site");
148 return S_OK;
149 }
150
151 STDAPI
152 DllUnregisterServer(void)
153 {
154 UnregisterComponent(CLSID_StartMenu);
155 UnregisterComponent(CLSID_MenuDeskBar);
156 UnregisterComponent(CLSID_MenuBand);
157 UnregisterComponent(CLSID_MenuBandSite);
158 UnregisterComponent(CLSID_MergedFolder);
159 UnregisterComponent(CLSID_RebarBandSite);
160 return S_OK;
161 }
162
163 class CRShellClassFactory :
164 public CComObjectRootEx<CComMultiThreadModelNoCS>,
165 public IClassFactory
166 {
167 private:
168 CLSID m_Clsid;
169
170 public:
171 CRShellClassFactory() {}
172 virtual ~CRShellClassFactory() {}
173
174 HRESULT Initialize(REFGUID clsid)
175 {
176 m_Clsid = clsid;
177 return S_OK;
178 }
179
180 /* IClassFactory */
181 virtual HRESULT WINAPI CreateInstance(LPUNKNOWN pUnkOuter, REFIID riid, LPVOID *ppvObject)
182 {
183 *ppvObject = NULL;
184
185 if (IsEqualCLSID(m_Clsid, CLSID_StartMenu))
186 return CStartMenu_Constructor(riid, ppvObject);
187
188 if (IsEqualCLSID(m_Clsid, CLSID_MenuDeskBar))
189 return CMenuDeskBar_Constructor(riid, ppvObject);
190
191 if (IsEqualCLSID(m_Clsid, CLSID_MenuBand))
192 return CMenuBand_Constructor(riid, ppvObject);
193
194 if (IsEqualCLSID(m_Clsid, CLSID_MenuBandSite))
195 return CMenuSite_Constructor(riid, ppvObject);
196
197 if (IsEqualCLSID(m_Clsid, CLSID_MergedFolder))
198 return CMergedFolder_Constructor(riid, ppvObject);
199
200 if (IsEqualCLSID(m_Clsid, CLSID_RebarBandSite))
201 return CBandSite_Constructor(riid, ppvObject);
202
203 return E_NOINTERFACE;
204 }
205
206 virtual HRESULT WINAPI LockServer(BOOL fLock)
207 {
208 return E_NOTIMPL;
209 }
210
211 BEGIN_COM_MAP(CRShellClassFactory)
212 COM_INTERFACE_ENTRY_IID(IID_IClassFactory, IClassFactory)
213 END_COM_MAP()
214 };
215
216 STDAPI
217 DllGetClassObject(
218 REFCLSID rclsid,
219 REFIID riid,
220 LPVOID *ppv)
221 {
222 if (!ppv)
223 return E_INVALIDARG;
224
225 *ppv = NULL;
226
227 return ShellObjectCreatorInit<CRShellClassFactory>(rclsid, riid, ppv);
228 }