cd351a9a52746887e46b2605fddfb0e4e43d00e6
[reactos.git] / reactos / lib / 3rdparty / strmbase / dllfunc.c
1 /*
2 * Strmbase DLL functions
3 *
4 * Copyright (C) 2005 Rolf Kalbermatter
5 * Copyright (C) 2010 Aric Stewart, CodeWeavers
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 #include "config.h"
22
23 #include <stdarg.h>
24 #include <assert.h>
25
26 #define COBJMACROS
27 #define NONAMELESSSTRUCT
28 #define NONAMELESSUNION
29
30 #include "windef.h"
31 #include "winbase.h"
32 #include "wingdi.h"
33 #include "winerror.h"
34 #include "winreg.h"
35 #include "objbase.h"
36 #include "uuids.h"
37 #include "strmif.h"
38
39 #include "wine/unicode.h"
40 #include "wine/debug.h"
41 #include "wine/strmbase.h"
42
43 WINE_DEFAULT_DEBUG_CHANNEL(strmbase);
44
45 extern const int g_cTemplates;
46 extern const FactoryTemplate g_Templates[];
47
48 static HINSTANCE g_hInst = NULL;
49 static LONG server_locks = 0;
50
51 /*
52 * defines and constants
53 */
54 #define MAX_KEY_LEN 260
55
56 static const WCHAR clsid_keyname[] = {'C','L','S','I','D',0 };
57 static const WCHAR ips32_keyname[] = {'I','n','P','r','o','c','S','e','r','v','e','r','3','2',0};
58 static const WCHAR tmodel_keyname[] = {'T','h','r','e','a','d','i','n','g','M','o','d','e','l',0};
59 static const WCHAR tmodel_both[] = {'B','o','t','h',0};
60
61 #ifdef __REACTOS__
62 static LSTATUS WINAPI RegDeleteTreeW(HKEY hKey, LPCWSTR lpszSubKey)
63 {
64 LONG ret;
65 DWORD dwMaxSubkeyLen, dwMaxValueLen;
66 DWORD dwMaxLen, dwSize;
67 WCHAR szNameBuf[MAX_PATH], *lpszName = szNameBuf;
68 HKEY hSubKey = hKey;
69
70 TRACE("(hkey=%p,%p %s)\n", hKey, lpszSubKey, debugstr_w(lpszSubKey));
71
72 if(lpszSubKey)
73 {
74 ret = RegOpenKeyExW(hKey, lpszSubKey, 0, KEY_READ, &hSubKey);
75 if (ret) return ret;
76 }
77
78 /* Get highest length for keys, values */
79 ret = RegQueryInfoKeyW(hSubKey, NULL, NULL, NULL, NULL,
80 &dwMaxSubkeyLen, NULL, NULL, &dwMaxValueLen, NULL, NULL, NULL);
81 if (ret) goto cleanup;
82
83 dwMaxSubkeyLen++;
84 dwMaxValueLen++;
85 dwMaxLen = max(dwMaxSubkeyLen, dwMaxValueLen);
86 if (dwMaxLen > sizeof(szNameBuf)/sizeof(WCHAR))
87 {
88 /* Name too big: alloc a buffer for it */
89 if (!(lpszName = HeapAlloc( GetProcessHeap(), 0, dwMaxLen*sizeof(WCHAR))))
90 {
91 ret = ERROR_NOT_ENOUGH_MEMORY;
92 goto cleanup;
93 }
94 }
95
96
97 /* Recursively delete all the subkeys */
98 while (TRUE)
99 {
100 dwSize = dwMaxLen;
101 if (RegEnumKeyExW(hSubKey, 0, lpszName, &dwSize, NULL,
102 NULL, NULL, NULL)) break;
103
104 ret = RegDeleteTreeW(hSubKey, lpszName);
105 if (ret) goto cleanup;
106 }
107
108 if (lpszSubKey)
109 ret = RegDeleteKeyW(hKey, lpszSubKey);
110 else
111 while (TRUE)
112 {
113 dwSize = dwMaxLen;
114 if (RegEnumValueW(hKey, 0, lpszName, &dwSize,
115 NULL, NULL, NULL, NULL)) break;
116
117 ret = RegDeleteValueW(hKey, lpszName);
118 if (ret) goto cleanup;
119 }
120
121 cleanup:
122 /* Free buffer if allocated */
123 if (lpszName != szNameBuf)
124 HeapFree( GetProcessHeap(), 0, lpszName);
125 if(lpszSubKey)
126 RegCloseKey(hSubKey);
127 return ret;
128 }
129 #endif
130
131 /*
132 * SetupRegisterClass()
133 */
134 static HRESULT SetupRegisterClass(HKEY clsid, LPCWSTR szCLSID,
135 LPCWSTR szDescription,
136 LPCWSTR szFileName,
137 LPCWSTR szServerType,
138 LPCWSTR szThreadingModel)
139 {
140 HKEY hkey, hsubkey = NULL;
141 LONG ret = RegCreateKeyW(clsid, szCLSID, &hkey);
142 if (ERROR_SUCCESS != ret)
143 return HRESULT_FROM_WIN32(ret);
144
145 /* set description string */
146 ret = RegSetValueW(hkey, NULL, REG_SZ, szDescription,
147 sizeof(WCHAR) * (lstrlenW(szDescription) + 1));
148 if (ERROR_SUCCESS != ret)
149 goto err_out;
150
151 /* create CLSID\\{"CLSID"}\\"ServerType" key, using key to CLSID\\{"CLSID"}
152 passed back by last call to RegCreateKeyW(). */
153 ret = RegCreateKeyW(hkey, szServerType, &hsubkey);
154 if (ERROR_SUCCESS != ret)
155 goto err_out;
156
157 /* set server path */
158 ret = RegSetValueW(hsubkey, NULL, REG_SZ, szFileName,
159 sizeof(WCHAR) * (lstrlenW(szFileName) + 1));
160 if (ERROR_SUCCESS != ret)
161 goto err_out;
162
163 /* set threading model */
164 ret = RegSetValueExW(hsubkey, tmodel_keyname, 0L, REG_SZ,
165 (const BYTE*)szThreadingModel,
166 sizeof(WCHAR) * (lstrlenW(szThreadingModel) + 1));
167 err_out:
168 if (hsubkey)
169 RegCloseKey(hsubkey);
170 RegCloseKey(hkey);
171 return HRESULT_FROM_WIN32(ret);
172 }
173
174 /*
175 * RegisterAllClasses()
176 */
177 static HRESULT SetupRegisterAllClasses(const FactoryTemplate * pList, int num,
178 LPCWSTR szFileName, BOOL bRegister)
179 {
180 HRESULT hr = NOERROR;
181 HKEY hkey;
182 OLECHAR szCLSID[CHARS_IN_GUID];
183 LONG i, ret = RegCreateKeyW(HKEY_CLASSES_ROOT, clsid_keyname, &hkey);
184 if (ERROR_SUCCESS != ret)
185 return HRESULT_FROM_WIN32(ret);
186
187 for (i = 0; i < num; i++, pList++)
188 {
189 /* (un)register CLSID and InprocServer32 */
190 hr = StringFromGUID2(pList->m_ClsID, szCLSID, CHARS_IN_GUID);
191 if (SUCCEEDED(hr))
192 {
193 if (bRegister )
194 hr = SetupRegisterClass(hkey, szCLSID,
195 pList->m_Name, szFileName,
196 ips32_keyname, tmodel_both);
197 else
198 hr = RegDeleteTreeW(hkey, szCLSID);
199 }
200 }
201 RegCloseKey(hkey);
202 return hr;
203 }
204
205 HRESULT WINAPI AMovieSetupRegisterFilter2(const AMOVIESETUP_FILTER *pFilter, IFilterMapper2 *pIFM2, BOOL bRegister)
206 {
207 if (!pFilter)
208 return S_OK;
209
210 if (bRegister)
211 {
212 {
213 REGFILTER2 rf2;
214 rf2.dwVersion = 1;
215 rf2.dwMerit = pFilter->merit;
216 rf2.u.s1.cPins = pFilter->pins;
217 rf2.u.s1.rgPins = pFilter->pPin;
218
219 return IFilterMapper2_RegisterFilter(pIFM2, pFilter->clsid, pFilter->name, NULL, &CLSID_LegacyAmFilterCategory, NULL, &rf2);
220 }
221 }
222 else
223 return IFilterMapper2_UnregisterFilter(pIFM2, &CLSID_LegacyAmFilterCategory, NULL, pFilter->clsid);
224 }
225
226 HRESULT WINAPI AMovieDllRegisterServer2(BOOL bRegister)
227 {
228 HRESULT hr;
229 int i;
230 IFilterMapper2 *pIFM2 = NULL;
231 WCHAR szFileName[MAX_PATH];
232
233 if (!GetModuleFileNameW(g_hInst, szFileName, MAX_PATH))
234 {
235 ERR("Failed to get module file name for registration\n");
236 return E_FAIL;
237 }
238
239 if (bRegister)
240 hr = SetupRegisterAllClasses(g_Templates, g_cTemplates, szFileName, TRUE );
241
242 hr = CoInitialize(NULL);
243
244 TRACE("Getting IFilterMapper2\r\n");
245 hr = CoCreateInstance(&CLSID_FilterMapper2, NULL, CLSCTX_INPROC_SERVER,
246 &IID_IFilterMapper2, (void **)&pIFM2);
247
248 for (i = 0; SUCCEEDED(hr) && i < g_cTemplates; i++)
249 hr = AMovieSetupRegisterFilter2(g_Templates[i].m_pAMovieSetup_Filter, pIFM2, bRegister);
250
251 /* release interface */
252 if (pIFM2)
253 IFilterMapper2_Release(pIFM2);
254
255 /* and clear up */
256 CoFreeUnusedLibraries();
257 CoUninitialize();
258
259 /* if unregistering, unregister all OLE servers */
260 if (SUCCEEDED(hr) && !bRegister)
261 hr = SetupRegisterAllClasses(g_Templates, g_cTemplates, szFileName, FALSE);
262
263 return hr;
264 }
265
266 /****************************************************************************
267 * SetupInitializeServers
268 *
269 * This function is table driven using the static members of the
270 * CFactoryTemplate class defined in the Dll.
271 *
272 * It calls the initialize function for any class in CFactoryTemplate with
273 * one defined.
274 *
275 ****************************************************************************/
276 static void SetupInitializeServers(const FactoryTemplate * pList, int num,
277 BOOL bLoading)
278 {
279 int i;
280
281 for (i = 0; i < num; i++, pList++)
282 {
283 if (pList->m_lpfnInit)
284 pList->m_lpfnInit(bLoading, pList->m_ClsID);
285 }
286 }
287
288 BOOL WINAPI STRMBASE_DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpv)
289 {
290 switch (fdwReason)
291 {
292 case DLL_PROCESS_ATTACH:
293 g_hInst = hInstDLL;
294 DisableThreadLibraryCalls(hInstDLL);
295 SetupInitializeServers(g_Templates, g_cTemplates, TRUE);
296 break;
297 case DLL_PROCESS_DETACH:
298 SetupInitializeServers(g_Templates, g_cTemplates, FALSE);
299 break;
300 }
301 return TRUE;
302 }
303
304 /******************************************************************************
305 * DLL ClassFactory
306 */
307 typedef struct {
308 IClassFactory IClassFactory_iface;
309 LONG ref;
310 LPFNNewCOMObject pfnCreateInstance;
311 } IClassFactoryImpl;
312
313 static inline IClassFactoryImpl *impl_from_IClassFactory(IClassFactory *iface)
314 {
315 return CONTAINING_RECORD(iface, IClassFactoryImpl, IClassFactory_iface);
316 }
317
318 static HRESULT WINAPI DSCF_QueryInterface(IClassFactory *iface, REFIID riid, void **ppobj)
319 {
320 if (IsEqualGUID(riid, &IID_IUnknown) ||
321 IsEqualGUID(riid, &IID_IClassFactory))
322 {
323 IClassFactory_AddRef(iface);
324 *ppobj = iface;
325 return S_OK;
326 }
327
328 *ppobj = NULL;
329 WARN("(%p)->(%s,%p), not found\n", iface, debugstr_guid(riid), ppobj);
330 return E_NOINTERFACE;
331 }
332
333 static ULONG WINAPI DSCF_AddRef(IClassFactory *iface)
334 {
335 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
336 return InterlockedIncrement(&This->ref);
337 }
338
339 static ULONG WINAPI DSCF_Release(IClassFactory *iface)
340 {
341 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
342 ULONG ref = InterlockedDecrement(&This->ref);
343
344 if (ref == 0)
345 HeapFree(GetProcessHeap(), 0, This);
346
347 return ref;
348 }
349
350 static HRESULT WINAPI DSCF_CreateInstance(IClassFactory *iface, IUnknown *pOuter,
351 REFIID riid, void **ppobj)
352 {
353 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
354 HRESULT hres = ERROR_SUCCESS;
355 LPUNKNOWN punk;
356
357 TRACE("(%p)->(%p,%s,%p)\n", This, pOuter, debugstr_guid(riid), ppobj);
358
359 if (!ppobj)
360 return E_POINTER;
361
362 /* Enforce the normal OLE rules regarding interfaces and delegation */
363 if (pOuter && !IsEqualGUID(riid, &IID_IUnknown))
364 return E_NOINTERFACE;
365
366 *ppobj = NULL;
367 punk = This->pfnCreateInstance(pOuter, &hres);
368 if (!punk)
369 {
370 /* No object created, update error if it isn't done already and return */
371 if (SUCCEEDED(hres))
372 hres = E_OUTOFMEMORY;
373 return hres;
374 }
375
376 if (SUCCEEDED(hres))
377 {
378 hres = IUnknown_QueryInterface(punk, riid, ppobj);
379 }
380 /* Releasing the object. If everything was successful, QueryInterface
381 should have incremented the refcount once more, otherwise this will
382 purge the object. */
383 IUnknown_Release(punk);
384 return hres;
385 }
386
387 static HRESULT WINAPI DSCF_LockServer(IClassFactory *iface, BOOL dolock)
388 {
389 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
390 TRACE("(%p)->(%d)\n",This, dolock);
391
392 if (dolock)
393 InterlockedIncrement(&server_locks);
394 else
395 InterlockedDecrement(&server_locks);
396 return S_OK;
397 }
398
399 static const IClassFactoryVtbl DSCF_Vtbl =
400 {
401 DSCF_QueryInterface,
402 DSCF_AddRef,
403 DSCF_Release,
404 DSCF_CreateInstance,
405 DSCF_LockServer
406 };
407
408 /***********************************************************************
409 * DllGetClassObject
410 */
411 HRESULT WINAPI STRMBASE_DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
412 {
413 const FactoryTemplate *pList = g_Templates;
414 IClassFactoryImpl *factory;
415 int i;
416
417 TRACE("(%s,%s,%p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
418
419 if (!ppv)
420 return E_POINTER;
421
422 *ppv = NULL;
423
424 if (!IsEqualGUID(&IID_IClassFactory, riid) &&
425 !IsEqualGUID(&IID_IUnknown, riid))
426 return E_NOINTERFACE;
427
428 for (i = 0; i < g_cTemplates; i++, pList++)
429 {
430 if (IsEqualGUID(pList->m_ClsID, rclsid))
431 break;
432 }
433
434 if (i == g_cTemplates)
435 {
436 char dllname[MAX_PATH];
437 if (!GetModuleFileNameA(g_hInst, dllname, sizeof(dllname)))
438 strcpy(dllname, "???");
439 ERR("%s: no class found in %s.\n", debugstr_guid(rclsid), dllname);
440 return CLASS_E_CLASSNOTAVAILABLE;
441 }
442 else if (!pList->m_lpfnNew)
443 {
444 FIXME("%s: class not implemented yet.\n", debugstr_guid(rclsid));
445 return CLASS_E_CLASSNOTAVAILABLE;
446 }
447
448 factory = HeapAlloc(GetProcessHeap(), 0, sizeof(IClassFactoryImpl));
449 if (!factory)
450 return E_OUTOFMEMORY;
451
452 factory->IClassFactory_iface.lpVtbl = &DSCF_Vtbl;
453 factory->ref = 1;
454
455 factory->pfnCreateInstance = pList->m_lpfnNew;
456
457 *ppv = &factory->IClassFactory_iface;
458 return S_OK;
459 }
460
461 /***********************************************************************
462 * DllCanUnloadNow
463 */
464 HRESULT WINAPI STRMBASE_DllCanUnloadNow(void)
465 {
466 TRACE("\n");
467
468 if (server_locks == 0)
469 return S_OK;
470 return S_FALSE;
471 }