Release 1.3.7.
[wine/gsoc-2012-control.git] / dlls / strmbase / dllfunc.c
blobadb5655c29d18a859b2a7e8268c632563b45b705
1 /*
2 * Strmbase DLL functions
4 * Copyright (C) 2005 Rolf Kalbermatter
5 * Copyright (C) 2010 Aric Stewart, CodeWeavers
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.
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.
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
21 #include "config.h"
23 #include <stdarg.h>
24 #include <assert.h>
26 #define COBJMACROS
27 #define NONAMELESSSTRUCT
28 #define NONAMELESSUNION
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"
39 #include "wine/unicode.h"
40 #include "wine/debug.h"
41 #include "wine/strmbase.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(strmbase);
45 extern const int g_cTemplates;
46 extern const FactoryTemplate g_Templates[];
48 static HINSTANCE g_hInst = NULL;
49 static LONG server_locks = 0;
52 * defines and constants
54 #define MAX_KEY_LEN 260
56 static WCHAR const clsid_keyname[6] =
57 {'C','L','S','I','D',0 };
58 static WCHAR const ips32_keyname[15] =
59 {'I','n','P','r','o','c','S','e','r','v','e','r','3','2',0};
60 static WCHAR const tmodel_keyname[15] =
61 {'T','h','r','e','a','d','i','n','g','M','o','d','e','l',0};
62 static WCHAR const tmodel_both[] =
63 {'B','o','t','h',0};
66 * SetupRegisterClass()
68 static HRESULT SetupRegisterClass(HKEY clsid, LPCWSTR szCLSID,
69 LPCWSTR szDescription,
70 LPCWSTR szFileName,
71 LPCWSTR szServerType,
72 LPCWSTR szThreadingModel)
74 HKEY hkey, hsubkey = NULL;
75 LONG ret = RegCreateKeyW(clsid, szCLSID, &hkey);
76 if (ERROR_SUCCESS != ret)
77 return HRESULT_FROM_WIN32(ret);
79 /* set description string */
80 ret = RegSetValueW(hkey, NULL, REG_SZ, szDescription,
81 sizeof(WCHAR) * (lstrlenW(szDescription) + 1));
82 if (ERROR_SUCCESS != ret)
83 goto err_out;
85 /* create CLSID\\{"CLSID"}\\"ServerType" key, using key to CLSID\\{"CLSID"}
86 passed back by last call to RegCreateKeyW(). */
87 ret = RegCreateKeyW(hkey, szServerType, &hsubkey);
88 if (ERROR_SUCCESS != ret)
89 goto err_out;
91 /* set server path */
92 ret = RegSetValueW(hsubkey, NULL, REG_SZ, szFileName,
93 sizeof(WCHAR) * (lstrlenW(szFileName) + 1));
94 if (ERROR_SUCCESS != ret)
95 goto err_out;
97 /* set threading model */
98 ret = RegSetValueExW(hsubkey, tmodel_keyname, 0L, REG_SZ,
99 (const BYTE*)szThreadingModel,
100 sizeof(WCHAR) * (lstrlenW(szThreadingModel) + 1));
101 err_out:
102 if (hsubkey)
103 RegCloseKey(hsubkey);
104 RegCloseKey(hkey);
105 return HRESULT_FROM_WIN32(ret);
109 * RegisterAllClasses()
111 static HRESULT SetupRegisterAllClasses(const FactoryTemplate * pList, int num,
112 LPCWSTR szFileName, BOOL bRegister)
114 HRESULT hr = NOERROR;
115 HKEY hkey;
116 OLECHAR szCLSID[CHARS_IN_GUID];
117 LONG i, ret = RegCreateKeyW(HKEY_CLASSES_ROOT, clsid_keyname, &hkey);
118 if (ERROR_SUCCESS != ret)
119 return HRESULT_FROM_WIN32(ret);
121 for (i = 0; i < num; i++, pList++)
123 /* (un)register CLSID and InprocServer32 */
124 hr = StringFromGUID2(pList->m_ClsID, szCLSID, CHARS_IN_GUID);
125 if (SUCCEEDED(hr))
127 if (bRegister )
128 hr = SetupRegisterClass(hkey, szCLSID,
129 pList->m_Name, szFileName,
130 ips32_keyname, tmodel_both);
131 else
132 hr = RegDeleteTreeW(hkey, szCLSID);
135 RegCloseKey(hkey);
136 return hr;
139 HRESULT WINAPI AMovieSetupRegisterFilter2(const AMOVIESETUP_FILTER *pFilter, IFilterMapper2 *pIFM2, BOOL bRegister)
141 if (!pFilter)
142 return S_OK;
144 if (bRegister)
147 REGFILTER2 rf2;
148 rf2.dwVersion = 1;
149 rf2.dwMerit = pFilter->merit;
150 rf2.u.s.cPins = pFilter->pins;
151 rf2.u.s.rgPins = pFilter->pPin;
153 return IFilterMapper2_RegisterFilter(pIFM2, pFilter->clsid, pFilter->name, NULL, &CLSID_LegacyAmFilterCategory, NULL, &rf2);
156 else
157 return IFilterMapper2_UnregisterFilter(pIFM2, &CLSID_LegacyAmFilterCategory, NULL, pFilter->clsid);
160 HRESULT WINAPI AMovieDllRegisterServer2(BOOL bRegister)
162 HRESULT hr;
163 int i;
164 IFilterMapper2 *pIFM2 = NULL;
165 WCHAR szFileName[MAX_PATH];
167 if (!GetModuleFileNameW(g_hInst, szFileName, MAX_PATH))
169 ERR("Failed to get module file name for registration\n");
170 return E_FAIL;
173 if (bRegister)
174 hr = SetupRegisterAllClasses(g_Templates, g_cTemplates, szFileName, TRUE );
176 hr = CoInitialize(NULL);
178 TRACE("Getting IFilterMapper2\r\n");
179 hr = CoCreateInstance(&CLSID_FilterMapper2, NULL, CLSCTX_INPROC_SERVER,
180 &IID_IFilterMapper2, (void **)&pIFM2);
182 for (i = 0; SUCCEEDED(hr) && i < g_cTemplates; i++)
183 hr = AMovieSetupRegisterFilter2(g_Templates[i].m_pAMovieSetup_Filter, pIFM2, bRegister);
185 /* release interface */
186 if (pIFM2)
187 IFilterMapper2_Release(pIFM2);
189 /* and clear up */
190 CoFreeUnusedLibraries();
191 CoUninitialize();
193 /* if unregistering, unregister all OLE servers */
194 if (SUCCEEDED(hr) && !bRegister)
195 hr = SetupRegisterAllClasses(g_Templates, g_cTemplates, szFileName, FALSE);
197 return hr;
200 /****************************************************************************
201 * SetupInitializeServers
203 * This function is table driven using the static members of the
204 * CFactoryTemplate class defined in the Dll.
206 * It calls the initialize function for any class in CFactoryTemplate with
207 * one defined.
209 ****************************************************************************/
210 static void SetupInitializeServers(const FactoryTemplate * pList, int num,
211 BOOL bLoading)
213 int i;
215 for (i = 0; i < num; i++, pList++)
217 if (pList->m_lpfnInit)
218 pList->m_lpfnInit(bLoading, pList->m_ClsID);
222 BOOL WINAPI STRMBASE_DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpv)
224 switch (fdwReason)
226 case DLL_PROCESS_ATTACH:
227 g_hInst = hInstDLL;
228 DisableThreadLibraryCalls(hInstDLL);
229 SetupInitializeServers(g_Templates, g_cTemplates, TRUE);
230 break;
231 case DLL_PROCESS_DETACH:
232 SetupInitializeServers(g_Templates, g_cTemplates, FALSE);
233 break;
235 return TRUE;
238 /******************************************************************************
239 * DLL ClassFactory
241 typedef struct {
242 IClassFactory ITF_IClassFactory;
244 LONG ref;
245 LPFNNewCOMObject pfnCreateInstance;
246 } IClassFactoryImpl;
248 static HRESULT WINAPI
249 DSCF_QueryInterface(LPCLASSFACTORY iface,REFIID riid,LPVOID *ppobj)
251 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
253 if (IsEqualGUID(riid, &IID_IUnknown) ||
254 IsEqualGUID(riid, &IID_IClassFactory))
256 IClassFactory_AddRef(iface);
257 *ppobj = This;
258 return S_OK;
261 WARN("(%p)->(%s,%p), not found\n", This, debugstr_guid(riid), ppobj);
262 return E_NOINTERFACE;
265 static ULONG WINAPI DSCF_AddRef(LPCLASSFACTORY iface)
267 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
268 return InterlockedIncrement(&This->ref);
271 static ULONG WINAPI DSCF_Release(LPCLASSFACTORY iface)
273 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
275 ULONG ref = InterlockedDecrement(&This->ref);
277 if (ref == 0)
278 HeapFree(GetProcessHeap(), 0, This);
280 return ref;
283 static HRESULT WINAPI DSCF_CreateInstance(LPCLASSFACTORY iface, LPUNKNOWN pOuter,
284 REFIID riid, LPVOID *ppobj)
286 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
287 HRESULT hres = ERROR_SUCCESS;
288 LPUNKNOWN punk;
290 TRACE("(%p)->(%p,%s,%p)\n", This, pOuter, debugstr_guid(riid), ppobj);
292 if (!ppobj)
293 return E_POINTER;
295 /* Enforce the normal OLE rules regarding interfaces and delegation */
296 if (pOuter && !IsEqualGUID(riid, &IID_IUnknown))
297 return E_NOINTERFACE;
299 *ppobj = NULL;
300 punk = This->pfnCreateInstance(pOuter, &hres);
301 if (!punk)
303 /* No object created, update error if it isn't done already and return */
304 if (SUCCEEDED(hres))
305 hres = E_OUTOFMEMORY;
306 return hres;
309 if (SUCCEEDED(hres))
311 hres = IUnknown_QueryInterface(punk, riid, ppobj);
313 /* Releasing the object. If everything was successful, QueryInterface
314 should have incremented the refcount once more, otherwise this will
315 purge the object. */
316 IUnknown_Release(punk);
317 return hres;
320 static HRESULT WINAPI DSCF_LockServer(LPCLASSFACTORY iface, BOOL dolock)
322 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
323 TRACE("(%p)->(%d)\n",This, dolock);
325 if (dolock)
326 InterlockedIncrement(&server_locks);
327 else
328 InterlockedDecrement(&server_locks);
329 return S_OK;
332 static const IClassFactoryVtbl DSCF_Vtbl =
334 DSCF_QueryInterface,
335 DSCF_AddRef,
336 DSCF_Release,
337 DSCF_CreateInstance,
338 DSCF_LockServer
341 /***********************************************************************
342 * DllGetClassObject
344 HRESULT WINAPI STRMBASE_DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
346 const FactoryTemplate *pList = g_Templates;
347 IClassFactoryImpl *factory;
348 int i;
350 TRACE("(%s,%s,%p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
352 if (!ppv)
353 return E_POINTER;
355 *ppv = NULL;
357 if (!IsEqualGUID(&IID_IClassFactory, riid) &&
358 !IsEqualGUID(&IID_IUnknown, riid))
359 return E_NOINTERFACE;
361 for (i = 0; i < g_cTemplates; i++, pList++)
363 if (IsEqualGUID(pList->m_ClsID, rclsid))
364 break;
367 if (i == g_cTemplates)
369 FIXME("%s: no class found.\n", debugstr_guid(rclsid));
370 return CLASS_E_CLASSNOTAVAILABLE;
373 factory = HeapAlloc(GetProcessHeap(), 0, sizeof(IClassFactoryImpl));
374 if (!factory)
375 return E_OUTOFMEMORY;
377 factory->ITF_IClassFactory.lpVtbl = &DSCF_Vtbl;
378 factory->ref = 1;
380 factory->pfnCreateInstance = pList->m_lpfnNew;
382 *ppv = &(factory->ITF_IClassFactory);
383 return S_OK;
386 /***********************************************************************
387 * DllCanUnloadNow
389 HRESULT WINAPI STRMBASE_DllCanUnloadNow(void)
391 TRACE("\n");
393 if (server_locks == 0)
394 return S_OK;
395 return S_FALSE;