urlmon: Use ifaces instead of vtbl pointers in MimeFilter.
[wine] / dlls / amstream / main.c
1 /*
2  *     MultiMedia Streams Base Functions (AMSTREAM.DLL)
3  *
4  * Copyright 2004 Christian Costa
5  *
6  * This file contains the (internal) driver registration functions,
7  * driver enumeration APIs and DirectDraw creation functions.
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23
24 #include <stdarg.h>
25 #include <string.h>
26
27 #define COBJMACROS
28
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winuser.h"
32 #include "winerror.h"
33
34 #include "ole2.h"
35 #include "rpcproxy.h"
36
37 #include "amstream_private.h"
38 #include "amstream.h"
39
40 #include "wine/debug.h"
41
42 WINE_DEFAULT_DEBUG_CHANNEL(amstream);
43
44 static HINSTANCE instance;
45 static DWORD dll_ref = 0;
46
47 /* For the moment, do nothing here. */
48 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpv)
49 {
50     switch(fdwReason) {
51         case DLL_PROCESS_ATTACH:
52             instance = hInstDLL;
53             DisableThreadLibraryCalls(hInstDLL);
54             break;
55         case DLL_PROCESS_DETACH:
56             break;
57     }
58     return TRUE;
59 }
60
61 /******************************************************************************
62  * Multimedia Streams ClassFactory
63  */
64 typedef struct {
65     IClassFactory ITF_IClassFactory;
66
67     LONG ref;
68     HRESULT (*pfnCreateInstance)(IUnknown *pUnkOuter, LPVOID *ppObj);
69 } IClassFactoryImpl;
70
71 struct object_creation_info
72 {
73     const CLSID *clsid;
74     HRESULT (*pfnCreateInstance)(IUnknown *pUnkOuter, LPVOID *ppObj);
75 };
76
77 static const struct object_creation_info object_creation[] =
78 {
79     { &CLSID_AMMultiMediaStream, AM_create },
80     { &CLSID_AMDirectDrawStream, AM_create },
81     { &CLSID_MediaStreamFilter, MediaStreamFilter_create }
82 };
83
84 static HRESULT WINAPI
85 AMCF_QueryInterface(LPCLASSFACTORY iface,REFIID riid,LPVOID *ppobj)
86 {
87     IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
88
89     if (IsEqualGUID(riid, &IID_IUnknown)
90         || IsEqualGUID(riid, &IID_IClassFactory))
91     {
92         IClassFactory_AddRef(iface);
93         *ppobj = This;
94         return S_OK;
95     }
96
97     WARN("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),ppobj);
98     return E_NOINTERFACE;
99 }
100
101 static ULONG WINAPI AMCF_AddRef(LPCLASSFACTORY iface)
102 {
103     IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
104     return InterlockedIncrement(&This->ref);
105 }
106
107 static ULONG WINAPI AMCF_Release(LPCLASSFACTORY iface)
108 {
109     IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
110
111     ULONG ref = InterlockedDecrement(&This->ref);
112
113     if (ref == 0)
114         HeapFree(GetProcessHeap(), 0, This);
115
116     return ref;
117 }
118
119
120 static HRESULT WINAPI AMCF_CreateInstance(LPCLASSFACTORY iface, LPUNKNOWN pOuter,
121                                           REFIID riid, LPVOID *ppobj)
122 {
123     IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
124     HRESULT hres;
125     LPUNKNOWN punk;
126     
127     TRACE("(%p)->(%p,%s,%p)\n",This,pOuter,debugstr_guid(riid),ppobj);
128
129     *ppobj = NULL;
130     hres = This->pfnCreateInstance(pOuter, (LPVOID *) &punk);
131     if (SUCCEEDED(hres)) {
132         hres = IUnknown_QueryInterface(punk, riid, ppobj);
133         IUnknown_Release(punk);
134     }
135     return hres;
136 }
137
138 static HRESULT WINAPI AMCF_LockServer(LPCLASSFACTORY iface,BOOL dolock)
139 {
140     IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
141     FIXME("(%p)->(%d),stub!\n",This,dolock);
142     return S_OK;
143 }
144
145 static const IClassFactoryVtbl DSCF_Vtbl =
146 {
147     AMCF_QueryInterface,
148     AMCF_AddRef,
149     AMCF_Release,
150     AMCF_CreateInstance,
151     AMCF_LockServer
152 };
153
154 /*******************************************************************************
155  * DllGetClassObject [AMSTREAM.@]
156  * Retrieves class object from a DLL object
157  *
158  * NOTES
159  *    Docs say returns STDAPI
160  *
161  * PARAMS
162  *    rclsid [I] CLSID for the class object
163  *    riid   [I] Reference to identifier of interface for class object
164  *    ppv    [O] Address of variable to receive interface pointer for riid
165  *
166  * RETURNS
167  *    Success: S_OK
168  *    Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
169  *             E_UNEXPECTED
170  */
171 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
172 {
173     unsigned int i;
174     IClassFactoryImpl *factory;
175     
176     TRACE("(%s,%s,%p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
177     
178     if ( !IsEqualGUID( &IID_IClassFactory, riid )
179          && ! IsEqualGUID( &IID_IUnknown, riid) )
180         return E_NOINTERFACE;
181
182     for (i=0; i < sizeof(object_creation)/sizeof(object_creation[0]); i++)
183     {
184         if (IsEqualGUID(object_creation[i].clsid, rclsid))
185             break;
186     }
187
188     if (i == sizeof(object_creation)/sizeof(object_creation[0]))
189     {
190         FIXME("%s: no class found.\n", debugstr_guid(rclsid));
191         return CLASS_E_CLASSNOTAVAILABLE;
192     }
193
194     factory = HeapAlloc(GetProcessHeap(), 0, sizeof(*factory));
195     if (factory == NULL) return E_OUTOFMEMORY;
196
197     factory->ITF_IClassFactory.lpVtbl = &DSCF_Vtbl;
198     factory->ref = 1;
199
200     factory->pfnCreateInstance = object_creation[i].pfnCreateInstance;
201
202     *ppv = &(factory->ITF_IClassFactory);
203     return S_OK;
204 }
205
206 /***********************************************************************
207  *              DllCanUnloadNow (AMSTREAM.@)
208  */
209 HRESULT WINAPI DllCanUnloadNow(void)
210 {
211     return dll_ref != 0 ? S_FALSE : S_OK;
212 }
213
214 /***********************************************************************
215  *              DllRegisterServer (AMSTREAM.@)
216  */
217 HRESULT WINAPI DllRegisterServer(void)
218 {
219     return __wine_register_resources( instance, NULL );
220 }
221
222 /***********************************************************************
223  *              DllUnregisterServer (AMSTREAM.@)
224  */
225 HRESULT WINAPI DllUnregisterServer(void)
226 {
227     return __wine_unregister_resources( instance, NULL );
228 }