msi: Simplify ready_media.
[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
29 #include "ole2.h"
30 #include "vfw.h"
31
32 #include "wine/debug.h"
33
34 WINE_DEFAULT_DEBUG_CHANNEL(avifile);
35
36 #include "initguid.h"
37 #include "avifile_private.h"
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   const IClassFactoryVtbl *lpVtbl;
62   DWORD  dwRef;
63
64   CLSID  clsid;
65 } IClassFactoryImpl;
66
67 static HRESULT AVIFILE_CreateClassFactory(const CLSID *pclsid, const IID *riid,
68                                           LPVOID *ppv)
69 {
70   IClassFactoryImpl *pClassFactory = NULL;
71   HRESULT            hr;
72
73   *ppv = NULL;
74
75   pClassFactory = HeapAlloc(GetProcessHeap(), 0, sizeof(*pClassFactory));
76   if (pClassFactory == NULL)
77     return E_OUTOFMEMORY;
78
79   pClassFactory->lpVtbl    = &iclassfact;
80   pClassFactory->dwRef     = 0;
81   memcpy(&pClassFactory->clsid, pclsid, sizeof(pClassFactory->clsid));
82
83   hr = IClassFactory_QueryInterface((IClassFactory*)pClassFactory, riid, ppv);
84   if (FAILED(hr)) {
85     HeapFree(GetProcessHeap(), 0, pClassFactory);
86     *ppv = NULL;
87   }
88
89   return hr;
90 }
91
92 static HRESULT WINAPI IClassFactory_fnQueryInterface(LPCLASSFACTORY iface,
93                                                      REFIID riid,LPVOID *ppobj)
94 {
95   TRACE("(%p,%p,%p)\n", iface, riid, ppobj);
96
97   if ((IsEqualGUID(&IID_IUnknown, riid)) ||
98       (IsEqualGUID(&IID_IClassFactory, riid))) {
99     *ppobj = iface;
100     IClassFactory_AddRef(iface);
101     return S_OK;
102   }
103
104   return E_NOINTERFACE;
105 }
106
107 static ULONG WINAPI IClassFactory_fnAddRef(LPCLASSFACTORY iface)
108 {
109   IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
110
111   TRACE("(%p)\n", iface);
112
113   return ++(This->dwRef);
114 }
115
116 static ULONG WINAPI IClassFactory_fnRelease(LPCLASSFACTORY iface)
117 {
118   IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
119
120   TRACE("(%p)\n", iface);
121   if ((--(This->dwRef)) > 0)
122     return This->dwRef;
123
124   HeapFree(GetProcessHeap(), 0, This);
125
126   return 0;
127 }
128
129 static HRESULT WINAPI IClassFactory_fnCreateInstance(LPCLASSFACTORY iface,
130                                                      LPUNKNOWN pOuter,
131                                                      REFIID riid,LPVOID *ppobj)
132 {
133   IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
134
135   TRACE("(%p,%p,%s,%p)\n", iface, pOuter, debugstr_guid(riid),
136         ppobj);
137
138   if (ppobj == NULL || pOuter != NULL)
139     return E_FAIL;
140   *ppobj = NULL;
141
142   if (IsEqualGUID(&CLSID_AVIFile, &This->clsid))
143     return AVIFILE_CreateAVIFile(riid,ppobj);
144   if (IsEqualGUID(&CLSID_ICMStream, &This->clsid))
145     return AVIFILE_CreateICMStream(riid,ppobj);
146   if (IsEqualGUID(&CLSID_WAVFile, &This->clsid))
147     return AVIFILE_CreateWAVFile(riid,ppobj);
148   if (IsEqualGUID(&CLSID_ACMStream, &This->clsid))
149     return AVIFILE_CreateACMStream(riid,ppobj);
150
151   return E_NOINTERFACE;
152 }
153
154 static HRESULT WINAPI IClassFactory_fnLockServer(LPCLASSFACTORY iface,BOOL dolock)
155 {
156   TRACE("(%p,%d)\n",iface,dolock);
157
158   AVIFILE_bLocked = dolock;
159
160   return S_OK;
161 }
162
163 LPCWSTR AVIFILE_BasenameW(LPCWSTR szPath)
164 {
165 #define SLASH(w) ((w) == '/' || (w) == '\\')
166
167   LPCWSTR szCur;
168
169   for (szCur = szPath + lstrlenW(szPath);
170        szCur > szPath && !SLASH(*szCur) && *szCur != ':';)
171     szCur--;
172
173   if (szCur == szPath)
174     return szCur;
175   else
176     return szCur + 1;
177
178 #undef SLASH
179 }
180
181 /***********************************************************************
182  *              DllGetClassObject (AVIFIL32.@)
183  */
184 HRESULT WINAPI DllGetClassObject(REFCLSID pclsid, REFIID piid, LPVOID *ppv)
185 {
186   TRACE("(%s,%s,%p)\n", debugstr_guid(pclsid), debugstr_guid(piid), ppv);
187
188   if (pclsid == NULL || piid == NULL || ppv == NULL)
189     return E_FAIL;
190
191   return AVIFILE_CreateClassFactory(pclsid,piid,ppv);
192 }
193
194 /*****************************************************************************
195  *              DllCanUnloadNow         (AVIFIL32.@)
196  */
197 HRESULT WINAPI DllCanUnloadNow(void)
198 {
199   return ((AVIFILE_bLocked || AVIFILE_uUseCount) ? S_FALSE : S_OK);
200 }
201
202 /*****************************************************************************
203  *              DllMain         [AVIFIL32.init]
204  */
205 BOOL WINAPI DllMain(HINSTANCE hInstDll, DWORD fdwReason, LPVOID lpvReserved)
206 {
207   TRACE("(%p,%d,%p)\n", hInstDll, fdwReason, lpvReserved);
208
209   switch (fdwReason) {
210   case DLL_PROCESS_ATTACH:
211     DisableThreadLibraryCalls(hInstDll);
212     AVIFILE_hModule = (HMODULE)hInstDll;
213     break;
214   case DLL_PROCESS_DETACH:
215     break;
216   };
217
218   return TRUE;
219 }