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