dmloader: Merge and simplify the IClassFactory implementations.
[wine/multimedia.git] / dlls / dmloader / dmloader_main.c
blob80e115e7f665da823e19076a8f75e6a8c592b852
1 /* DirectMusicLoader Main
3 * Copyright (C) 2003-2004 Rok Mandeljc
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include "dmloader_private.h"
21 #include "rpcproxy.h"
23 WINE_DEFAULT_DEBUG_CHANNEL(dmloader);
25 static HINSTANCE instance;
26 LONG dwDirectMusicContainer = 0;
27 LONG dwDirectMusicLoader = 0;
29 typedef struct {
30 IClassFactory IClassFactory_iface;
31 HRESULT WINAPI (*fnCreateInstance)(REFIID riid, void **ppv, IUnknown *pUnkOuter);
32 } IClassFactoryImpl;
34 /******************************************************************
35 * IClassFactory implementation
37 static inline IClassFactoryImpl *impl_from_IClassFactory(IClassFactory *iface)
39 return CONTAINING_RECORD(iface, IClassFactoryImpl, IClassFactory_iface);
42 static HRESULT WINAPI ClassFactory_QueryInterface(IClassFactory *iface, REFIID riid, void **ppv)
44 if (ppv == NULL)
45 return E_POINTER;
47 if (IsEqualGUID(&IID_IUnknown, riid))
48 TRACE("(%p)->(IID_IUnknown %p)\n", iface, ppv);
49 else if (IsEqualGUID(&IID_IClassFactory, riid))
50 TRACE("(%p)->(IID_IClassFactory %p)\n", iface, ppv);
51 else {
52 FIXME("(%p)->(%s %p)\n", iface, debugstr_guid(riid), ppv);
53 *ppv = NULL;
54 return E_NOINTERFACE;
57 *ppv = iface;
58 IClassFactory_AddRef(iface);
59 return S_OK;
62 static ULONG WINAPI ClassFactory_AddRef(IClassFactory *iface)
64 InterlockedIncrement(&dwDirectMusicLoader);
66 return 2; /* non-heap based object */
69 static ULONG WINAPI ClassFactory_Release(IClassFactory *iface)
71 InterlockedDecrement(&dwDirectMusicLoader);
73 return 1; /* non-heap based object */
76 static HRESULT WINAPI ClassFactory_CreateInstance(IClassFactory *iface, IUnknown *pUnkOuter,
77 REFIID riid, void **ppv)
79 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
81 TRACE ("(%p, %s, %p)\n", pUnkOuter, debugstr_dmguid(riid), ppv);
83 return This->fnCreateInstance(riid, ppv, pUnkOuter);
86 static HRESULT WINAPI ClassFactory_LockServer(IClassFactory *iface, BOOL dolock)
88 TRACE("(%d)\n", dolock);
90 if (dolock)
91 InterlockedIncrement(&dwDirectMusicLoader);
92 else
93 InterlockedDecrement(&dwDirectMusicLoader);
95 return S_OK;
98 static const IClassFactoryVtbl classfactory_vtbl = {
99 ClassFactory_QueryInterface,
100 ClassFactory_AddRef,
101 ClassFactory_Release,
102 ClassFactory_CreateInstance,
103 ClassFactory_LockServer
106 static IClassFactoryImpl dm_loader_CF = {{&classfactory_vtbl}, DMUSIC_CreateDirectMusicLoaderImpl};
107 static IClassFactoryImpl dm_container_CF = {{&classfactory_vtbl},
108 DMUSIC_CreateDirectMusicContainerImpl};
110 /******************************************************************
111 * DllMain
113 BOOL WINAPI DllMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {
114 if (fdwReason == DLL_PROCESS_ATTACH) {
115 instance = hinstDLL;
116 DisableThreadLibraryCalls(hinstDLL);
117 /* FIXME: Initialisation */
118 } else if (fdwReason == DLL_PROCESS_DETACH) {
119 /* FIXME: Cleanup */
121 return TRUE;
125 /******************************************************************
126 * DllCanUnloadNow (DMLOADER.@)
128 HRESULT WINAPI DllCanUnloadNow (void)
130 TRACE("(void)\n");
131 /* if there are no instances left, it's safe to release */
132 if (!dwDirectMusicContainer && !dwDirectMusicLoader)
133 return S_OK;
134 else
135 return S_FALSE;
139 /******************************************************************
140 * DllGetClassObject (DMLOADER.@)
142 HRESULT WINAPI DllGetClassObject (REFCLSID rclsid, REFIID riid, LPVOID *ppv)
144 TRACE("(%s, %s, %p)\n", debugstr_dmguid(rclsid), debugstr_dmguid(riid), ppv);
145 if (IsEqualCLSID (rclsid, &CLSID_DirectMusicLoader) && IsEqualIID (riid, &IID_IClassFactory)) {
146 IClassFactory_AddRef(&dm_loader_CF.IClassFactory_iface);
147 *ppv = &dm_loader_CF.IClassFactory_iface;
148 return S_OK;
149 } else if (IsEqualCLSID (rclsid, &CLSID_DirectMusicContainer) && IsEqualIID (riid, &IID_IClassFactory)) {
150 IClassFactory_AddRef(&dm_container_CF.IClassFactory_iface);
151 *ppv = &dm_container_CF.IClassFactory_iface;
152 return S_OK;
155 WARN(": no class found\n");
156 return CLASS_E_CLASSNOTAVAILABLE;
159 /***********************************************************************
160 * DllRegisterServer (DMLOADER.@)
162 HRESULT WINAPI DllRegisterServer(void)
164 return __wine_register_resources( instance, NULL );
167 /***********************************************************************
168 * DllUnregisterServer (DMLOADER.@)
170 HRESULT WINAPI DllUnregisterServer(void)
172 return __wine_unregister_resources( instance, NULL );