factory.c 6.27 KB
Newer Older
1
/*
2
 * Copyright 2002 Michael Günnewig
3 4 5 6 7 8 9 10 11 12 13 14 15
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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 22
#define COBJMACROS

23
#include "windef.h"
24
#include "winbase.h"
25
#include "wingdi.h"
26
#include "winuser.h"
27 28
#include "winerror.h"
#include "ole2.h"
29
#include "rpcproxy.h"
30 31

#include "initguid.h"
32
#include "vfw.h"
33
#include "avifile_private.h"
34 35 36 37 38

#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(avifile);

39
HMODULE AVIFILE_hModule   = NULL;
40

41 42
static BOOL    AVIFILE_bLocked;
static UINT    AVIFILE_uUseCount;
43 44 45 46 47 48 49

static HRESULT WINAPI IClassFactory_fnQueryInterface(LPCLASSFACTORY iface,REFIID riid,LPVOID *ppobj);
static ULONG   WINAPI IClassFactory_fnAddRef(LPCLASSFACTORY iface);
static ULONG   WINAPI IClassFactory_fnRelease(LPCLASSFACTORY iface);
static HRESULT WINAPI IClassFactory_fnCreateInstance(LPCLASSFACTORY iface,LPUNKNOWN pOuter,REFIID riid,LPVOID *ppobj);
static HRESULT WINAPI IClassFactory_fnLockServer(LPCLASSFACTORY iface,BOOL dolock);

50
static const IClassFactoryVtbl iclassfact = {
51 52 53 54 55 56 57 58 59 60
  IClassFactory_fnQueryInterface,
  IClassFactory_fnAddRef,
  IClassFactory_fnRelease,
  IClassFactory_fnCreateInstance,
  IClassFactory_fnLockServer
};

typedef struct
{
  /* IUnknown fields */
61
  IClassFactory IClassFactory_iface;
62 63 64 65 66
  DWORD	 dwRef;

  CLSID  clsid;
} IClassFactoryImpl;

67 68 69 70 71
static inline IClassFactoryImpl *impl_from_IClassFactory(IClassFactory *iface)
{
  return CONTAINING_RECORD(iface, IClassFactoryImpl, IClassFactory_iface);
}

72 73 74 75 76 77 78 79
static HRESULT AVIFILE_CreateClassFactory(const CLSID *pclsid, const IID *riid,
					  LPVOID *ppv)
{
  IClassFactoryImpl *pClassFactory = NULL;
  HRESULT            hr;

  *ppv = NULL;

80
  pClassFactory = HeapAlloc(GetProcessHeap(), 0, sizeof(*pClassFactory));
81 82 83
  if (pClassFactory == NULL)
    return E_OUTOFMEMORY;

84
  pClassFactory->IClassFactory_iface.lpVtbl = &iclassfact;
85
  pClassFactory->dwRef     = 0;
86
  pClassFactory->clsid     = *pclsid;
87

88
  hr = IClassFactory_QueryInterface(&pClassFactory->IClassFactory_iface, riid, ppv);
89
  if (FAILED(hr)) {
90
    HeapFree(GetProcessHeap(), 0, pClassFactory);
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
    *ppv = NULL;
  }

  return hr;
}

static HRESULT WINAPI IClassFactory_fnQueryInterface(LPCLASSFACTORY iface,
						     REFIID riid,LPVOID *ppobj)
{
  TRACE("(%p,%p,%p)\n", iface, riid, ppobj);

  if ((IsEqualGUID(&IID_IUnknown, riid)) ||
      (IsEqualGUID(&IID_IClassFactory, riid))) {
    *ppobj = iface;
    IClassFactory_AddRef(iface);
    return S_OK;
  }

  return E_NOINTERFACE;
}

static ULONG WINAPI IClassFactory_fnAddRef(LPCLASSFACTORY iface)
{
114
  IClassFactoryImpl *This = impl_from_IClassFactory(iface);
115 116 117 118 119 120 121 122

  TRACE("(%p)\n", iface);

  return ++(This->dwRef);
}

static ULONG WINAPI IClassFactory_fnRelease(LPCLASSFACTORY iface)
{
123
  IClassFactoryImpl *This = impl_from_IClassFactory(iface);
124 125 126 127 128

  TRACE("(%p)\n", iface);
  if ((--(This->dwRef)) > 0)
    return This->dwRef;

129 130
  HeapFree(GetProcessHeap(), 0, This);

131 132 133 134 135 136 137
  return 0;
}

static HRESULT WINAPI IClassFactory_fnCreateInstance(LPCLASSFACTORY iface,
						     LPUNKNOWN pOuter,
						     REFIID riid,LPVOID *ppobj)
{
138
  IClassFactoryImpl *This = impl_from_IClassFactory(iface);
139

140
  TRACE("(%p,%p,%s,%p)\n", iface, pOuter, debugstr_guid(riid),
141
	ppobj);
142

143 144
  if (!ppobj)
    return E_INVALIDARG;
145 146
  *ppobj = NULL;

147 148 149
  if (pOuter && !IsEqualGUID(&IID_IUnknown, riid))
    return E_INVALIDARG;

150
  if (IsEqualGUID(&CLSID_AVIFile, &This->clsid))
151
    return AVIFILE_CreateAVIFile(pOuter, riid, ppobj);
152 153
  if (IsEqualGUID(&CLSID_WAVFile, &This->clsid))
    return AVIFILE_CreateWAVFile(pOuter, riid, ppobj);
154 155 156 157

  if (pOuter)
    return CLASS_E_NOAGGREGATION;

158 159 160 161
  if (IsEqualGUID(&CLSID_ICMStream, &This->clsid))
    return AVIFILE_CreateICMStream(riid,ppobj);
  if (IsEqualGUID(&CLSID_ACMStream, &This->clsid))
    return AVIFILE_CreateACMStream(riid,ppobj);
162 163 164 165 166 167 168 169 170 171 172 173 174

  return E_NOINTERFACE;
}

static HRESULT WINAPI IClassFactory_fnLockServer(LPCLASSFACTORY iface,BOOL dolock)
{
  TRACE("(%p,%d)\n",iface,dolock);

  AVIFILE_bLocked = dolock;

  return S_OK;
}

175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
LPCWSTR AVIFILE_BasenameW(LPCWSTR szPath)
{
#define SLASH(w) ((w) == '/' || (w) == '\\')

  LPCWSTR szCur;

  for (szCur = szPath + lstrlenW(szPath);
       szCur > szPath && !SLASH(*szCur) && *szCur != ':';)
    szCur--;

  if (szCur == szPath)
    return szCur;
  else
    return szCur + 1;

#undef SLASH
}
192 193 194 195

/***********************************************************************
 *		DllGetClassObject (AVIFIL32.@)
 */
196
HRESULT WINAPI DllGetClassObject(REFCLSID pclsid, REFIID piid, LPVOID *ppv)
197 198 199 200 201 202 203 204 205 206 207 208
{
  TRACE("(%s,%s,%p)\n", debugstr_guid(pclsid), debugstr_guid(piid), ppv);

  if (pclsid == NULL || piid == NULL || ppv == NULL)
    return E_FAIL;

  return AVIFILE_CreateClassFactory(pclsid,piid,ppv);
}

/*****************************************************************************
 *		DllCanUnloadNow		(AVIFIL32.@)
 */
209
HRESULT WINAPI DllCanUnloadNow(void)
210 211 212 213 214
{
  return ((AVIFILE_bLocked || AVIFILE_uUseCount) ? S_FALSE : S_OK);
}

/*****************************************************************************
215
 *		DllMain		[AVIFIL32.init]
216
 */
217
BOOL WINAPI DllMain(HINSTANCE hInstDll, DWORD fdwReason, LPVOID lpvReserved)
218
{
219
  TRACE("(%p,%d,%p)\n", hInstDll, fdwReason, lpvReserved);
220 221 222

  switch (fdwReason) {
  case DLL_PROCESS_ATTACH:
223
    DisableThreadLibraryCalls(hInstDll);
224
    AVIFILE_hModule = hInstDll;
225 226 227 228 229
    break;
  };

  return TRUE;
}
230 231 232 233 234 235

/***********************************************************************
 *		DllRegisterServer (AVIFIL32.@)
 */
HRESULT WINAPI DllRegisterServer(void)
{
236
    return __wine_register_resources( AVIFILE_hModule );
237 238 239 240 241 242 243
}

/***********************************************************************
 *		DllUnregisterServer (AVIFIL32.@)
 */
HRESULT WINAPI DllUnregisterServer(void)
{
244
    return __wine_unregister_resources( AVIFILE_hModule );
245
}