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