winex11.drv: Ignore .dwAspect in FORMATETC during XDnD.
[wine.git] / dlls / strmbase / dllfunc.c
blob842bee6121decea9a41e5366a7cee9ae6baaf3f8
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
22 #include "strmbase_private.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(strmbase);
26 extern const int g_cTemplates;
27 extern const FactoryTemplate g_Templates[];
29 static HINSTANCE g_hInst = NULL;
30 static LONG server_locks = 0;
33 * defines and constants
36 static const WCHAR clsid_keyname[] = {'C','L','S','I','D',0 };
37 static const WCHAR ips32_keyname[] = {'I','n','P','r','o','c','S','e','r','v','e','r','3','2',0};
38 static const WCHAR tmodel_keyname[] = {'T','h','r','e','a','d','i','n','g','M','o','d','e','l',0};
39 static const WCHAR tmodel_both[] = {'B','o','t','h',0};
42 * SetupRegisterClass()
44 static HRESULT SetupRegisterClass(HKEY clsid, LPCWSTR szCLSID,
45 LPCWSTR szDescription,
46 LPCWSTR szFileName,
47 LPCWSTR szServerType,
48 LPCWSTR szThreadingModel)
50 HKEY hkey, hsubkey = NULL;
51 LONG ret = RegCreateKeyW(clsid, szCLSID, &hkey);
52 if (ERROR_SUCCESS != ret)
53 return HRESULT_FROM_WIN32(ret);
55 /* set description string */
56 ret = RegSetValueW(hkey, NULL, REG_SZ, szDescription,
57 sizeof(WCHAR) * (lstrlenW(szDescription) + 1));
58 if (ERROR_SUCCESS != ret)
59 goto err_out;
61 /* create CLSID\\{"CLSID"}\\"ServerType" key, using key to CLSID\\{"CLSID"}
62 passed back by last call to RegCreateKeyW(). */
63 ret = RegCreateKeyW(hkey, szServerType, &hsubkey);
64 if (ERROR_SUCCESS != ret)
65 goto err_out;
67 /* set server path */
68 ret = RegSetValueW(hsubkey, NULL, REG_SZ, szFileName,
69 sizeof(WCHAR) * (lstrlenW(szFileName) + 1));
70 if (ERROR_SUCCESS != ret)
71 goto err_out;
73 /* set threading model */
74 ret = RegSetValueExW(hsubkey, tmodel_keyname, 0L, REG_SZ,
75 (const BYTE*)szThreadingModel,
76 sizeof(WCHAR) * (lstrlenW(szThreadingModel) + 1));
77 err_out:
78 if (hsubkey)
79 RegCloseKey(hsubkey);
80 RegCloseKey(hkey);
81 return HRESULT_FROM_WIN32(ret);
85 * RegisterAllClasses()
87 static HRESULT SetupRegisterAllClasses(const FactoryTemplate * pList, int num,
88 LPCWSTR szFileName, BOOL bRegister)
90 HRESULT hr = NOERROR;
91 HKEY hkey;
92 OLECHAR szCLSID[CHARS_IN_GUID];
93 LONG i, ret = RegCreateKeyW(HKEY_CLASSES_ROOT, clsid_keyname, &hkey);
94 if (ERROR_SUCCESS != ret)
95 return HRESULT_FROM_WIN32(ret);
97 for (i = 0; i < num; i++, pList++)
99 /* (un)register CLSID and InprocServer32 */
100 hr = StringFromGUID2(pList->m_ClsID, szCLSID, CHARS_IN_GUID);
101 if (SUCCEEDED(hr))
103 if (bRegister )
104 hr = SetupRegisterClass(hkey, szCLSID,
105 pList->m_Name, szFileName,
106 ips32_keyname, tmodel_both);
107 else
108 hr = RegDeleteTreeW(hkey, szCLSID);
111 RegCloseKey(hkey);
112 return hr;
115 HRESULT WINAPI AMovieSetupRegisterFilter2(const AMOVIESETUP_FILTER *pFilter, IFilterMapper2 *pIFM2, BOOL bRegister)
117 if (!pFilter)
118 return S_OK;
120 if (bRegister)
123 REGFILTER2 rf2;
124 rf2.dwVersion = 1;
125 rf2.dwMerit = pFilter->merit;
126 rf2.u.s1.cPins = pFilter->pins;
127 rf2.u.s1.rgPins = pFilter->pPin;
129 return IFilterMapper2_RegisterFilter(pIFM2, pFilter->clsid, pFilter->name, NULL, &CLSID_LegacyAmFilterCategory, NULL, &rf2);
132 else
133 return IFilterMapper2_UnregisterFilter(pIFM2, &CLSID_LegacyAmFilterCategory, NULL, pFilter->clsid);
136 HRESULT WINAPI AMovieDllRegisterServer2(BOOL bRegister)
138 HRESULT hr;
139 int i;
140 IFilterMapper2 *pIFM2 = NULL;
141 WCHAR szFileName[MAX_PATH];
143 if (!GetModuleFileNameW(g_hInst, szFileName, MAX_PATH))
145 ERR("Failed to get module file name for registration\n");
146 return E_FAIL;
149 if (bRegister)
150 hr = SetupRegisterAllClasses(g_Templates, g_cTemplates, szFileName, TRUE );
152 CoInitialize(NULL);
154 TRACE("Getting IFilterMapper2\r\n");
155 hr = CoCreateInstance(&CLSID_FilterMapper2, NULL, CLSCTX_INPROC_SERVER,
156 &IID_IFilterMapper2, (void **)&pIFM2);
158 for (i = 0; SUCCEEDED(hr) && i < g_cTemplates; i++)
159 hr = AMovieSetupRegisterFilter2(g_Templates[i].m_pAMovieSetup_Filter, pIFM2, bRegister);
161 /* release interface */
162 if (pIFM2)
163 IFilterMapper2_Release(pIFM2);
165 /* and clear up */
166 CoFreeUnusedLibraries();
167 CoUninitialize();
169 /* if unregistering, unregister all OLE servers */
170 if (SUCCEEDED(hr) && !bRegister)
171 hr = SetupRegisterAllClasses(g_Templates, g_cTemplates, szFileName, FALSE);
173 return hr;
176 /****************************************************************************
177 * SetupInitializeServers
179 * This function is table driven using the static members of the
180 * CFactoryTemplate class defined in the Dll.
182 * It calls the initialize function for any class in CFactoryTemplate with
183 * one defined.
185 ****************************************************************************/
186 static void SetupInitializeServers(const FactoryTemplate * pList, int num,
187 BOOL bLoading)
189 int i;
191 for (i = 0; i < num; i++, pList++)
193 if (pList->m_lpfnInit)
194 pList->m_lpfnInit(bLoading, pList->m_ClsID);
198 BOOL WINAPI STRMBASE_DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpv)
200 switch (fdwReason)
202 case DLL_PROCESS_ATTACH:
203 g_hInst = hInstDLL;
204 DisableThreadLibraryCalls(hInstDLL);
205 SetupInitializeServers(g_Templates, g_cTemplates, TRUE);
206 break;
207 case DLL_PROCESS_DETACH:
208 SetupInitializeServers(g_Templates, g_cTemplates, FALSE);
209 break;
211 return TRUE;
214 /******************************************************************************
215 * DLL ClassFactory
217 typedef struct {
218 IClassFactory IClassFactory_iface;
219 LONG ref;
220 LPFNNewCOMObject pfnCreateInstance;
221 } IClassFactoryImpl;
223 static inline IClassFactoryImpl *impl_from_IClassFactory(IClassFactory *iface)
225 return CONTAINING_RECORD(iface, IClassFactoryImpl, IClassFactory_iface);
228 static HRESULT WINAPI DSCF_QueryInterface(IClassFactory *iface, REFIID riid, void **ppobj)
230 if (IsEqualGUID(riid, &IID_IUnknown) ||
231 IsEqualGUID(riid, &IID_IClassFactory))
233 IClassFactory_AddRef(iface);
234 *ppobj = iface;
235 return S_OK;
238 *ppobj = NULL;
239 WARN("(%p)->(%s,%p), not found\n", iface, debugstr_guid(riid), ppobj);
240 return E_NOINTERFACE;
243 static ULONG WINAPI DSCF_AddRef(IClassFactory *iface)
245 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
246 return InterlockedIncrement(&This->ref);
249 static ULONG WINAPI DSCF_Release(IClassFactory *iface)
251 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
252 ULONG ref = InterlockedDecrement(&This->ref);
254 if (ref == 0)
255 HeapFree(GetProcessHeap(), 0, This);
257 return ref;
260 static HRESULT WINAPI DSCF_CreateInstance(IClassFactory *iface, IUnknown *pOuter,
261 REFIID riid, void **ppobj)
263 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
264 HRESULT hres = ERROR_SUCCESS;
265 LPUNKNOWN punk;
267 TRACE("(%p)->(%p,%s,%p)\n", This, pOuter, debugstr_guid(riid), ppobj);
269 if (!ppobj)
270 return E_POINTER;
272 /* Enforce the normal OLE rules regarding interfaces and delegation */
273 if (pOuter && !IsEqualGUID(riid, &IID_IUnknown))
274 return E_NOINTERFACE;
276 *ppobj = NULL;
277 punk = This->pfnCreateInstance(pOuter, &hres);
278 if (!punk)
280 /* No object created, update error if it isn't done already and return */
281 if (SUCCEEDED(hres))
282 hres = E_OUTOFMEMORY;
283 return hres;
286 if (SUCCEEDED(hres))
288 hres = IUnknown_QueryInterface(punk, riid, ppobj);
290 /* Releasing the object. If everything was successful, QueryInterface
291 should have incremented the refcount once more, otherwise this will
292 purge the object. */
293 IUnknown_Release(punk);
294 return hres;
297 static HRESULT WINAPI DSCF_LockServer(IClassFactory *iface, BOOL dolock)
299 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
300 TRACE("(%p)->(%d)\n",This, dolock);
302 if (dolock)
303 InterlockedIncrement(&server_locks);
304 else
305 InterlockedDecrement(&server_locks);
306 return S_OK;
309 static const IClassFactoryVtbl DSCF_Vtbl =
311 DSCF_QueryInterface,
312 DSCF_AddRef,
313 DSCF_Release,
314 DSCF_CreateInstance,
315 DSCF_LockServer
318 /***********************************************************************
319 * DllGetClassObject
321 HRESULT WINAPI STRMBASE_DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
323 const FactoryTemplate *pList = g_Templates;
324 IClassFactoryImpl *factory;
325 int i;
327 TRACE("(%s,%s,%p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
329 if (!ppv)
330 return E_POINTER;
332 *ppv = NULL;
334 if (!IsEqualGUID(&IID_IClassFactory, riid) &&
335 !IsEqualGUID(&IID_IUnknown, riid))
336 return E_NOINTERFACE;
338 for (i = 0; i < g_cTemplates; i++, pList++)
340 if (IsEqualGUID(pList->m_ClsID, rclsid))
341 break;
344 if (i == g_cTemplates)
346 char dllname[MAX_PATH];
347 if (!GetModuleFileNameA(g_hInst, dllname, sizeof(dllname)))
348 strcpy(dllname, "???");
349 ERR("%s: no class found in %s.\n", debugstr_guid(rclsid), dllname);
350 return CLASS_E_CLASSNOTAVAILABLE;
352 else if (!pList->m_lpfnNew)
354 FIXME("%s: class not implemented yet.\n", debugstr_guid(rclsid));
355 return CLASS_E_CLASSNOTAVAILABLE;
358 factory = HeapAlloc(GetProcessHeap(), 0, sizeof(IClassFactoryImpl));
359 if (!factory)
360 return E_OUTOFMEMORY;
362 factory->IClassFactory_iface.lpVtbl = &DSCF_Vtbl;
363 factory->ref = 1;
365 factory->pfnCreateInstance = pList->m_lpfnNew;
367 *ppv = &factory->IClassFactory_iface;
368 return S_OK;
371 /***********************************************************************
372 * DllCanUnloadNow
374 HRESULT WINAPI STRMBASE_DllCanUnloadNow(void)
376 TRACE("\n");
378 if (server_locks == 0)
379 return S_OK;
380 return S_FALSE;