Synchronize with trunk r58528.
[reactos.git] / dll / win32 / 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 #define WIN32_NO_STATUS
20 #define _INC_WINDOWS
21 #define COM_NO_WINDOWS_H
22
23 #include <stdarg.h>
24
25 #define COBJMACROS
26
27 #include <windef.h>
28 #include <winbase.h>
29 #include <wingdi.h>
30 //#include "winuser.h"
31 //#include "winerror.h"
32 #include <ole2.h>
33 #include <rpcproxy.h>
34
35 #include <initguid.h>
36 #include <vfw.h>
37 #include "avifile_private.h"
38
39 #include <wine/debug.h>
40
41 WINE_DEFAULT_DEBUG_CHANNEL(avifile);
42
43 HMODULE AVIFILE_hModule = NULL;
44
45 static BOOL AVIFILE_bLocked;
46 static UINT AVIFILE_uUseCount;
47
48 static HRESULT WINAPI IClassFactory_fnQueryInterface(LPCLASSFACTORY iface,REFIID riid,LPVOID *ppobj);
49 static ULONG WINAPI IClassFactory_fnAddRef(LPCLASSFACTORY iface);
50 static ULONG WINAPI IClassFactory_fnRelease(LPCLASSFACTORY iface);
51 static HRESULT WINAPI IClassFactory_fnCreateInstance(LPCLASSFACTORY iface,LPUNKNOWN pOuter,REFIID riid,LPVOID *ppobj);
52 static HRESULT WINAPI IClassFactory_fnLockServer(LPCLASSFACTORY iface,BOOL dolock);
53
54 static const IClassFactoryVtbl iclassfact = {
55 IClassFactory_fnQueryInterface,
56 IClassFactory_fnAddRef,
57 IClassFactory_fnRelease,
58 IClassFactory_fnCreateInstance,
59 IClassFactory_fnLockServer
60 };
61
62 typedef struct
63 {
64 /* IUnknown fields */
65 IClassFactory IClassFactory_iface;
66 DWORD dwRef;
67
68 CLSID clsid;
69 } IClassFactoryImpl;
70
71 static inline IClassFactoryImpl *impl_from_IClassFactory(IClassFactory *iface)
72 {
73 return CONTAINING_RECORD(iface, IClassFactoryImpl, IClassFactory_iface);
74 }
75
76 static HRESULT AVIFILE_CreateClassFactory(const CLSID *pclsid, const IID *riid,
77 LPVOID *ppv)
78 {
79 IClassFactoryImpl *pClassFactory = NULL;
80 HRESULT hr;
81
82 *ppv = NULL;
83
84 pClassFactory = HeapAlloc(GetProcessHeap(), 0, sizeof(*pClassFactory));
85 if (pClassFactory == NULL)
86 return E_OUTOFMEMORY;
87
88 pClassFactory->IClassFactory_iface.lpVtbl = &iclassfact;
89 pClassFactory->dwRef = 0;
90 pClassFactory->clsid = *pclsid;
91
92 hr = IClassFactory_QueryInterface(&pClassFactory->IClassFactory_iface, riid, ppv);
93 if (FAILED(hr)) {
94 HeapFree(GetProcessHeap(), 0, pClassFactory);
95 *ppv = NULL;
96 }
97
98 return hr;
99 }
100
101 static HRESULT WINAPI IClassFactory_fnQueryInterface(LPCLASSFACTORY iface,
102 REFIID riid,LPVOID *ppobj)
103 {
104 TRACE("(%p,%p,%p)\n", iface, riid, ppobj);
105
106 if ((IsEqualGUID(&IID_IUnknown, riid)) ||
107 (IsEqualGUID(&IID_IClassFactory, riid))) {
108 *ppobj = iface;
109 IClassFactory_AddRef(iface);
110 return S_OK;
111 }
112
113 return E_NOINTERFACE;
114 }
115
116 static ULONG WINAPI IClassFactory_fnAddRef(LPCLASSFACTORY iface)
117 {
118 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
119
120 TRACE("(%p)\n", iface);
121
122 return ++(This->dwRef);
123 }
124
125 static ULONG WINAPI IClassFactory_fnRelease(LPCLASSFACTORY iface)
126 {
127 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
128
129 TRACE("(%p)\n", iface);
130 if ((--(This->dwRef)) > 0)
131 return This->dwRef;
132
133 HeapFree(GetProcessHeap(), 0, This);
134
135 return 0;
136 }
137
138 static HRESULT WINAPI IClassFactory_fnCreateInstance(LPCLASSFACTORY iface,
139 LPUNKNOWN pOuter,
140 REFIID riid,LPVOID *ppobj)
141 {
142 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
143
144 TRACE("(%p,%p,%s,%p)\n", iface, pOuter, debugstr_guid(riid),
145 ppobj);
146
147 if (ppobj == NULL || pOuter != NULL)
148 return E_FAIL;
149 *ppobj = NULL;
150
151 if (IsEqualGUID(&CLSID_AVIFile, &This->clsid))
152 return AVIFILE_CreateAVIFile(riid,ppobj);
153 if (IsEqualGUID(&CLSID_ICMStream, &This->clsid))
154 return AVIFILE_CreateICMStream(riid,ppobj);
155 if (IsEqualGUID(&CLSID_WAVFile, &This->clsid))
156 return AVIFILE_CreateWAVFile(riid,ppobj);
157 if (IsEqualGUID(&CLSID_ACMStream, &This->clsid))
158 return AVIFILE_CreateACMStream(riid,ppobj);
159
160 return E_NOINTERFACE;
161 }
162
163 static HRESULT WINAPI IClassFactory_fnLockServer(LPCLASSFACTORY iface,BOOL dolock)
164 {
165 TRACE("(%p,%d)\n",iface,dolock);
166
167 AVIFILE_bLocked = dolock;
168
169 return S_OK;
170 }
171
172 LPCWSTR AVIFILE_BasenameW(LPCWSTR szPath)
173 {
174 #define SLASH(w) ((w) == '/' || (w) == '\\')
175
176 LPCWSTR szCur;
177
178 for (szCur = szPath + lstrlenW(szPath);
179 szCur > szPath && !SLASH(*szCur) && *szCur != ':';)
180 szCur--;
181
182 if (szCur == szPath)
183 return szCur;
184 else
185 return szCur + 1;
186
187 #undef SLASH
188 }
189
190 /***********************************************************************
191 * DllGetClassObject (AVIFIL32.@)
192 */
193 HRESULT WINAPI DllGetClassObject(REFCLSID pclsid, REFIID piid, LPVOID *ppv)
194 {
195 TRACE("(%s,%s,%p)\n", debugstr_guid(pclsid), debugstr_guid(piid), ppv);
196
197 if (pclsid == NULL || piid == NULL || ppv == NULL)
198 return E_FAIL;
199
200 return AVIFILE_CreateClassFactory(pclsid,piid,ppv);
201 }
202
203 /*****************************************************************************
204 * DllCanUnloadNow (AVIFIL32.@)
205 */
206 HRESULT WINAPI DllCanUnloadNow(void)
207 {
208 return ((AVIFILE_bLocked || AVIFILE_uUseCount) ? S_FALSE : S_OK);
209 }
210
211 /*****************************************************************************
212 * DllMain [AVIFIL32.init]
213 */
214 BOOL WINAPI DllMain(HINSTANCE hInstDll, DWORD fdwReason, LPVOID lpvReserved)
215 {
216 TRACE("(%p,%d,%p)\n", hInstDll, fdwReason, lpvReserved);
217
218 switch (fdwReason) {
219 case DLL_PROCESS_ATTACH:
220 DisableThreadLibraryCalls(hInstDll);
221 AVIFILE_hModule = hInstDll;
222 break;
223 case DLL_PROCESS_DETACH:
224 break;
225 };
226
227 return TRUE;
228 }
229
230 /***********************************************************************
231 * DllRegisterServer (AVIFIL32.@)
232 */
233 HRESULT WINAPI DllRegisterServer(void)
234 {
235 return __wine_register_resources( AVIFILE_hModule );
236 }
237
238 /***********************************************************************
239 * DllUnregisterServer (AVIFIL32.@)
240 */
241 HRESULT WINAPI DllUnregisterServer(void)
242 {
243 return __wine_unregister_resources( AVIFILE_hModule );
244 }