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
27 #define NONAMELESSSTRUCT
28 #define NONAMELESSUNION
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[] =
66 * SetupRegisterClass()
68 static HRESULT SetupRegisterClass(HKEY clsid, LPCWSTR szCLSID,
69 LPCWSTR szDescription,
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)
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)
92 ret = RegSetValueW(hsubkey, NULL, REG_SZ, szFileName,
93 sizeof(WCHAR) * (lstrlenW(szFileName) + 1));
94 if (ERROR_SUCCESS != ret)
97 /* set threading model */
98 ret = RegSetValueExW(hsubkey, tmodel_keyname, 0L, REG_SZ,
99 (const BYTE*)szThreadingModel,
100 sizeof(WCHAR) * (lstrlenW(szThreadingModel) + 1));
103 RegCloseKey(hsubkey);
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;
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);
128 hr = SetupRegisterClass(hkey, szCLSID,
129 pList->m_Name, szFileName,
130 ips32_keyname, tmodel_both);
132 hr = RegDeleteTreeW(hkey, szCLSID);
139 HRESULT WINAPI AMovieSetupRegisterFilter2(const AMOVIESETUP_FILTER *pFilter, IFilterMapper2 *pIFM2, BOOL bRegister)
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);
157 return IFilterMapper2_UnregisterFilter(pIFM2, &CLSID_LegacyAmFilterCategory, NULL, pFilter->clsid);
160 HRESULT WINAPI AMovieDllRegisterServer2(BOOL bRegister)
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");
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 */
187 IFilterMapper2_Release(pIFM2);
190 CoFreeUnusedLibraries();
193 /* if unregistering, unregister all OLE servers */
194 if (SUCCEEDED(hr) && !bRegister)
195 hr = SetupRegisterAllClasses(g_Templates, g_cTemplates, szFileName, FALSE);
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
209 ****************************************************************************/
210 static void SetupInitializeServers(const FactoryTemplate * pList, int num,
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)
226 case DLL_PROCESS_ATTACH:
228 DisableThreadLibraryCalls(hInstDLL);
229 SetupInitializeServers(g_Templates, g_cTemplates, TRUE);
231 case DLL_PROCESS_DETACH:
232 SetupInitializeServers(g_Templates, g_cTemplates, FALSE);
238 /******************************************************************************
242 IClassFactory ITF_IClassFactory;
245 LPFNNewCOMObject pfnCreateInstance;
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);
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);
278 HeapFree(GetProcessHeap(), 0, This);
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;
290 TRACE("(%p)->(%p,%s,%p)\n", This, pOuter, debugstr_guid(riid), ppobj);
295 /* Enforce the normal OLE rules regarding interfaces and delegation */
296 if (pOuter && !IsEqualGUID(riid, &IID_IUnknown))
297 return E_NOINTERFACE;
300 punk = This->pfnCreateInstance(pOuter, &hres);
303 /* No object created, update error if it isn't done already and return */
305 hres = E_OUTOFMEMORY;
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
316 IUnknown_Release(punk);
320 static HRESULT WINAPI DSCF_LockServer(LPCLASSFACTORY iface, BOOL dolock)
322 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
323 TRACE("(%p)->(%d)\n",This, dolock);
326 InterlockedIncrement(&server_locks);
328 InterlockedDecrement(&server_locks);
332 static const IClassFactoryVtbl DSCF_Vtbl =
341 /***********************************************************************
344 HRESULT WINAPI STRMBASE_DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
346 const FactoryTemplate *pList = g_Templates;
347 IClassFactoryImpl *factory;
350 TRACE("(%s,%s,%p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
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))
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));
375 return E_OUTOFMEMORY;
377 factory->ITF_IClassFactory.lpVtbl = &DSCF_Vtbl;
380 factory->pfnCreateInstance = pList->m_lpfnNew;
382 *ppv = &(factory->ITF_IClassFactory);
386 /***********************************************************************
389 HRESULT WINAPI STRMBASE_DllCanUnloadNow(void)
393 if (server_locks == 0)