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