Recovery of release 990110 after disk crash.
[wine] / dlls / shell32 / shellole.c
1 /*
2  *      handling of SHELL32.DLL OLE-Objects
3  *
4  *      Copyright 1997  Marcus Meissner
5  *      Copyright 1998  Juergen Schmied  <juergen.schmied@metronet.de>
6  *
7  */
8
9 #include <ctype.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include "ole.h"
13 #include "ole2.h"
14 #include "debug.h"
15 #include "shlobj.h"
16 #include "objbase.h"
17 #include "shell.h"
18 #include "winerror.h"
19 #include "winnls.h"
20 #include "winproc.h"
21 #include "winversion.h"
22 #include "commctrl.h"
23
24 #include "shell32_main.h"
25
26 /*************************************************************************
27  *
28  */
29 typedef DWORD (* WINAPI GetClassPtr)(REFCLSID,REFIID,LPVOID);
30
31 static GetClassPtr SH_find_moduleproc(LPSTR dllname,HMODULE32 *xhmod,LPSTR name)
32 {       HMODULE32 hmod;
33         FARPROC32 dllunload,nameproc;
34
35         TRACE(shell,"dll=%s, hmodule=%p, name=%s\n",dllname, xhmod, name);
36         if (xhmod)
37         { *xhmod = 0;
38         }
39         if (!strcasecmp(PathFindFilename32A(dllname),"shell32.dll"))
40         { return (GetClassPtr)SHELL32_DllGetClassObject;
41         }
42
43         hmod = LoadLibraryEx32A(dllname,0,LOAD_WITH_ALTERED_SEARCH_PATH);
44         if (!hmod)
45         { return NULL;
46         }
47         dllunload = GetProcAddress32(hmod,"DllCanUnloadNow");
48         if (!dllunload)
49         { if (xhmod)
50           { *xhmod = hmod;
51           }
52         }
53         nameproc = GetProcAddress32(hmod,name);
54         if (!nameproc)
55         { FreeLibrary32(hmod);
56           return NULL;
57         }
58         /* register unloadable dll with unloadproc ... */
59         return (GetClassPtr)nameproc;
60 }
61 /*************************************************************************
62  *
63  */
64 static DWORD SH_get_instance(REFCLSID clsid,LPSTR dllname,LPVOID unknownouter,REFIID refiid,LPVOID inst) 
65 {       GetClassPtr     dllgetclassob;
66         DWORD           hres;
67         LPCLASSFACTORY  classfac;
68
69         char    xclsid[50],xrefiid[50];
70         WINE_StringFromCLSID((LPCLSID)clsid,xclsid);
71         WINE_StringFromCLSID((LPCLSID)refiid,xrefiid);
72         TRACE(shell,"\n\tCLSID:%s,%s,%p,\n\tIID:%s,%p\n",xclsid, dllname,unknownouter,xrefiid,inst);
73         
74         dllgetclassob = SH_find_moduleproc(dllname,NULL,"DllGetClassObject");
75         if (!dllgetclassob)
76         { return 0x80070000|GetLastError();
77         }
78
79         hres = (*dllgetclassob)(clsid,(REFIID)&IID_IClassFactory,&classfac);
80         if ((hres<0) || (hres>=0x80000000))
81             return hres;
82         if (!classfac)
83         { FIXME(shell,"no classfactory, but hres is 0x%ld!\n",hres);
84           return E_FAIL;
85         }
86         IClassFactory_CreateInstance(classfac,unknownouter,refiid,inst);
87         IClassFactory_Release(classfac);
88         return 0;
89 }
90
91 /*************************************************************************
92  * SHCoCreateInstance [SHELL32.102]
93  * 
94  * NOTES
95  *     exported by ordinal
96  */
97 LRESULT WINAPI SHCoCreateInstance(LPSTR aclsid,CLSID *clsid,LPUNKNOWN unknownouter,REFIID refiid,LPVOID inst)
98 {       char    buffer[256],xclsid[48],xiid[48],path[260],tmodel[100];
99         HKEY    inprockey;
100         DWORD   pathlen,type,tmodellen;
101         DWORD   hres;
102         
103         WINE_StringFromCLSID(refiid,xiid);
104
105         if (clsid)
106         { WINE_StringFromCLSID(clsid,xclsid);
107         }
108         else
109         { if (!aclsid)
110           {     return 0x80040154;
111           }
112           strcpy(xclsid,aclsid);
113         }
114         TRACE(shell,"(%p,\n\tSID:\t%s,%p,\n\tIID:\t%s,%p)\n",aclsid,xclsid,unknownouter,xiid,inst);
115
116         sprintf(buffer,"CLSID\\%s\\InProcServer32",xclsid);
117
118         if (RegOpenKeyEx32A(HKEY_CLASSES_ROOT,buffer,0,0x02000000,&inprockey))
119         { return SH_get_instance(clsid,"shell32.dll",unknownouter,refiid,inst);
120         }
121         pathlen=sizeof(path);
122
123         if (RegQueryValue32A(inprockey,NULL,path,&pathlen))
124         { RegCloseKey(inprockey);
125           return SH_get_instance(clsid,"shell32.dll",unknownouter,refiid,inst);
126         }
127
128         TRACE(shell, "Server dll is %s\n",path);
129         tmodellen=sizeof(tmodel);
130         type=REG_SZ;
131         if (RegQueryValueEx32A(inprockey,"ThreadingModel",NULL,&type,tmodel,&tmodellen))
132         { RegCloseKey(inprockey);
133           return SH_get_instance(clsid,"shell32.dll",unknownouter,refiid,inst);
134         }
135
136         TRACE(shell, "Threading model is %s\n",tmodel);
137
138         hres=SH_get_instance(clsid,path,unknownouter,refiid,inst);
139         if (hres<0 || (hres>0x80000000))
140         { hres=SH_get_instance(clsid,"shell32.dll",unknownouter,refiid,inst);
141         }
142         RegCloseKey(inprockey);
143         return hres;
144 }
145
146
147 /*************************************************************************
148  * SHELL32_DllGetClassObject   [SHELL32.128]
149  *
150  * [Standart OLE/COM Interface Method]
151  * This Function retrives the pointer to a specified interface (iid) of
152  * a given class (rclsid).
153  * With this pointer it's possible to call the IClassFactory_CreateInstance
154  * method to get a instance of the requested Class.
155  * This function does NOT instantiate the Class!!!
156  * 
157  * RETURNS
158  *   HRESULT
159  *
160  */
161 DWORD WINAPI SHELL32_DllGetClassObject(REFCLSID rclsid,REFIID iid,LPVOID *ppv)
162 {       HRESULT hres = E_OUTOFMEMORY;
163         LPCLASSFACTORY lpclf;
164
165         char xclsid[50],xiid[50];
166         WINE_StringFromCLSID((LPCLSID)rclsid,xclsid);
167         WINE_StringFromCLSID((LPCLSID)iid,xiid);
168         TRACE(shell,"\n\tCLSID:\t%s,\n\tIID:\t%s\n",xclsid,xiid);
169         
170         *ppv = NULL;
171         if(IsEqualCLSID(rclsid, &CLSID_ShellDesktop)|| 
172            IsEqualCLSID(rclsid, &CLSID_ShellLink))
173         { if(IsEqualCLSID(rclsid, &CLSID_ShellDesktop))      /*debug*/
174           { TRACE(shell,"-- requested CLSID_ShellDesktop\n");
175           }
176
177           if (IsEqualCLSID(rclsid, &CLSID_ShellLink))
178           { if (VERSION_OsIsUnicode ())
179               lpclf = IShellLinkW_CF_Constructor();
180             else  
181               lpclf = IShellLink_CF_Constructor();
182           }
183           else
184           { lpclf = IClassFactory_Constructor();
185           }
186
187           if(lpclf) {
188             hres = IClassFactory_QueryInterface(lpclf,iid, ppv);
189             IClassFactory_Release(lpclf);
190           }
191         }
192         else
193         { WARN(shell, "-- CLSID not found\n");
194           hres = CLASS_E_CLASSNOTAVAILABLE;
195         }
196         TRACE(shell,"-- pointer to class factory: %p\n",*ppv);
197         return hres;
198 }
199
200 /*************************************************************************
201  *                       SHGetMalloc                    [SHELL32.220]
202  * returns the interface to shell malloc.
203  *
204  * [SDK header win95/shlobj.h:
205  * equivalent to:  #define SHGetMalloc(ppmem)   CoGetMalloc(MEMCTX_TASK, ppmem)
206  * ]
207  * What we are currently doing is not very wrong, since we always use the same
208  * heap (ProcessHeap).
209  */
210 DWORD WINAPI SHGetMalloc(LPMALLOC32 *lpmal) 
211 {       TRACE(shell,"(%p)\n", lpmal);
212         return CoGetMalloc32(0,lpmal);
213 }
214
215 /**************************************************************************
216 *  IClassFactory Implementation
217 */
218
219 static ICOM_VTABLE(IClassFactory) clfvt;
220
221 /**************************************************************************
222  *  IClassFactory_Constructor
223  */
224
225 LPCLASSFACTORY IClassFactory_Constructor(void)
226 {
227         _IClassFactory* lpclf;
228
229         lpclf= (_IClassFactory*)HeapAlloc(GetProcessHeap(),0,sizeof(_IClassFactory));
230         lpclf->ref = 1;
231         lpclf->lpvtbl = &clfvt;
232   TRACE(shell,"(%p)->()\n",lpclf);
233         return (LPCLASSFACTORY)lpclf;
234 }
235 /**************************************************************************
236  *  IClassFactory_QueryInterface
237  */
238 static HRESULT WINAPI IClassFactory_fnQueryInterface(
239   LPUNKNOWN iface, REFIID riid, LPVOID *ppvObj)
240 {
241         ICOM_THIS(IClassFactory,iface);
242         char    xriid[50];
243         WINE_StringFromCLSID((LPCLSID)riid,xriid);
244         TRACE(shell,"(%p)->(\n\tIID:\t%s)\n",this,xriid);
245
246         *ppvObj = NULL;
247
248         if(IsEqualIID(riid, &IID_IUnknown))          /*IUnknown*/
249         { *ppvObj = this; 
250         }
251         else if(IsEqualIID(riid, &IID_IClassFactory))  /*IClassFactory*/
252         { *ppvObj = (IClassFactory*)this;
253         }   
254
255         if(*ppvObj)
256         { IUnknown_AddRef((LPUNKNOWN)*ppvObj);          
257           TRACE(shell,"-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
258           return S_OK;
259         }
260         TRACE(shell,"-- Interface: E_NOINTERFACE\n");
261         return E_NOINTERFACE;
262 }  
263 /******************************************************************************
264  * IClassFactory_AddRef
265  */
266 static ULONG WINAPI IClassFactory_fnAddRef(LPUNKNOWN iface)
267 {
268         ICOM_THIS(IClassFactory,iface);
269         TRACE(shell,"(%p)->(count=%lu)\n",this,this->ref);
270         return ++(this->ref);
271 }
272 /******************************************************************************
273  * IClassFactory_Release
274  */
275 static ULONG WINAPI IClassFactory_fnRelease(LPUNKNOWN iface)
276 {
277         ICOM_THIS(IClassFactory,iface);
278         TRACE(shell,"(%p)->(count=%lu)\n",this,this->ref);
279         if (!--(this->ref)) 
280         { TRACE(shell,"-- destroying IClassFactory(%p)\n",this);
281                 HeapFree(GetProcessHeap(),0,this);
282                 return 0;
283         }
284         return this->ref;
285 }
286 /******************************************************************************
287  * IClassFactory_CreateInstance
288  */
289 static HRESULT WINAPI IClassFactory_fnCreateInstance(
290   LPCLASSFACTORY iface, LPUNKNOWN pUnknown, REFIID riid, LPVOID *ppObject)
291 {
292         ICOM_THIS(IClassFactory,iface);
293         IUnknown *pObj = NULL;
294         HRESULT hres;
295         char    xriid[50];
296
297         WINE_StringFromCLSID((LPCLSID)riid,xriid);
298         TRACE(shell,"%p->(%p,\n\tIID:\t%s,%p)\n",this,pUnknown,xriid,ppObject);
299
300         *ppObject = NULL;
301                 
302         if(pUnknown)
303         {       return(CLASS_E_NOAGGREGATION);
304         }
305
306         if (IsEqualIID(riid, &IID_IShellFolder))
307         { pObj = (IUnknown *)IShellFolder_Constructor(NULL,NULL);
308         } 
309         else if (IsEqualIID(riid, &IID_IShellView))
310         { pObj = (IUnknown *)IShellView_Constructor(NULL,NULL);
311         } 
312         else if (IsEqualIID(riid, &IID_IExtractIcon))
313         { pObj = (IUnknown *)IExtractIcon_Constructor(NULL);
314         } 
315         else if (IsEqualIID(riid, &IID_IContextMenu))
316         { pObj = (IUnknown *)IContextMenu_Constructor(NULL, NULL, 0);
317         } 
318         else if (IsEqualIID(riid, &IID_IDataObject))
319         { pObj = (IUnknown *)IDataObject_Constructor(0,NULL,NULL,0);
320         } 
321         else
322         { ERR(shell,"unknown IID requested\n\tIID:\t%s\n",xriid);
323           return(E_NOINTERFACE);
324         }
325         
326         if (!pObj)
327         { return(E_OUTOFMEMORY);
328         }
329          
330         hres = pObj->lpvtbl->fnQueryInterface(pObj,riid, ppObject);
331         pObj->lpvtbl->fnRelease(pObj);
332         TRACE(shell,"-- Object created: (%p)->%p\n",this,*ppObject);
333
334         return hres;
335 }
336 /******************************************************************************
337  * IClassFactory_LockServer
338  */
339 static HRESULT WINAPI IClassFactory_fnLockServer(LPCLASSFACTORY iface, BOOL32 fLock)
340 {
341         ICOM_THIS(IClassFactory,iface);
342         TRACE(shell,"%p->(0x%x), not implemented\n",this, fLock);
343         return E_NOTIMPL;
344 }
345
346 static ICOM_VTABLE(IClassFactory) clfvt = 
347 {
348   {
349     IClassFactory_fnQueryInterface,
350     IClassFactory_fnAddRef,
351     IClassFactory_fnRelease
352   },
353   IClassFactory_fnCreateInstance,
354   IClassFactory_fnLockServer
355 };