ole32: Create moniker enumerator with an initial reference count.
[wine/multimedia.git] / dlls / avifil32 / factory.c
blobc1fb3b302b2349a51354063fb640e17d9c4c9fc1
1 /*
2 * Copyright 2002 Michael Günnewig
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.
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.
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
19 #include <assert.h>
20 #include <stdarg.h>
22 #define COBJMACROS
23 #define COM_NO_WINDOWS_H
25 #include "windef.h"
26 #include "winbase.h"
27 #include "wingdi.h"
28 #include "winuser.h"
29 #include "winnls.h"
30 #include "winerror.h"
32 #include "ole2.h"
33 #include "vfw.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(avifile);
39 #include "initguid.h"
40 #include "avifile_private.h"
42 HMODULE AVIFILE_hModule = NULL;
44 BOOL AVIFILE_bLocked = FALSE;
45 UINT AVIFILE_uUseCount = 0;
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);
53 static const IClassFactoryVtbl iclassfact = {
54 IClassFactory_fnQueryInterface,
55 IClassFactory_fnAddRef,
56 IClassFactory_fnRelease,
57 IClassFactory_fnCreateInstance,
58 IClassFactory_fnLockServer
61 typedef struct
63 /* IUnknown fields */
64 const IClassFactoryVtbl *lpVtbl;
65 DWORD dwRef;
67 CLSID clsid;
68 } IClassFactoryImpl;
70 static HRESULT AVIFILE_CreateClassFactory(const CLSID *pclsid, const IID *riid,
71 LPVOID *ppv)
73 IClassFactoryImpl *pClassFactory = NULL;
74 HRESULT hr;
76 *ppv = NULL;
78 pClassFactory = HeapAlloc(GetProcessHeap(), 0, sizeof(*pClassFactory));
79 if (pClassFactory == NULL)
80 return E_OUTOFMEMORY;
82 pClassFactory->lpVtbl = &iclassfact;
83 pClassFactory->dwRef = 0;
84 memcpy(&pClassFactory->clsid, pclsid, sizeof(pClassFactory->clsid));
86 hr = IClassFactory_QueryInterface((IClassFactory*)pClassFactory, riid, ppv);
87 if (FAILED(hr)) {
88 HeapFree(GetProcessHeap(), 0, pClassFactory);
89 *ppv = NULL;
92 return hr;
95 static HRESULT WINAPI IClassFactory_fnQueryInterface(LPCLASSFACTORY iface,
96 REFIID riid,LPVOID *ppobj)
98 TRACE("(%p,%p,%p)\n", iface, riid, ppobj);
100 if ((IsEqualGUID(&IID_IUnknown, riid)) ||
101 (IsEqualGUID(&IID_IClassFactory, riid))) {
102 *ppobj = iface;
103 IClassFactory_AddRef(iface);
104 return S_OK;
107 return E_NOINTERFACE;
110 static ULONG WINAPI IClassFactory_fnAddRef(LPCLASSFACTORY iface)
112 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
114 TRACE("(%p)\n", iface);
116 return ++(This->dwRef);
119 static ULONG WINAPI IClassFactory_fnRelease(LPCLASSFACTORY iface)
121 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
123 TRACE("(%p)\n", iface);
124 if ((--(This->dwRef)) > 0)
125 return This->dwRef;
127 HeapFree(GetProcessHeap(), 0, This);
129 return 0;
132 static HRESULT WINAPI IClassFactory_fnCreateInstance(LPCLASSFACTORY iface,
133 LPUNKNOWN pOuter,
134 REFIID riid,LPVOID *ppobj)
136 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
138 TRACE("(%p,%p,%s,%p)\n", iface, pOuter, debugstr_guid(riid),
139 ppobj);
141 if (ppobj == NULL || pOuter != NULL)
142 return E_FAIL;
143 *ppobj = NULL;
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);
154 return E_NOINTERFACE;
157 static HRESULT WINAPI IClassFactory_fnLockServer(LPCLASSFACTORY iface,BOOL dolock)
159 TRACE("(%p,%d)\n",iface,dolock);
161 AVIFILE_bLocked = dolock;
163 return S_OK;
166 LPCWSTR AVIFILE_BasenameW(LPCWSTR szPath)
168 #define SLASH(w) ((w) == '/' || (w) == '\\')
170 LPCWSTR szCur;
172 for (szCur = szPath + lstrlenW(szPath);
173 szCur > szPath && !SLASH(*szCur) && *szCur != ':';)
174 szCur--;
176 if (szCur == szPath)
177 return szCur;
178 else
179 return szCur + 1;
181 #undef SLASH
184 /***********************************************************************
185 * DllGetClassObject (AVIFIL32.@)
187 HRESULT WINAPI DllGetClassObject(REFCLSID pclsid, REFIID piid, LPVOID *ppv)
189 TRACE("(%s,%s,%p)\n", debugstr_guid(pclsid), debugstr_guid(piid), ppv);
191 if (pclsid == NULL || piid == NULL || ppv == NULL)
192 return E_FAIL;
194 return AVIFILE_CreateClassFactory(pclsid,piid,ppv);
197 /*****************************************************************************
198 * DllCanUnloadNow (AVIFIL32.@)
200 HRESULT WINAPI DllCanUnloadNow(void)
202 return ((AVIFILE_bLocked || AVIFILE_uUseCount) ? S_FALSE : S_OK);
205 /*****************************************************************************
206 * DllMain [AVIFIL32.init]
208 BOOL WINAPI DllMain(HINSTANCE hInstDll, DWORD fdwReason, LPVOID lpvReserved)
210 TRACE("(%p,%ld,%p)\n", hInstDll, fdwReason, lpvReserved);
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;
221 return TRUE;