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