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
,
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
)
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
)
68 ret
= RegSetValueW(hsubkey
, NULL
, REG_SZ
, szFileName
,
69 sizeof(WCHAR
) * (lstrlenW(szFileName
) + 1));
70 if (ERROR_SUCCESS
!= ret
)
73 /* set threading model */
74 ret
= RegSetValueExW(hsubkey
, tmodel_keyname
, 0L, REG_SZ
,
75 (const BYTE
*)szThreadingModel
,
76 sizeof(WCHAR
) * (lstrlenW(szThreadingModel
) + 1));
81 return HRESULT_FROM_WIN32(ret
);
85 * RegisterAllClasses()
87 static HRESULT
SetupRegisterAllClasses(const FactoryTemplate
* pList
, int num
,
88 LPCWSTR szFileName
, BOOL bRegister
)
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
);
104 hr
= SetupRegisterClass(hkey
, szCLSID
,
105 pList
->m_Name
, szFileName
,
106 ips32_keyname
, tmodel_both
);
108 hr
= RegDeleteTreeW(hkey
, szCLSID
);
115 HRESULT WINAPI
AMovieSetupRegisterFilter2(const AMOVIESETUP_FILTER
*pFilter
, IFilterMapper2
*pIFM2
, BOOL bRegister
)
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
);
133 return IFilterMapper2_UnregisterFilter(pIFM2
, &CLSID_LegacyAmFilterCategory
, NULL
, pFilter
->clsid
);
136 HRESULT WINAPI
AMovieDllRegisterServer2(BOOL bRegister
)
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");
150 hr
= SetupRegisterAllClasses(g_Templates
, g_cTemplates
, szFileName
, TRUE
);
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 */
163 IFilterMapper2_Release(pIFM2
);
166 CoFreeUnusedLibraries();
169 /* if unregistering, unregister all OLE servers */
170 if (SUCCEEDED(hr
) && !bRegister
)
171 hr
= SetupRegisterAllClasses(g_Templates
, g_cTemplates
, szFileName
, FALSE
);
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
185 ****************************************************************************/
186 static void SetupInitializeServers(const FactoryTemplate
* pList
, int num
,
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
)
202 case DLL_PROCESS_ATTACH
:
204 DisableThreadLibraryCalls(hInstDLL
);
205 SetupInitializeServers(g_Templates
, g_cTemplates
, TRUE
);
207 case DLL_PROCESS_DETACH
:
208 SetupInitializeServers(g_Templates
, g_cTemplates
, FALSE
);
214 /******************************************************************************
218 IClassFactory IClassFactory_iface
;
220 LPFNNewCOMObject pfnCreateInstance
;
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
);
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
);
255 HeapFree(GetProcessHeap(), 0, This
);
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
;
267 TRACE("(%p)->(%p,%s,%p)\n", This
, pOuter
, debugstr_guid(riid
), ppobj
);
272 /* Enforce the normal OLE rules regarding interfaces and delegation */
273 if (pOuter
&& !IsEqualGUID(riid
, &IID_IUnknown
))
274 return E_NOINTERFACE
;
277 punk
= This
->pfnCreateInstance(pOuter
, &hres
);
280 /* No object created, update error if it isn't done already and return */
282 hres
= E_OUTOFMEMORY
;
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
293 IUnknown_Release(punk
);
297 static HRESULT WINAPI
DSCF_LockServer(IClassFactory
*iface
, BOOL dolock
)
299 IClassFactoryImpl
*This
= impl_from_IClassFactory(iface
);
300 TRACE("(%p)->(%d)\n",This
, dolock
);
303 InterlockedIncrement(&server_locks
);
305 InterlockedDecrement(&server_locks
);
309 static const IClassFactoryVtbl DSCF_Vtbl
=
318 /***********************************************************************
321 HRESULT WINAPI
STRMBASE_DllGetClassObject(REFCLSID rclsid
, REFIID riid
, LPVOID
*ppv
)
323 const FactoryTemplate
*pList
= g_Templates
;
324 IClassFactoryImpl
*factory
;
327 TRACE("(%s,%s,%p)\n", debugstr_guid(rclsid
), debugstr_guid(riid
), ppv
);
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
))
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
));
360 return E_OUTOFMEMORY
;
362 factory
->IClassFactory_iface
.lpVtbl
= &DSCF_Vtbl
;
365 factory
->pfnCreateInstance
= pList
->m_lpfnNew
;
367 *ppv
= &factory
->IClassFactory_iface
;
371 /***********************************************************************
374 HRESULT WINAPI
STRMBASE_DllCanUnloadNow(void)
378 if (server_locks
== 0)