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