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