dsound: Added missing LeaveCriticalSection (Coverity).
[wine] / dlls / avifil32 / factory.c
1 /*
2  * Copyright 2002 Michael Günnewig
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #include <stdarg.h>
20
21 #define COBJMACROS
22
23 #include "windef.h"
24 #include "winbase.h"
25 #include "wingdi.h"
26 #include "winuser.h"
27 #include "winerror.h"
28 #include "ole2.h"
29 #include "rpcproxy.h"
30
31 #include "initguid.h"
32 #include "vfw.h"
33 #include "avifile_private.h"
34
35 #include "wine/debug.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(avifile);
38
39 HMODULE AVIFILE_hModule   = NULL;
40
41 static BOOL    AVIFILE_bLocked;
42 static UINT    AVIFILE_uUseCount;
43
44 static HRESULT WINAPI IClassFactory_fnQueryInterface(LPCLASSFACTORY iface,REFIID riid,LPVOID *ppobj);
45 static ULONG   WINAPI IClassFactory_fnAddRef(LPCLASSFACTORY iface);
46 static ULONG   WINAPI IClassFactory_fnRelease(LPCLASSFACTORY iface);
47 static HRESULT WINAPI IClassFactory_fnCreateInstance(LPCLASSFACTORY iface,LPUNKNOWN pOuter,REFIID riid,LPVOID *ppobj);
48 static HRESULT WINAPI IClassFactory_fnLockServer(LPCLASSFACTORY iface,BOOL dolock);
49
50 static const IClassFactoryVtbl iclassfact = {
51   IClassFactory_fnQueryInterface,
52   IClassFactory_fnAddRef,
53   IClassFactory_fnRelease,
54   IClassFactory_fnCreateInstance,
55   IClassFactory_fnLockServer
56 };
57
58 typedef struct
59 {
60   /* IUnknown fields */
61   IClassFactory IClassFactory_iface;
62   DWORD  dwRef;
63
64   CLSID  clsid;
65 } IClassFactoryImpl;
66
67 static inline IClassFactoryImpl *impl_from_IClassFactory(IClassFactory *iface)
68 {
69   return CONTAINING_RECORD(iface, IClassFactoryImpl, IClassFactory_iface);
70 }
71
72 static HRESULT AVIFILE_CreateClassFactory(const CLSID *pclsid, const IID *riid,
73                                           LPVOID *ppv)
74 {
75   IClassFactoryImpl *pClassFactory = NULL;
76   HRESULT            hr;
77
78   *ppv = NULL;
79
80   pClassFactory = HeapAlloc(GetProcessHeap(), 0, sizeof(*pClassFactory));
81   if (pClassFactory == NULL)
82     return E_OUTOFMEMORY;
83
84   pClassFactory->IClassFactory_iface.lpVtbl = &iclassfact;
85   pClassFactory->dwRef     = 0;
86   pClassFactory->clsid     = *pclsid;
87
88   hr = IClassFactory_QueryInterface(&pClassFactory->IClassFactory_iface, riid, ppv);
89   if (FAILED(hr)) {
90     HeapFree(GetProcessHeap(), 0, pClassFactory);
91     *ppv = NULL;
92   }
93
94   return hr;
95 }
96
97 static HRESULT WINAPI IClassFactory_fnQueryInterface(LPCLASSFACTORY iface,
98                                                      REFIID riid,LPVOID *ppobj)
99 {
100   TRACE("(%p,%p,%p)\n", iface, riid, ppobj);
101
102   if ((IsEqualGUID(&IID_IUnknown, riid)) ||
103       (IsEqualGUID(&IID_IClassFactory, riid))) {
104     *ppobj = iface;
105     IClassFactory_AddRef(iface);
106     return S_OK;
107   }
108
109   return E_NOINTERFACE;
110 }
111
112 static ULONG WINAPI IClassFactory_fnAddRef(LPCLASSFACTORY iface)
113 {
114   IClassFactoryImpl *This = impl_from_IClassFactory(iface);
115
116   TRACE("(%p)\n", iface);
117
118   return ++(This->dwRef);
119 }
120
121 static ULONG WINAPI IClassFactory_fnRelease(LPCLASSFACTORY iface)
122 {
123   IClassFactoryImpl *This = impl_from_IClassFactory(iface);
124
125   TRACE("(%p)\n", iface);
126   if ((--(This->dwRef)) > 0)
127     return This->dwRef;
128
129   HeapFree(GetProcessHeap(), 0, This);
130
131   return 0;
132 }
133
134 static HRESULT WINAPI IClassFactory_fnCreateInstance(LPCLASSFACTORY iface,
135                                                      LPUNKNOWN pOuter,
136                                                      REFIID riid,LPVOID *ppobj)
137 {
138   IClassFactoryImpl *This = impl_from_IClassFactory(iface);
139
140   TRACE("(%p,%p,%s,%p)\n", iface, pOuter, debugstr_guid(riid),
141         ppobj);
142
143   if (ppobj == NULL || pOuter != NULL)
144     return E_FAIL;
145   *ppobj = NULL;
146
147   if (IsEqualGUID(&CLSID_AVIFile, &This->clsid))
148     return AVIFILE_CreateAVIFile(riid,ppobj);
149   if (IsEqualGUID(&CLSID_ICMStream, &This->clsid))
150     return AVIFILE_CreateICMStream(riid,ppobj);
151   if (IsEqualGUID(&CLSID_WAVFile, &This->clsid))
152     return AVIFILE_CreateWAVFile(riid,ppobj);
153   if (IsEqualGUID(&CLSID_ACMStream, &This->clsid))
154     return AVIFILE_CreateACMStream(riid,ppobj);
155
156   return E_NOINTERFACE;
157 }
158
159 static HRESULT WINAPI IClassFactory_fnLockServer(LPCLASSFACTORY iface,BOOL dolock)
160 {
161   TRACE("(%p,%d)\n",iface,dolock);
162
163   AVIFILE_bLocked = dolock;
164
165   return S_OK;
166 }
167
168 LPCWSTR AVIFILE_BasenameW(LPCWSTR szPath)
169 {
170 #define SLASH(w) ((w) == '/' || (w) == '\\')
171
172   LPCWSTR szCur;
173
174   for (szCur = szPath + lstrlenW(szPath);
175        szCur > szPath && !SLASH(*szCur) && *szCur != ':';)
176     szCur--;
177
178   if (szCur == szPath)
179     return szCur;
180   else
181     return szCur + 1;
182
183 #undef SLASH
184 }
185
186 /***********************************************************************
187  *              DllGetClassObject (AVIFIL32.@)
188  */
189 HRESULT WINAPI DllGetClassObject(REFCLSID pclsid, REFIID piid, LPVOID *ppv)
190 {
191   TRACE("(%s,%s,%p)\n", debugstr_guid(pclsid), debugstr_guid(piid), ppv);
192
193   if (pclsid == NULL || piid == NULL || ppv == NULL)
194     return E_FAIL;
195
196   return AVIFILE_CreateClassFactory(pclsid,piid,ppv);
197 }
198
199 /*****************************************************************************
200  *              DllCanUnloadNow         (AVIFIL32.@)
201  */
202 HRESULT WINAPI DllCanUnloadNow(void)
203 {
204   return ((AVIFILE_bLocked || AVIFILE_uUseCount) ? S_FALSE : S_OK);
205 }
206
207 /*****************************************************************************
208  *              DllMain         [AVIFIL32.init]
209  */
210 BOOL WINAPI DllMain(HINSTANCE hInstDll, DWORD fdwReason, LPVOID lpvReserved)
211 {
212   TRACE("(%p,%d,%p)\n", hInstDll, fdwReason, lpvReserved);
213
214   switch (fdwReason) {
215   case DLL_PROCESS_ATTACH:
216     DisableThreadLibraryCalls(hInstDll);
217     AVIFILE_hModule = hInstDll;
218     break;
219   case DLL_PROCESS_DETACH:
220     break;
221   };
222
223   return TRUE;
224 }
225
226 /***********************************************************************
227  *              DllRegisterServer (AVIFIL32.@)
228  */
229 HRESULT WINAPI DllRegisterServer(void)
230 {
231     return __wine_register_resources( AVIFILE_hModule );
232 }
233
234 /***********************************************************************
235  *              DllUnregisterServer (AVIFIL32.@)
236  */
237 HRESULT WINAPI DllUnregisterServer(void)
238 {
239     return __wine_unregister_resources( AVIFILE_hModule );
240 }