oleaut32: Add a test for loading/saving an empty picture.
[wine] / dlls / 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 /*
62  * SetupRegisterClass()
63  */
64 static HRESULT SetupRegisterClass(HKEY clsid, LPCWSTR szCLSID,
65                                   LPCWSTR szDescription,
66                                   LPCWSTR szFileName,
67                                   LPCWSTR szServerType,
68                                   LPCWSTR szThreadingModel)
69 {
70     HKEY hkey, hsubkey = NULL;
71     LONG ret = RegCreateKeyW(clsid, szCLSID, &hkey);
72     if (ERROR_SUCCESS != ret)
73         return HRESULT_FROM_WIN32(ret);
74
75     /* set description string */
76     ret = RegSetValueW(hkey, NULL, REG_SZ, szDescription,
77                        sizeof(WCHAR) * (lstrlenW(szDescription) + 1));
78     if (ERROR_SUCCESS != ret)
79         goto err_out;
80
81     /* create CLSID\\{"CLSID"}\\"ServerType" key, using key to CLSID\\{"CLSID"}
82        passed back by last call to RegCreateKeyW(). */
83     ret = RegCreateKeyW(hkey,  szServerType, &hsubkey);
84     if (ERROR_SUCCESS != ret)
85         goto err_out;
86
87     /* set server path */
88     ret = RegSetValueW(hsubkey, NULL, REG_SZ, szFileName,
89                        sizeof(WCHAR) * (lstrlenW(szFileName) + 1));
90     if (ERROR_SUCCESS != ret)
91         goto err_out;
92
93     /* set threading model */
94     ret = RegSetValueExW(hsubkey, tmodel_keyname, 0L, REG_SZ,
95                          (const BYTE*)szThreadingModel,
96                          sizeof(WCHAR) * (lstrlenW(szThreadingModel) + 1));
97 err_out:
98     if (hsubkey)
99         RegCloseKey(hsubkey);
100     RegCloseKey(hkey);
101     return HRESULT_FROM_WIN32(ret);
102 }
103
104 /*
105  * RegisterAllClasses()
106  */
107 static HRESULT SetupRegisterAllClasses(const FactoryTemplate * pList, int num,
108                                        LPCWSTR szFileName, BOOL bRegister)
109 {
110     HRESULT hr = NOERROR;
111     HKEY hkey;
112     OLECHAR szCLSID[CHARS_IN_GUID];
113     LONG i, ret = RegCreateKeyW(HKEY_CLASSES_ROOT, clsid_keyname, &hkey);
114     if (ERROR_SUCCESS != ret)
115         return HRESULT_FROM_WIN32(ret);
116
117     for (i = 0; i < num; i++, pList++)
118     {
119         /* (un)register CLSID and InprocServer32 */
120         hr = StringFromGUID2(pList->m_ClsID, szCLSID, CHARS_IN_GUID);
121         if (SUCCEEDED(hr))
122         {
123             if (bRegister )
124                 hr = SetupRegisterClass(hkey, szCLSID,
125                                         pList->m_Name, szFileName,
126                                         ips32_keyname, tmodel_both);
127             else
128                 hr = RegDeleteTreeW(hkey, szCLSID);
129         }
130     }
131     RegCloseKey(hkey);
132     return hr;
133 }
134
135 HRESULT WINAPI AMovieSetupRegisterFilter2(const AMOVIESETUP_FILTER *pFilter, IFilterMapper2  *pIFM2, BOOL  bRegister)
136 {
137     if (!pFilter)
138         return S_OK;
139
140     if (bRegister)
141     {
142         {
143             REGFILTER2 rf2;
144             rf2.dwVersion = 1;
145             rf2.dwMerit = pFilter->merit;
146             rf2.u.s1.cPins = pFilter->pins;
147             rf2.u.s1.rgPins = pFilter->pPin;
148
149             return IFilterMapper2_RegisterFilter(pIFM2, pFilter->clsid, pFilter->name, NULL, &CLSID_LegacyAmFilterCategory, NULL, &rf2);
150         }
151     }
152     else
153        return IFilterMapper2_UnregisterFilter(pIFM2, &CLSID_LegacyAmFilterCategory, NULL, pFilter->clsid);
154 }
155
156 HRESULT WINAPI AMovieDllRegisterServer2(BOOL bRegister)
157 {
158     HRESULT hr;
159     int i;
160     IFilterMapper2 *pIFM2 = NULL;
161     WCHAR szFileName[MAX_PATH];
162
163     if (!GetModuleFileNameW(g_hInst, szFileName, MAX_PATH))
164     {
165         ERR("Failed to get module file name for registration\n");
166         return E_FAIL;
167     }
168
169     if (bRegister)
170         hr = SetupRegisterAllClasses(g_Templates, g_cTemplates, szFileName, TRUE );
171
172     hr = CoInitialize(NULL);
173
174     TRACE("Getting IFilterMapper2\r\n");
175     hr = CoCreateInstance(&CLSID_FilterMapper2, NULL, CLSCTX_INPROC_SERVER,
176                           &IID_IFilterMapper2, (void **)&pIFM2);
177
178     for (i = 0; SUCCEEDED(hr) && i < g_cTemplates; i++)
179         hr = AMovieSetupRegisterFilter2(g_Templates[i].m_pAMovieSetup_Filter, pIFM2, bRegister);
180
181     /* release interface */
182     if (pIFM2)
183         IFilterMapper2_Release(pIFM2);
184
185     /* and clear up */
186     CoFreeUnusedLibraries();
187     CoUninitialize();
188
189     /* if unregistering, unregister all OLE servers */
190     if (SUCCEEDED(hr) && !bRegister)
191         hr = SetupRegisterAllClasses(g_Templates, g_cTemplates, szFileName, FALSE);
192
193     return hr;
194 }
195
196 /****************************************************************************
197  * SetupInitializeServers
198  *
199  * This function is table driven using the static members of the
200  * CFactoryTemplate class defined in the Dll.
201  *
202  * It calls the initialize function for any class in CFactoryTemplate with
203  * one defined.
204  *
205  ****************************************************************************/
206 static void SetupInitializeServers(const FactoryTemplate * pList, int num,
207                             BOOL bLoading)
208 {
209     int i;
210
211     for (i = 0; i < num; i++, pList++)
212     {
213         if (pList->m_lpfnInit)
214             pList->m_lpfnInit(bLoading, pList->m_ClsID);
215     }
216 }
217
218 BOOL WINAPI STRMBASE_DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpv)
219 {
220     switch (fdwReason)
221     {
222         case DLL_PROCESS_ATTACH:
223             g_hInst = hInstDLL;
224             DisableThreadLibraryCalls(hInstDLL);
225             SetupInitializeServers(g_Templates, g_cTemplates, TRUE);
226             break;
227         case DLL_PROCESS_DETACH:
228             SetupInitializeServers(g_Templates, g_cTemplates, FALSE);
229             break;
230     }
231     return TRUE;
232 }
233
234 /******************************************************************************
235  * DLL ClassFactory
236  */
237 typedef struct {
238     IClassFactory IClassFactory_iface;
239     LONG ref;
240     LPFNNewCOMObject pfnCreateInstance;
241 } IClassFactoryImpl;
242
243 static inline IClassFactoryImpl *impl_from_IClassFactory(IClassFactory *iface)
244 {
245     return CONTAINING_RECORD(iface, IClassFactoryImpl, IClassFactory_iface);
246 }
247
248 static HRESULT WINAPI DSCF_QueryInterface(IClassFactory *iface, REFIID riid, void **ppobj)
249 {
250     if (IsEqualGUID(riid, &IID_IUnknown) ||
251         IsEqualGUID(riid, &IID_IClassFactory))
252     {
253         IClassFactory_AddRef(iface);
254         *ppobj = iface;
255         return S_OK;
256     }
257
258     *ppobj = NULL;
259     WARN("(%p)->(%s,%p), not found\n", iface, debugstr_guid(riid), ppobj);
260     return E_NOINTERFACE;
261 }
262
263 static ULONG WINAPI DSCF_AddRef(IClassFactory *iface)
264 {
265     IClassFactoryImpl *This = impl_from_IClassFactory(iface);
266     return InterlockedIncrement(&This->ref);
267 }
268
269 static ULONG WINAPI DSCF_Release(IClassFactory *iface)
270 {
271     IClassFactoryImpl *This = impl_from_IClassFactory(iface);
272     ULONG ref = InterlockedDecrement(&This->ref);
273
274     if (ref == 0)
275         HeapFree(GetProcessHeap(), 0, This);
276
277     return ref;
278 }
279
280 static HRESULT WINAPI DSCF_CreateInstance(IClassFactory *iface, IUnknown *pOuter,
281                                           REFIID riid, void **ppobj)
282 {
283     IClassFactoryImpl *This = impl_from_IClassFactory(iface);
284     HRESULT hres = ERROR_SUCCESS;
285     LPUNKNOWN punk;
286
287     TRACE("(%p)->(%p,%s,%p)\n", This, pOuter, debugstr_guid(riid), ppobj);
288
289     if (!ppobj)
290         return E_POINTER;
291
292     /* Enforce the normal OLE rules regarding interfaces and delegation */
293     if (pOuter && !IsEqualGUID(riid, &IID_IUnknown))
294         return E_NOINTERFACE;
295
296     *ppobj = NULL;
297     punk = This->pfnCreateInstance(pOuter, &hres);
298     if (!punk)
299     {
300         /* No object created, update error if it isn't done already and return */
301         if (SUCCEEDED(hres))
302             hres = E_OUTOFMEMORY;
303     return hres;
304     }
305
306     if (SUCCEEDED(hres))
307     {
308         hres = IUnknown_QueryInterface(punk, riid, ppobj);
309     }
310     /* Releasing the object. If everything was successful, QueryInterface
311        should have incremented the refcount once more, otherwise this will
312        purge the object. */
313     IUnknown_Release(punk);
314     return hres;
315 }
316
317 static HRESULT WINAPI DSCF_LockServer(IClassFactory *iface, BOOL dolock)
318 {
319     IClassFactoryImpl *This = impl_from_IClassFactory(iface);
320     TRACE("(%p)->(%d)\n",This, dolock);
321
322     if (dolock)
323         InterlockedIncrement(&server_locks);
324     else
325         InterlockedDecrement(&server_locks);
326     return S_OK;
327 }
328
329 static const IClassFactoryVtbl DSCF_Vtbl =
330 {
331     DSCF_QueryInterface,
332     DSCF_AddRef,
333     DSCF_Release,
334     DSCF_CreateInstance,
335     DSCF_LockServer
336 };
337
338 /***********************************************************************
339  *    DllGetClassObject
340  */
341 HRESULT WINAPI STRMBASE_DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
342 {
343     const FactoryTemplate *pList = g_Templates;
344     IClassFactoryImpl *factory;
345     int i;
346
347     TRACE("(%s,%s,%p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
348
349     if (!ppv)
350         return E_POINTER;
351
352     *ppv = NULL;
353
354     if (!IsEqualGUID(&IID_IClassFactory, riid) &&
355         !IsEqualGUID(&IID_IUnknown, riid))
356         return E_NOINTERFACE;
357
358     for (i = 0; i < g_cTemplates; i++, pList++)
359     {
360         if (IsEqualGUID(pList->m_ClsID, rclsid))
361             break;
362     }
363
364     if (i == g_cTemplates)
365     {
366         char dllname[MAX_PATH];
367         if (!GetModuleFileNameA(g_hInst, dllname, sizeof(dllname)))
368             strcpy(dllname, "???");
369         ERR("%s: no class found in %s.\n", debugstr_guid(rclsid), dllname);
370         return CLASS_E_CLASSNOTAVAILABLE;
371     }
372     else if (!pList->m_lpfnNew)
373     {
374         FIXME("%s: class not implemented yet.\n", debugstr_guid(rclsid));
375         return CLASS_E_CLASSNOTAVAILABLE;
376     }
377
378     factory = HeapAlloc(GetProcessHeap(), 0, sizeof(IClassFactoryImpl));
379     if (!factory)
380         return E_OUTOFMEMORY;
381
382     factory->IClassFactory_iface.lpVtbl = &DSCF_Vtbl;
383     factory->ref = 1;
384
385     factory->pfnCreateInstance = pList->m_lpfnNew;
386
387     *ppv = &factory->IClassFactory_iface;
388     return S_OK;
389 }
390
391 /***********************************************************************
392  *    DllCanUnloadNow
393  */
394 HRESULT WINAPI STRMBASE_DllCanUnloadNow(void)
395 {
396     TRACE("\n");
397
398     if (server_locks == 0)
399         return S_OK;
400     return S_FALSE;
401 }