Use GUIDs from itss.idl.
[wine] / dlls / itss / itss.c
1 /*
2  *    ITSS Class Factory
3  *
4  * Copyright 2002 Lionel Ulmer
5  * Copyright 2004 Mike McCormack
6  *
7  *  see http://bonedaddy.net/pabs3/hhm/#chmspec
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23
24 #include "config.h"
25
26 #include <stdarg.h>
27 #include <stdio.h>
28
29 #define COBJMACROS
30
31 #include "windef.h"
32 #include "winbase.h"
33 #include "winuser.h"
34 #include "winnls.h"
35 #include "winreg.h"
36 #include "ole2.h"
37
38 #include "uuids.h"
39
40 #include "wine/unicode.h"
41 #include "wine/debug.h"
42
43 #include "itsstor.h"
44
45 #define ITSS_INITGUID
46 #include "itss.h"
47
48 WINE_DEFAULT_DEBUG_CHANNEL(itss);
49
50 #include "initguid.h"
51
52 static HRESULT ITSS_create(IUnknown *pUnkOuter, LPVOID *ppObj);
53
54 ULONG dll_count = 0;
55
56 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpv)
57 {
58     switch(fdwReason) {
59     case DLL_PROCESS_ATTACH:
60         DisableThreadLibraryCalls(hInstDLL);
61         break;
62     case DLL_PROCESS_DETACH:
63         break;
64     }
65     return TRUE;
66 }
67
68 /******************************************************************************
69  * ITSS ClassFactory
70  */
71 typedef struct {
72     IClassFactory ITF_IClassFactory;
73
74     DWORD ref;
75     HRESULT (*pfnCreateInstance)(IUnknown *pUnkOuter, LPVOID *ppObj);
76 } IClassFactoryImpl;
77
78 struct object_creation_info
79 {
80     const CLSID *clsid;
81     LPCSTR szClassName;
82     HRESULT (*pfnCreateInstance)(IUnknown *pUnkOuter, LPVOID *ppObj);
83 };
84
85 static const struct object_creation_info object_creation[] =
86 {
87     { &CLSID_ITStorage, "ITStorage", ITSS_create },
88     { &CLSID_ITSProtocol, "ITSProtocol", ITS_IParseDisplayName_create },
89 };
90
91 static HRESULT WINAPI
92 ITSSCF_QueryInterface(LPCLASSFACTORY iface,REFIID riid,LPVOID *ppobj)
93 {
94     IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
95
96     if (IsEqualGUID(riid, &IID_IUnknown)
97         || IsEqualGUID(riid, &IID_IClassFactory))
98     {
99         IClassFactory_AddRef(iface);
100         *ppobj = This;
101         return S_OK;
102     }
103
104     WARN("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),ppobj);
105     return E_NOINTERFACE;
106 }
107
108 static ULONG WINAPI ITSSCF_AddRef(LPCLASSFACTORY iface)
109 {
110     IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
111     return InterlockedIncrement(&This->ref);
112 }
113
114 static ULONG WINAPI ITSSCF_Release(LPCLASSFACTORY iface)
115 {
116     IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
117
118     ULONG ref = InterlockedDecrement(&This->ref);
119
120     if (ref == 0) {
121         HeapFree(GetProcessHeap(), 0, This);
122         InterlockedDecrement(&dll_count);
123     }
124
125     return ref;
126 }
127
128
129 static HRESULT WINAPI ITSSCF_CreateInstance(LPCLASSFACTORY iface, LPUNKNOWN pOuter,
130                                           REFIID riid, LPVOID *ppobj)
131 {
132     IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
133     HRESULT hres;
134     LPUNKNOWN punk;
135     
136     TRACE("(%p)->(%p,%s,%p)\n",This,pOuter,debugstr_guid(riid),ppobj);
137
138     *ppobj = NULL;
139     hres = This->pfnCreateInstance(pOuter, (LPVOID *) &punk);
140     if (SUCCEEDED(hres)) {
141         hres = IUnknown_QueryInterface(punk, riid, ppobj);
142         IUnknown_Release(punk);
143     }
144     return hres;
145 }
146
147 static HRESULT WINAPI ITSSCF_LockServer(LPCLASSFACTORY iface, BOOL dolock)
148 {
149     TRACE("(%p)->(%d)\n", iface, dolock);
150
151     if(dolock)
152         InterlockedIncrement(&dll_count);
153     else
154         InterlockedDecrement(&dll_count);
155
156     return S_OK;
157 }
158
159 static const IClassFactoryVtbl ITSSCF_Vtbl =
160 {
161     ITSSCF_QueryInterface,
162     ITSSCF_AddRef,
163     ITSSCF_Release,
164     ITSSCF_CreateInstance,
165     ITSSCF_LockServer
166 };
167
168
169 /***********************************************************************
170  *              DllGetClassObject       (ITSS.@)
171  */
172 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID iid, LPVOID *ppv)
173 {
174     DWORD i;
175     IClassFactoryImpl *factory;
176
177     TRACE("%s %s %p\n",debugstr_guid(rclsid), debugstr_guid(iid), ppv);
178     
179     if ( !IsEqualGUID( &IID_IClassFactory, iid )
180          && ! IsEqualGUID( &IID_IUnknown, iid) )
181         return E_NOINTERFACE;
182
183     for (i=0; i < sizeof(object_creation)/sizeof(object_creation[0]); i++)
184     {
185         if (IsEqualGUID(object_creation[i].clsid, rclsid))
186             break;
187     }
188
189     if (i == sizeof(object_creation)/sizeof(object_creation[0]))
190     {
191         FIXME("%s: no class found.\n", debugstr_guid(rclsid));
192         return CLASS_E_CLASSNOTAVAILABLE;
193     }
194
195     TRACE("Creating a class factory for %s\n",object_creation[i].szClassName);
196
197     factory = HeapAlloc(GetProcessHeap(), 0, sizeof(*factory));
198     if (factory == NULL) return E_OUTOFMEMORY;
199
200     factory->ITF_IClassFactory.lpVtbl = &ITSSCF_Vtbl;
201     factory->ref = 1;
202
203     factory->pfnCreateInstance = object_creation[i].pfnCreateInstance;
204
205     *ppv = &(factory->ITF_IClassFactory);
206     InterlockedIncrement(&dll_count);
207
208     TRACE("(%p) <- %p\n", ppv, &(factory->ITF_IClassFactory) );
209
210     return S_OK;
211 }
212
213 /*****************************************************************************/
214
215 typedef struct {
216     const IITStorageVtbl *vtbl_IITStorage;
217     DWORD ref;
218 } ITStorageImpl;
219
220
221 HRESULT WINAPI ITStorageImpl_QueryInterface(
222     IITStorage* iface,
223     REFIID riid,
224     void** ppvObject)
225 {
226     ITStorageImpl *This = (ITStorageImpl *)iface;
227     if (IsEqualGUID(riid, &IID_IUnknown)
228         || IsEqualGUID(riid, &IID_IITStorage))
229     {
230         IClassFactory_AddRef(iface);
231         *ppvObject = This;
232         return S_OK;
233     }
234
235     WARN("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),ppvObject);
236     return E_NOINTERFACE;
237 }
238
239 ULONG WINAPI ITStorageImpl_AddRef(
240     IITStorage* iface)
241 {
242     ITStorageImpl *This = (ITStorageImpl *)iface;
243     TRACE("%p\n", This);
244     return InterlockedIncrement(&This->ref);
245 }
246
247 ULONG WINAPI ITStorageImpl_Release(
248     IITStorage* iface)
249 {
250     ITStorageImpl *This = (ITStorageImpl *)iface;
251     ULONG ref = InterlockedDecrement(&This->ref);
252
253     if (ref == 0) {
254         HeapFree(GetProcessHeap(), 0, This);
255         InterlockedDecrement(&dll_count);
256     }
257
258     return ref;
259 }
260
261 HRESULT WINAPI ITStorageImpl_StgCreateDocfile(
262     IITStorage* iface,
263     const WCHAR* pwcsName,
264     DWORD grfMode,
265     DWORD reserved,
266     IStorage** ppstgOpen)
267 {
268     ITStorageImpl *This = (ITStorageImpl *)iface;
269
270     TRACE("%p %s %lu %lu %p\n", This,
271           debugstr_w(pwcsName), grfMode, reserved, ppstgOpen );
272
273     return ITSS_StgOpenStorage( pwcsName, NULL, grfMode,
274                                 0, reserved, ppstgOpen);
275 }
276
277 HRESULT WINAPI ITStorageImpl_StgCreateDocfileOnILockBytes(
278     IITStorage* iface,
279     ILockBytes* plkbyt,
280     DWORD grfMode,
281     DWORD reserved,
282     IStorage** ppstgOpen)
283 {
284     ITStorageImpl *This = (ITStorageImpl *)iface;
285     FIXME("%p\n", This);
286     return E_NOTIMPL;
287 }
288
289 HRESULT WINAPI ITStorageImpl_StgIsStorageFile(
290     IITStorage* iface,
291     const WCHAR* pwcsName)
292 {
293     ITStorageImpl *This = (ITStorageImpl *)iface;
294     FIXME("%p\n", This);
295     return E_NOTIMPL;
296 }
297
298 HRESULT WINAPI ITStorageImpl_StgIsStorageILockBytes(
299     IITStorage* iface,
300     ILockBytes* plkbyt)
301 {
302     ITStorageImpl *This = (ITStorageImpl *)iface;
303     FIXME("%p\n", This);
304     return E_NOTIMPL;
305 }
306
307 HRESULT WINAPI ITStorageImpl_StgOpenStorage(
308     IITStorage* iface,
309     const WCHAR* pwcsName,
310     IStorage* pstgPriority,
311     DWORD grfMode,
312     SNB snbExclude,
313     DWORD reserved,
314     IStorage** ppstgOpen)
315 {
316     ITStorageImpl *This = (ITStorageImpl *)iface;
317
318     TRACE("%p %s %p %ld %p\n", This, debugstr_w( pwcsName ),
319            pstgPriority, grfMode, snbExclude );
320
321     return ITSS_StgOpenStorage( pwcsName, pstgPriority, grfMode,
322                                 snbExclude, reserved, ppstgOpen);
323 }
324
325 HRESULT WINAPI ITStorageImpl_StgOpenStorageOnILockBytes(
326     IITStorage* iface,
327     ILockBytes* plkbyt,
328     IStorage* pStgPriority,
329     DWORD grfMode,
330     SNB snbExclude,
331     DWORD reserved,
332     IStorage** ppstgOpen)
333 {
334     ITStorageImpl *This = (ITStorageImpl *)iface;
335     FIXME("%p\n", This);
336     return E_NOTIMPL;
337 }
338
339 HRESULT WINAPI ITStorageImpl_StgSetTimes(
340     IITStorage* iface,
341     WCHAR* lpszName,
342     FILETIME* pctime,
343     FILETIME* patime,
344     FILETIME* pmtime)
345 {
346     ITStorageImpl *This = (ITStorageImpl *)iface;
347     FIXME("%p\n", This);
348     return E_NOTIMPL;
349 }
350
351 HRESULT WINAPI ITStorageImpl_SetControlData(
352     IITStorage* iface,
353     PITS_Control_Data pControlData)
354 {
355     ITStorageImpl *This = (ITStorageImpl *)iface;
356     FIXME("%p\n", This);
357     return E_NOTIMPL;
358 }
359
360 HRESULT WINAPI ITStorageImpl_DefaultControlData(
361     IITStorage* iface,
362     PITS_Control_Data* ppControlData)
363 {
364     ITStorageImpl *This = (ITStorageImpl *)iface;
365     FIXME("%p\n", This);
366     return E_NOTIMPL;
367 }
368
369 HRESULT WINAPI ITStorageImpl_Compact(
370     IITStorage* iface,
371     const WCHAR* pwcsName,
372     ECompactionLev iLev)
373 {
374     ITStorageImpl *This = (ITStorageImpl *)iface;
375     FIXME("%p\n", This);
376     return E_NOTIMPL;
377 }
378
379 static const IITStorageVtbl ITStorageImpl_Vtbl =
380 {
381     ITStorageImpl_QueryInterface,
382     ITStorageImpl_AddRef,
383     ITStorageImpl_Release,
384     ITStorageImpl_StgCreateDocfile,
385     ITStorageImpl_StgCreateDocfileOnILockBytes,
386     ITStorageImpl_StgIsStorageFile,
387     ITStorageImpl_StgIsStorageILockBytes,
388     ITStorageImpl_StgOpenStorage,
389     ITStorageImpl_StgOpenStorageOnILockBytes,
390     ITStorageImpl_StgSetTimes,
391     ITStorageImpl_SetControlData,
392     ITStorageImpl_DefaultControlData,
393     ITStorageImpl_Compact,
394 };
395
396 static HRESULT ITSS_create(IUnknown *pUnkOuter, LPVOID *ppObj)
397 {
398     ITStorageImpl *its;
399
400     if( pUnkOuter )
401         return CLASS_E_NOAGGREGATION;
402
403     its = HeapAlloc( GetProcessHeap(), 0, sizeof(ITStorageImpl) );
404     its->vtbl_IITStorage = &ITStorageImpl_Vtbl;
405     its->ref = 1;
406
407     TRACE("-> %p\n", its);
408     *ppObj = (LPVOID) its;
409     InterlockedIncrement(&dll_count);
410
411     return S_OK;
412 }
413
414 /*****************************************************************************/
415
416 HRESULT WINAPI DllRegisterServer(void)
417 {
418     FIXME("\n");
419     return S_OK;
420 }
421
422 HRESULT WINAPI DllCanUnloadNow(void)
423 {
424     TRACE("dll_count = %lu\n", dll_count);
425     return dll_count ? S_FALSE : S_OK;
426 }