Fixed a race condition on RPC worker thread creation, and a typo.
[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  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include "config.h"
23
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include "shellapi.h"
28 #include "shlobj.h"
29 #include "shlguid.h"
30 #include "winreg.h"
31 #include "winerror.h"
32
33 #include "undocshell.h"
34 #include "wine/unicode.h"
35 #include "shell32_main.h"
36
37 #include "wine/debug.h"
38 #include "shlwapi.h"
39 #include "winuser.h"
40 #include "debughlp.h"
41 #include "wine/obj_dragdrophelper.h"
42
43 WINE_DEFAULT_DEBUG_CHANNEL(shell);
44
45 extern HRESULT WINAPI IFSFolder_Constructor(IUnknown * pUnkOuter, REFIID riid, LPVOID * ppv);
46
47 const WCHAR sShell32[12] = {'S','H','E','L','L','3','2','.','D','L','L','\0'};
48 const WCHAR sOLE32[10] = {'O','L','E','3','2','.','D','L','L','\0'};
49
50 HINSTANCE hShellOle32 = 0;
51 /**************************************************************************
52  * Default ClassFactory types
53  */
54 typedef HRESULT (CALLBACK *LPFNCREATEINSTANCE)(IUnknown* pUnkOuter, REFIID riid, LPVOID* ppvObject);
55 IClassFactory * IDefClF_fnConstructor(LPFNCREATEINSTANCE lpfnCI, PLONG pcRefDll, REFIID riidInst);
56
57 /* this table contains all CLSID's of shell32 objects */
58 struct {
59         REFIID                  riid;
60         LPFNCREATEINSTANCE      lpfnCI;
61 } InterfaceTable[6] = {
62         {&CLSID_ShellFSFolder,  &IFSFolder_Constructor},
63         {&CLSID_MyComputer,     &ISF_MyComputer_Constructor},
64         {&CLSID_ShellDesktop,   &ISF_Desktop_Constructor},
65         {&CLSID_ShellLink,      &IShellLink_Constructor},
66         {&CLSID_DragDropHelper, &IDropTargetHelper_Constructor},
67         {NULL,NULL}
68 };
69
70 /*************************************************************************
71  * __CoCreateInstance [internal]
72  *
73  * NOTES
74  *   wraper for late bound call to OLE32.DLL
75  *
76  */
77 HRESULT (WINAPI *pCoCreateInstance)(REFCLSID rclsid, LPUNKNOWN pUnkOuter, DWORD dwClsContext, REFIID iid, LPVOID *ppv) = NULL;
78
79 void * __GetExternalFunc(HMODULE * phModule, LPCWSTR szModuleName, LPCSTR szProcName)
80 {
81         if (!*phModule) *phModule = GetModuleHandleW(szModuleName);
82         if (!*phModule) *phModule = LoadLibraryW(szModuleName);
83         if (*phModule) return GetProcAddress(*phModule, szProcName);
84         return NULL;
85 }
86
87 HRESULT  __CoCreateInstance(REFCLSID rclsid, LPUNKNOWN pUnkOuter, DWORD dwClsContext, REFIID iid, LPVOID *ppv)
88 {
89         if(!pCoCreateInstance) pCoCreateInstance = __GetExternalFunc(&hShellOle32, sOLE32, "CoCreateInstance");
90         if(!pCoCreateInstance) return E_FAIL;
91         return pCoCreateInstance(rclsid, pUnkOuter, dwClsContext, iid, ppv);
92 }
93
94 /*************************************************************************
95  * SHCoCreateInstance [SHELL32.102]
96  *
97  * NOTES
98  *     exported by ordinal
99  */
100
101 /* FIXME: this should be SHLWAPI.24 since we can't yet import by ordinal */
102
103 DWORD WINAPI __SHGUIDToStringW (REFGUID guid, LPWSTR str)
104 {
105     WCHAR sFormat[52] = {'{','%','0','8','l','x','-','%','0','4',
106                          'x','-','%','0','4','x','-','%','0','2',
107                          'x','%','0','2','x','-','%','0','2','x',
108                          '%','0','2','x','%','0','2','x','%','0',
109                          '2','x','%','0','2','x','%','0','2','x',
110                          '}','\0'};
111
112     return wsprintfW ( str, sFormat,
113              guid->Data1, guid->Data2, guid->Data3,
114              guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
115              guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7] );
116
117 }
118
119 /************************************************************************/
120
121 LRESULT WINAPI SHCoCreateInstance(
122         LPCSTR aclsid,
123         REFCLSID clsid,
124         LPUNKNOWN pUnkOuter,
125         REFIID refiid,
126         LPVOID *ppv)
127 {
128         DWORD   hres;
129         IID     iid;
130         CLSID * myclsid = (CLSID*)clsid;
131         WCHAR   sKeyName[MAX_PATH];
132         const   WCHAR sCLSID[7] = {'C','L','S','I','D','\\','\0'};
133         WCHAR   sClassID[60];
134         const WCHAR sInProcServer32[16] ={'\\','I','n','p','r','o','c','S','e','r','v','e','r','3','2','\0'};
135         const WCHAR sLoadWithoutCOM[15] ={'L','o','a','d','W','i','t','h','o','u','t','C','O','M','\0'};
136         WCHAR   sDllPath[MAX_PATH];
137         HKEY    hKey;
138         DWORD   dwSize;
139         BOOLEAN bLoadFromShell32 = FALSE;
140         BOOLEAN bLoadWithoutCOM = FALSE;
141         IClassFactory * pcf = NULL;
142
143         if(!ppv) return E_POINTER;
144         *ppv=NULL;
145
146         /* if the clsid is a string, convert it */
147         if (!clsid)
148         {
149           if (!aclsid) return REGDB_E_CLASSNOTREG;
150           SHCLSIDFromStringA(aclsid, &iid);
151           myclsid = &iid;
152         }
153
154         TRACE("(%p,%s,unk:%p,%s,%p)\n",
155                 aclsid,shdebugstr_guid(myclsid),pUnkOuter,shdebugstr_guid(refiid),ppv);
156
157         /* we look up the dll path in the registry */
158         __SHGUIDToStringW(myclsid, sClassID);
159         lstrcpyW(sKeyName, sCLSID);
160         lstrcatW(sKeyName, sClassID);
161         lstrcatW(sKeyName, sInProcServer32);
162
163         if (ERROR_SUCCESS == RegOpenKeyExW(HKEY_CLASSES_ROOT, sKeyName, 0, KEY_READ, &hKey)) {
164             dwSize = sizeof(sDllPath);
165             SHQueryValueExW(hKey, NULL, 0,0, sDllPath, &dwSize );
166
167             /* if a special registry key is set, we load a shell extension without help of OLE32 */
168             bLoadWithoutCOM = (ERROR_SUCCESS == SHQueryValueExW(hKey, sLoadWithoutCOM, 0, 0, 0, 0));
169
170             /* if the com object is inside shell32, omit use of ole32 */
171             bLoadFromShell32 = (0==lstrcmpiW( PathFindFileNameW(sDllPath), sShell32));
172
173             RegCloseKey (hKey);
174         } else {
175             /* since we can't find it in the registry we try internally */
176             bLoadFromShell32 = TRUE;
177         }
178
179         TRACE("WithoutCom=%u FromShell=%u\n", bLoadWithoutCOM, bLoadFromShell32);
180
181         /* now we create a instance */
182         if (bLoadFromShell32) {
183             if (! SUCCEEDED(SHELL32_DllGetClassObject(myclsid, &IID_IClassFactory,(LPVOID*)&pcf))) {
184                 ERR("LoadFromShell failed for CLSID=%s\n", shdebugstr_guid(myclsid));
185             }
186         } else if (bLoadWithoutCOM) {
187
188             /* load a external dll without ole32 */
189             HANDLE hLibrary;
190             typedef HRESULT (CALLBACK *DllGetClassObjectFunc)(REFCLSID clsid, REFIID iid, LPVOID *ppv);
191             DllGetClassObjectFunc DllGetClassObject;
192
193             if ((hLibrary = LoadLibraryExW(sDllPath, 0, LOAD_WITH_ALTERED_SEARCH_PATH)) == 0) {
194                 ERR("couldn't load InprocServer32 dll %s\n", debugstr_w(sDllPath));
195                 hres = E_ACCESSDENIED;
196                 goto end;
197             } else if (!(DllGetClassObject = (DllGetClassObjectFunc)GetProcAddress(hLibrary, "DllGetClassObject"))) {
198                 ERR("couldn't find function DllGetClassObject in %s\n", debugstr_w(sDllPath));
199                 FreeLibrary( hLibrary );
200                 hres = E_ACCESSDENIED;
201                 goto end;
202             } else if (! SUCCEEDED(hres = DllGetClassObject(myclsid, &IID_IClassFactory, (LPVOID*)&pcf))) {
203                     TRACE("GetClassObject failed 0x%08lx\n", hres);
204                     goto end;
205             }
206
207         } else {
208
209             /* load a external dll in the usual way */
210             hres = __CoCreateInstance(myclsid, pUnkOuter, CLSCTX_INPROC_SERVER, refiid, ppv);
211             goto end;
212         }
213
214         /* here we should have a ClassFactory */
215         if (!pcf) return E_ACCESSDENIED;
216
217         hres = IClassFactory_CreateInstance(pcf, pUnkOuter, refiid, ppv);
218         IClassFactory_Release(pcf);
219 end:
220         if(hres!=S_OK)
221         {
222           ERR("failed (0x%08lx) to create CLSID:%s IID:%s\n",
223               hres, shdebugstr_guid(myclsid), shdebugstr_guid(refiid));
224           ERR("class not found in registry\n");
225         }
226
227         TRACE("-- instance: %p\n",*ppv);
228         return hres;
229 }
230
231 /*************************************************************************
232  * DllGetClassObject   [SHELL32.128]
233  */
234 HRESULT WINAPI SHELL32_DllGetClassObject(REFCLSID rclsid, REFIID iid, LPVOID *ppv)
235 {
236         HRESULT hres = E_OUTOFMEMORY;
237         IClassFactory * pcf = NULL;
238         int i;
239
240         TRACE("CLSID:%s,IID:%s\n",shdebugstr_guid(rclsid),shdebugstr_guid(iid));
241
242         if (!ppv) return E_INVALIDARG;
243         *ppv = NULL;
244
245         /* search our internal interface table */
246         for(i=0;InterfaceTable[i].riid;i++) {
247             if(IsEqualIID(InterfaceTable[i].riid, rclsid)) {
248                 TRACE("index[%u]\n", i);
249                 pcf = IDefClF_fnConstructor(InterfaceTable[i].lpfnCI, NULL, NULL);
250             }
251         }
252
253         if (!pcf) {
254             FIXME("failed for CLSID=%s\n", shdebugstr_guid(rclsid));
255             return CLASS_E_CLASSNOTAVAILABLE;
256         }
257
258         hres = IClassFactory_QueryInterface(pcf, iid, ppv);
259         IClassFactory_Release(pcf);
260
261         TRACE("-- pointer to class factory: %p\n",*ppv);
262         return hres;
263 }
264
265 /*************************************************************************
266  * SHCLSIDFromString                            [SHELL32.147]
267  *
268  * NOTES
269  *     exported by ordinal
270  */
271 DWORD WINAPI SHCLSIDFromStringA (LPCSTR clsid, CLSID *id)
272 {
273     WCHAR buffer[40];
274     TRACE("(%p(%s) %p)\n", clsid, clsid, id);
275     if (!MultiByteToWideChar( CP_ACP, 0, clsid, -1, buffer, sizeof(buffer)/sizeof(WCHAR) ))
276         return CO_E_CLASSSTRING;
277     return CLSIDFromString( buffer, id );
278 }
279 DWORD WINAPI SHCLSIDFromStringW (LPWSTR clsid, CLSID *id)
280 {
281         TRACE("(%p(%s) %p)\n", clsid, debugstr_w(clsid), id);
282         return CLSIDFromString(clsid, id);
283 }
284 DWORD WINAPI SHCLSIDFromStringAW (LPVOID clsid, CLSID *id)
285 {
286         if (SHELL_OsIsUnicode())
287           return SHCLSIDFromStringW (clsid, id);
288         return SHCLSIDFromStringA (clsid, id);
289 }
290
291 /*************************************************************************
292  *      Shell Memory Allocator
293  */
294
295 /* set the vtable later */
296 extern ICOM_VTABLE(IMalloc) VT_Shell_IMalloc32;
297
298 /* this is the static object instance */
299 typedef struct {
300         ICOM_VFIELD(IMalloc);
301         DWORD dummy;
302 } _ShellMalloc;
303
304 _ShellMalloc Shell_Malloc = { &VT_Shell_IMalloc32,1};
305
306 /* this is the global allocator of shell32 */
307 IMalloc * ShellTaskAllocator = NULL;
308
309 /******************************************************************************
310  *              IShellMalloc_QueryInterface        [VTABLE]
311  */
312 static HRESULT WINAPI IShellMalloc_fnQueryInterface(LPMALLOC iface, REFIID refiid, LPVOID *obj)
313 {
314         TRACE("(%s,%p)\n",shdebugstr_guid(refiid),obj);
315         if (IsEqualIID(refiid, &IID_IUnknown) || IsEqualIID(refiid, &IID_IMalloc)) {
316                 *obj = (LPMALLOC) &Shell_Malloc;
317                 return S_OK;
318         }
319         return E_NOINTERFACE;
320 }
321
322 /******************************************************************************
323  *              IShellMalloc_AddRefRelease        [VTABLE]
324  */
325 static ULONG WINAPI IShellMalloc_fnAddRefRelease(LPMALLOC iface)
326 {
327         return 1;
328 }
329
330 /******************************************************************************
331  *              IShellMalloc_Alloc [VTABLE]
332  */
333 static LPVOID WINAPI IShellMalloc_fnAlloc(LPMALLOC iface, DWORD cb)
334 {
335         LPVOID addr;
336
337         addr = (LPVOID) LocalAlloc(GMEM_ZEROINIT, cb);
338         TRACE("(%p,%ld);\n",addr,cb);
339         return addr;
340 }
341
342 /******************************************************************************
343  *              IShellMalloc_Realloc [VTABLE]
344  */
345 static LPVOID WINAPI IShellMalloc_fnRealloc(LPMALLOC iface, LPVOID pv, DWORD cb)
346 {
347         LPVOID addr;
348
349         if (pv) {
350                 if (cb) {
351                         addr = (LPVOID) LocalReAlloc((HANDLE) pv, cb, GMEM_ZEROINIT | GMEM_MOVEABLE);
352                 } else {
353                         LocalFree((HANDLE) pv);
354                         addr = NULL;
355                 }
356         } else {
357                 if (cb) {
358                         addr = (LPVOID) LocalAlloc(GMEM_ZEROINIT, cb);
359                 } else {
360                         addr = NULL;
361                 }
362         }
363
364         TRACE("(%p->%p,%ld)\n",pv,addr,cb);
365         return addr;
366 }
367
368 /******************************************************************************
369  *              IShellMalloc_Free [VTABLE]
370  */
371 static VOID WINAPI IShellMalloc_fnFree(LPMALLOC iface, LPVOID pv)
372 {
373         TRACE("(%p)\n",pv);
374         LocalFree((HANDLE) pv);
375 }
376
377 /******************************************************************************
378  *              IShellMalloc_GetSize [VTABLE]
379  */
380 static DWORD WINAPI IShellMalloc_fnGetSize(LPMALLOC iface, LPVOID pv)
381 {
382         DWORD cb = (DWORD) LocalSize((HANDLE)pv);
383         TRACE("(%p,%ld)\n", pv, cb);
384         return cb;
385 }
386
387 /******************************************************************************
388  *              IShellMalloc_DidAlloc [VTABLE]
389  */
390 static INT WINAPI IShellMalloc_fnDidAlloc(LPMALLOC iface, LPVOID pv)
391 {
392         TRACE("(%p)\n",pv);
393         return -1;
394 }
395
396 /******************************************************************************
397  *              IShellMalloc_HeapMinimize [VTABLE]
398  */
399 static VOID WINAPI IShellMalloc_fnHeapMinimize(LPMALLOC iface)
400 {
401         TRACE("()\n");
402 }
403
404 static ICOM_VTABLE(IMalloc) VT_Shell_IMalloc32 =
405 {
406         ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
407         IShellMalloc_fnQueryInterface,
408         IShellMalloc_fnAddRefRelease,
409         IShellMalloc_fnAddRefRelease,
410         IShellMalloc_fnAlloc,
411         IShellMalloc_fnRealloc,
412         IShellMalloc_fnFree,
413         IShellMalloc_fnGetSize,
414         IShellMalloc_fnDidAlloc,
415         IShellMalloc_fnHeapMinimize
416 };
417
418 /*************************************************************************
419  *                       SHGetMalloc                    [SHELL32.@]
420  *
421  * Return the shell IMalloc interface.
422  *
423  * PARAMS
424  *  lpmal [O] Destination for IMalloc interface.
425  *
426  * RETURNS
427  *  Success: S_OK. lpmal contains the shells IMalloc interface.
428  *  Failure. An HRESULT error code.
429  *
430  * NOTES
431  *  This function will use CoGetMalloc() if OLE32.DLL is already loaded.
432  *  If not it uses an internal implementation as a fallback.
433  */
434 HRESULT WINAPI SHGetMalloc(LPMALLOC *lpmal)
435 {
436         HRESULT (WINAPI *pCoGetMalloc)(DWORD,LPMALLOC *);
437         HMODULE hOle32;
438
439         TRACE("(%p)\n", lpmal);
440
441         if (!ShellTaskAllocator)
442         {
443                 hOle32 = GetModuleHandleA("OLE32.DLL");
444                 if(hOle32) {
445                         pCoGetMalloc = (void*) GetProcAddress(hOle32, "CoGetMalloc");
446                         if (pCoGetMalloc) pCoGetMalloc(MEMCTX_TASK, &ShellTaskAllocator);
447                         TRACE("got ole32 IMalloc\n");
448                 }
449                 if(!ShellTaskAllocator) {
450                         ShellTaskAllocator = (IMalloc* ) &Shell_Malloc;
451                         TRACE("use fallback allocator\n");
452                 }
453         }
454         *lpmal = ShellTaskAllocator;
455         return  S_OK;
456 }
457
458 /*************************************************************************
459  * SHAlloc                                      [SHELL32.196]
460  *
461  * NOTES
462  *     exported by ordinal
463  */
464 LPVOID WINAPI SHAlloc(DWORD len)
465 {
466         IMalloc * ppv;
467         LPBYTE ret;
468
469         if (!ShellTaskAllocator) SHGetMalloc(&ppv);
470
471         ret = (LPVOID) IMalloc_Alloc(ShellTaskAllocator, len);
472         TRACE("%lu bytes at %p\n",len, ret);
473         return (LPVOID)ret;
474 }
475
476 /*************************************************************************
477  * SHFree                                       [SHELL32.195]
478  *
479  * NOTES
480  *     exported by ordinal
481  */
482 void WINAPI SHFree(LPVOID pv)
483 {
484         IMalloc * ppv;
485
486         TRACE("%p\n",pv);
487         if (!ShellTaskAllocator) SHGetMalloc(&ppv);
488         IMalloc_Free(ShellTaskAllocator, pv);
489 }
490
491 /*************************************************************************
492  * SHGetDesktopFolder                   [SHELL32.@]
493  */
494 DWORD WINAPI SHGetDesktopFolder(IShellFolder **psf)
495 {
496         HRESULT hres = S_OK;
497         TRACE("\n");
498
499         if(!psf) return E_INVALIDARG;
500         *psf = NULL;
501         hres = ISF_Desktop_Constructor(NULL, &IID_IShellFolder,(LPVOID*)psf);
502
503         TRACE("-- %p->(%p)\n",psf, *psf);
504         return hres;
505 }
506 /**************************************************************************
507  * Default ClassFactory Implementation
508  *
509  * SHCreateDefClassObject
510  *
511  * NOTES
512  *  helper function for dll's without a own classfactory
513  *  a generic classfactory is returned
514  *  when the CreateInstance of the cf is called the callback is executed
515  */
516
517 typedef struct
518 {
519     ICOM_VFIELD(IClassFactory);
520     DWORD                       ref;
521     CLSID                       *rclsid;
522     LPFNCREATEINSTANCE          lpfnCI;
523     const IID *                 riidInst;
524     ULONG *                     pcRefDll; /* pointer to refcounter in external dll (ugrrr...) */
525 } IDefClFImpl;
526
527 static ICOM_VTABLE(IClassFactory) dclfvt;
528
529 /**************************************************************************
530  *  IDefClF_fnConstructor
531  */
532
533 IClassFactory * IDefClF_fnConstructor(LPFNCREATEINSTANCE lpfnCI, PLONG pcRefDll, REFIID riidInst)
534 {
535         IDefClFImpl* lpclf;
536
537         lpclf = (IDefClFImpl*)HeapAlloc(GetProcessHeap(),0,sizeof(IDefClFImpl));
538         lpclf->ref = 1;
539         lpclf->lpVtbl = &dclfvt;
540         lpclf->lpfnCI = lpfnCI;
541         lpclf->pcRefDll = pcRefDll;
542
543         if (pcRefDll) InterlockedIncrement(pcRefDll);
544         lpclf->riidInst = riidInst;
545
546         TRACE("(%p)%s\n",lpclf, shdebugstr_guid(riidInst));
547         return (LPCLASSFACTORY)lpclf;
548 }
549 /**************************************************************************
550  *  IDefClF_fnQueryInterface
551  */
552 static HRESULT WINAPI IDefClF_fnQueryInterface(
553   LPCLASSFACTORY iface, REFIID riid, LPVOID *ppvObj)
554 {
555         ICOM_THIS(IDefClFImpl,iface);
556
557         TRACE("(%p)->(%s)\n",This,shdebugstr_guid(riid));
558
559         *ppvObj = NULL;
560
561         if(IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IClassFactory)) {
562           *ppvObj = This;
563           InterlockedIncrement(&This->ref);
564           return S_OK;
565         }
566
567         TRACE("-- E_NOINTERFACE\n");
568         return E_NOINTERFACE;
569 }
570 /******************************************************************************
571  * IDefClF_fnAddRef
572  */
573 static ULONG WINAPI IDefClF_fnAddRef(LPCLASSFACTORY iface)
574 {
575         ICOM_THIS(IDefClFImpl,iface);
576         TRACE("(%p)->(count=%lu)\n",This,This->ref);
577
578         return InterlockedIncrement(&This->ref);
579 }
580 /******************************************************************************
581  * IDefClF_fnRelease
582  */
583 static ULONG WINAPI IDefClF_fnRelease(LPCLASSFACTORY iface)
584 {
585         ICOM_THIS(IDefClFImpl,iface);
586         TRACE("(%p)->(count=%lu)\n",This,This->ref);
587
588         if (!InterlockedDecrement(&This->ref))
589         {
590           if (This->pcRefDll) InterlockedDecrement(This->pcRefDll);
591
592           TRACE("-- destroying IClassFactory(%p)\n",This);
593           HeapFree(GetProcessHeap(),0,This);
594           return 0;
595         }
596         return This->ref;
597 }
598 /******************************************************************************
599  * IDefClF_fnCreateInstance
600  */
601 static HRESULT WINAPI IDefClF_fnCreateInstance(
602   LPCLASSFACTORY iface, LPUNKNOWN pUnkOuter, REFIID riid, LPVOID *ppvObject)
603 {
604         ICOM_THIS(IDefClFImpl,iface);
605
606         TRACE("%p->(%p,%s,%p)\n",This,pUnkOuter,shdebugstr_guid(riid),ppvObject);
607
608         *ppvObject = NULL;
609
610         if ( This->riidInst==NULL ||
611              IsEqualCLSID(riid, This->riidInst) ||
612              IsEqualCLSID(riid, &IID_IUnknown) )
613         {
614           return This->lpfnCI(pUnkOuter, riid, ppvObject);
615         }
616
617         ERR("unknown IID requested %s\n",shdebugstr_guid(riid));
618         return E_NOINTERFACE;
619 }
620 /******************************************************************************
621  * IDefClF_fnLockServer
622  */
623 static HRESULT WINAPI IDefClF_fnLockServer(LPCLASSFACTORY iface, BOOL fLock)
624 {
625         ICOM_THIS(IDefClFImpl,iface);
626         TRACE("%p->(0x%x), not implemented\n",This, fLock);
627         return E_NOTIMPL;
628 }
629
630 static ICOM_VTABLE(IClassFactory) dclfvt =
631 {
632     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
633     IDefClF_fnQueryInterface,
634     IDefClF_fnAddRef,
635   IDefClF_fnRelease,
636   IDefClF_fnCreateInstance,
637   IDefClF_fnLockServer
638 };
639
640 /******************************************************************************
641  * SHCreateDefClassObject                       [SHELL32.70]
642  */
643 HRESULT WINAPI SHCreateDefClassObject(
644         REFIID  riid,
645         LPVOID* ppv,
646         LPFNCREATEINSTANCE lpfnCI,      /* [in] create instance callback entry */
647         LPDWORD pcRefDll,               /* [in/out] ref count of the dll */
648         REFIID  riidInst)               /* [in] optional interface to the instance */
649 {
650         IClassFactory * pcf;
651
652         TRACE("%s %p %p %p %s\n",
653               shdebugstr_guid(riid), ppv, lpfnCI, pcRefDll, shdebugstr_guid(riidInst));
654
655         if (! IsEqualCLSID(riid, &IID_IClassFactory) ) return E_NOINTERFACE;
656         if (! (pcf = IDefClF_fnConstructor(lpfnCI, pcRefDll, riidInst))) return E_OUTOFMEMORY;
657         *ppv = pcf;
658         return NOERROR;
659 }
660
661 /*************************************************************************
662  *  DragAcceptFiles             [SHELL32.54]
663  */
664 void WINAPI DragAcceptFiles(HWND hWnd, BOOL b)
665 {
666         LONG exstyle;
667
668         if( !IsWindow(hWnd) ) return;
669         exstyle = GetWindowLongA(hWnd,GWL_EXSTYLE);
670         if (b)
671           exstyle |= WS_EX_ACCEPTFILES;
672         else
673           exstyle &= ~WS_EX_ACCEPTFILES;
674         SetWindowLongA(hWnd,GWL_EXSTYLE,exstyle);
675 }
676
677 /*************************************************************************
678  * DragFinish           [SHELL32.80]
679  */
680 void WINAPI DragFinish(HDROP h)
681 {
682         TRACE("\n");
683         GlobalFree((HGLOBAL)h);
684 }
685
686 /*************************************************************************
687  * DragQueryPoint               [SHELL32.135]
688  */
689 BOOL WINAPI DragQueryPoint(HDROP hDrop, POINT *p)
690 {
691         DROPFILES *lpDropFileStruct;
692         BOOL bRet;
693
694         TRACE("\n");
695
696         lpDropFileStruct = (DROPFILES *) GlobalLock(hDrop);
697
698         *p = lpDropFileStruct->pt;
699         bRet = lpDropFileStruct->fNC;
700
701         GlobalUnlock(hDrop);
702         return bRet;
703 }
704
705 /*************************************************************************
706  *  DragQueryFile               [SHELL32.81]
707  *  DragQueryFileA              [SHELL32.82]
708  */
709 UINT WINAPI DragQueryFileA(
710         HDROP hDrop,
711         UINT lFile,
712         LPSTR lpszFile,
713         UINT lLength)
714 {
715         LPSTR lpDrop;
716         UINT i = 0;
717         DROPFILES *lpDropFileStruct = (DROPFILES *) GlobalLock(hDrop);
718
719         TRACE("(%p, %x, %p, %u)\n",     hDrop,lFile,lpszFile,lLength);
720
721         if(!lpDropFileStruct) goto end;
722
723         lpDrop = (LPSTR) lpDropFileStruct + lpDropFileStruct->pFiles;
724
725         if(lpDropFileStruct->fWide == TRUE) {
726             LPWSTR lpszFileW = NULL;
727
728             if(lpszFile) {
729                 lpszFileW = (LPWSTR) HeapAlloc(GetProcessHeap(), 0, lLength*sizeof(WCHAR));
730                 if(lpszFileW == NULL) {
731                     goto end;
732                 }
733             }
734             i = DragQueryFileW(hDrop, lFile, lpszFileW, lLength);
735
736             if(lpszFileW) {
737                 WideCharToMultiByte(CP_ACP, 0, lpszFileW, -1, lpszFile, lLength, 0, NULL);
738                 HeapFree(GetProcessHeap(), 0, lpszFileW);
739             }
740             goto end;
741         }
742
743         while (i++ < lFile)
744         {
745           while (*lpDrop++); /* skip filename */
746           if (!*lpDrop)
747           {
748             i = (lFile == 0xFFFFFFFF) ? i : 0;
749             goto end;
750           }
751         }
752
753         i = strlen(lpDrop);
754         i++;
755         if (!lpszFile ) goto end;   /* needed buffer size */
756         i = (lLength > i) ? i : lLength;
757         lstrcpynA (lpszFile,  lpDrop,  i);
758 end:
759         GlobalUnlock(hDrop);
760         return i;
761 }
762
763 /*************************************************************************
764  *  DragQueryFileW              [SHELL32.133]
765  */
766 UINT WINAPI DragQueryFileW(
767         HDROP hDrop,
768         UINT lFile,
769         LPWSTR lpszwFile,
770         UINT lLength)
771 {
772         LPWSTR lpwDrop;
773         UINT i = 0;
774         DROPFILES *lpDropFileStruct = (DROPFILES *) GlobalLock(hDrop);
775
776         TRACE("(%p, %x, %p, %u)\n", hDrop,lFile,lpszwFile,lLength);
777
778         if(!lpDropFileStruct) goto end;
779
780         lpwDrop = (LPWSTR) ((LPSTR)lpDropFileStruct + lpDropFileStruct->pFiles);
781
782         if(lpDropFileStruct->fWide == FALSE) {
783             LPSTR lpszFileA = NULL;
784
785             if(lpszwFile) {
786                 lpszFileA = (LPSTR) HeapAlloc(GetProcessHeap(), 0, lLength);
787                 if(lpszFileA == NULL) {
788                     goto end;
789                 }
790             }
791             i = DragQueryFileA(hDrop, lFile, lpszFileA, lLength);
792
793             if(lpszFileA) {
794                 MultiByteToWideChar(CP_ACP, 0, lpszFileA, -1, lpszwFile, lLength);
795                 HeapFree(GetProcessHeap(), 0, lpszFileA);
796             }
797             goto end;
798         }
799
800         i = 0;
801         while (i++ < lFile)
802         {
803           while (*lpwDrop++); /* skip filename */
804           if (!*lpwDrop)
805           {
806             i = (lFile == 0xFFFFFFFF) ? i : 0;
807             goto end;
808           }
809         }
810
811         i = strlenW(lpwDrop);
812         i++;
813         if ( !lpszwFile) goto end;   /* needed buffer size */
814
815         i = (lLength > i) ? i : lLength;
816         lstrcpynW (lpszwFile, lpwDrop, i);
817 end:
818         GlobalUnlock(hDrop);
819         return i;
820 }