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