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