Release 1.5.29.
[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     }
55     return TRUE;
56 }
57
58 /******************************************************************************
59  * DirectX File ClassFactory
60  */
61 typedef struct {
62     IClassFactory IClassFactory_iface;
63
64     LONG ref;
65     HRESULT (*pfnCreateInstance)(IUnknown *pUnkOuter, LPVOID *ppObj);
66 } IClassFactoryImpl;
67
68 static inline IClassFactoryImpl *impl_from_IClassFactory(IClassFactory *iface)
69 {
70     return CONTAINING_RECORD(iface, IClassFactoryImpl, IClassFactory_iface);
71 }
72
73 struct object_creation_info
74 {
75     const CLSID *clsid;
76     HRESULT (*pfnCreateInstance)(IUnknown *pUnkOuter, LPVOID *ppObj);
77 };
78
79 static const struct object_creation_info object_creation[] =
80 {
81     { &CLSID_CDirectXFile, IDirectXFileImpl_Create },
82 };
83
84 static HRESULT WINAPI XFCF_QueryInterface(LPCLASSFACTORY iface,REFIID riid, LPVOID *ppobj)
85 {
86     IClassFactoryImpl *This = impl_from_IClassFactory(iface);
87
88     if (IsEqualGUID(riid, &IID_IUnknown)
89         || IsEqualGUID(riid, &IID_IClassFactory))
90     {
91         IClassFactory_AddRef(iface);
92         *ppobj = &This->IClassFactory_iface;
93         return S_OK;
94     }
95
96     WARN("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),ppobj);
97     return E_NOINTERFACE;
98 }
99
100 static ULONG WINAPI XFCF_AddRef(LPCLASSFACTORY iface)
101 {
102     IClassFactoryImpl *This = impl_from_IClassFactory(iface);
103     return InterlockedIncrement(&This->ref);
104 }
105
106 static ULONG WINAPI XFCF_Release(LPCLASSFACTORY iface)
107 {
108     IClassFactoryImpl *This = impl_from_IClassFactory(iface);
109
110     ULONG ref = InterlockedDecrement(&This->ref);
111
112     if (ref == 0)
113         HeapFree(GetProcessHeap(), 0, This);
114
115     return ref;
116 }
117
118 static HRESULT WINAPI XFCF_CreateInstance(LPCLASSFACTORY iface, LPUNKNOWN pOuter, REFIID riid, LPVOID *ppobj)
119 {
120     IClassFactoryImpl *This = impl_from_IClassFactory(iface);
121     HRESULT hres;
122     LPUNKNOWN punk;
123     
124     TRACE("(%p)->(%p,%s,%p)\n",This,pOuter,debugstr_guid(riid),ppobj);
125
126     *ppobj = NULL;
127     hres = This->pfnCreateInstance(pOuter, (LPVOID *) &punk);
128     if (SUCCEEDED(hres)) {
129         hres = IUnknown_QueryInterface(punk, riid, ppobj);
130         IUnknown_Release(punk);
131     }
132     return hres;
133 }
134
135 static HRESULT WINAPI XFCF_LockServer(LPCLASSFACTORY iface, BOOL dolock)
136 {
137     IClassFactoryImpl *This = impl_from_IClassFactory(iface);
138     FIXME("(%p)->(%d),stub!\n",This,dolock);
139     return S_OK;
140 }
141
142 static const IClassFactoryVtbl XFCF_Vtbl =
143 {
144     XFCF_QueryInterface,
145     XFCF_AddRef,
146     XFCF_Release,
147     XFCF_CreateInstance,
148     XFCF_LockServer
149 };
150
151 /***********************************************************************
152  *              DirectXFileCreate (D3DXOF.@)
153  */
154 HRESULT WINAPI DirectXFileCreate(LPDIRECTXFILE* lplpDirectXFile)
155 {
156     HRESULT hr;
157
158     TRACE("(%p)\n", lplpDirectXFile);
159
160     if (!lplpDirectXFile)
161         return DXFILEERR_BADVALUE;
162
163     hr = IDirectXFileImpl_Create(NULL, (LPVOID)lplpDirectXFile);
164
165     if (FAILED(hr))
166         return DXFILEERR_BADALLOC;
167
168     return S_OK;
169 }
170
171 /*******************************************************************************
172  * DllGetClassObject [D3DXOF.@]
173  * Retrieves class object from a DLL object
174  *
175  * NOTES
176  *    Docs say returns STDAPI
177  *
178  * PARAMS
179  *    rclsid [I] CLSID for the class object
180  *    riid   [I] Reference to identifier of interface for class object
181  *    ppv    [O] Address of variable to receive interface pointer for riid
182  *
183  * RETURNS
184  *    Success: S_OK
185  *    Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
186  *             E_UNEXPECTED
187  */
188 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
189 {
190     unsigned int i;
191     IClassFactoryImpl *factory;
192
193     TRACE("(%s,%s,%p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
194     
195     if ( !IsEqualGUID( &IID_IClassFactory, riid )
196          && ! IsEqualGUID( &IID_IUnknown, riid) )
197         return E_NOINTERFACE;
198
199     for (i=0; i < sizeof(object_creation)/sizeof(object_creation[0]); i++)
200     {
201         if (IsEqualGUID(object_creation[i].clsid, rclsid))
202             break;
203     }
204
205     if (i == sizeof(object_creation)/sizeof(object_creation[0]))
206     {
207         FIXME("%s: no class found.\n", debugstr_guid(rclsid));
208         return CLASS_E_CLASSNOTAVAILABLE;
209     }
210
211     factory = HeapAlloc(GetProcessHeap(), 0, sizeof(*factory));
212     if (factory == NULL) return E_OUTOFMEMORY;
213
214     factory->IClassFactory_iface.lpVtbl = &XFCF_Vtbl;
215     factory->ref = 1;
216
217     factory->pfnCreateInstance = object_creation[i].pfnCreateInstance;
218
219     *ppv = &(factory->IClassFactory_iface);
220     return S_OK;
221 }
222
223 /***********************************************************************
224  *              DllCanUnloadNow (D3DXOF.@)
225  */
226 HRESULT WINAPI DllCanUnloadNow(void)
227 {
228     return dll_ref != 0 ? S_FALSE : S_OK;
229 }
230
231 /***********************************************************************
232  *              DllRegisterServer (D3DXOF.@)
233  */
234 HRESULT WINAPI DllRegisterServer(void)
235 {
236     return __wine_register_resources( instance );
237 }
238
239 /***********************************************************************
240  *              DllUnregisterServer (D3DXOF.@)
241  */
242 HRESULT WINAPI DllUnregisterServer(void)
243 {
244     return __wine_unregister_resources( instance );
245 }