Added stubbed implementation of IPersistPropertyBag interface.
[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         {&CLSID_FolderShortcut, &FolderShortcut_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  */
220 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID iid, LPVOID *ppv)
221 {
222         HRESULT hres = E_OUTOFMEMORY;
223         IClassFactory * pcf = NULL;
224         int i;
225
226         TRACE("CLSID:%s,IID:%s\n",shdebugstr_guid(rclsid),shdebugstr_guid(iid));
227
228         if (!ppv) return E_INVALIDARG;
229         *ppv = NULL;
230
231         /* search our internal interface table */
232         for(i=0;InterfaceTable[i].riid;i++) {
233             if(IsEqualIID(InterfaceTable[i].riid, rclsid)) {
234                 TRACE("index[%u]\n", i);
235                 pcf = IDefClF_fnConstructor(InterfaceTable[i].lpfnCI, NULL, NULL);
236             }
237         }
238
239         if (!pcf) {
240             FIXME("failed for CLSID=%s\n", shdebugstr_guid(rclsid));
241             return CLASS_E_CLASSNOTAVAILABLE;
242         }
243
244         hres = IClassFactory_QueryInterface(pcf, iid, ppv);
245         IClassFactory_Release(pcf);
246
247         TRACE("-- pointer to class factory: %p\n",*ppv);
248         return hres;
249 }
250
251 /*************************************************************************
252  * SHCLSIDFromString                            [SHELL32.147]
253  *
254  * NOTES
255  *     exported by ordinal
256  */
257 DWORD WINAPI SHCLSIDFromStringA (LPCSTR clsid, CLSID *id)
258 {
259     WCHAR buffer[40];
260     TRACE("(%p(%s) %p)\n", clsid, clsid, id);
261     if (!MultiByteToWideChar( CP_ACP, 0, clsid, -1, buffer, sizeof(buffer)/sizeof(WCHAR) ))
262         return CO_E_CLASSSTRING;
263     return CLSIDFromString( buffer, id );
264 }
265 DWORD WINAPI SHCLSIDFromStringW (LPCWSTR clsid, CLSID *id)
266 {
267         TRACE("(%p(%s) %p)\n", clsid, debugstr_w(clsid), id);
268         return CLSIDFromString((LPWSTR)clsid, id);
269 }
270 DWORD WINAPI SHCLSIDFromStringAW (LPVOID clsid, CLSID *id)
271 {
272         if (SHELL_OsIsUnicode())
273           return SHCLSIDFromStringW (clsid, id);
274         return SHCLSIDFromStringA (clsid, id);
275 }
276
277 /*************************************************************************
278  *      Shell Memory Allocator
279  */
280
281 /* set the vtable later */
282 static const IMallocVtbl VT_Shell_IMalloc32;
283
284 /* this is the static object instance */
285 typedef struct {
286         const IMallocVtbl *lpVtbl;
287         DWORD dummy;
288 } _ShellMalloc;
289
290 static _ShellMalloc Shell_Malloc = { &VT_Shell_IMalloc32,1};
291
292 /* this is the global allocator of shell32 */
293 static IMalloc * ShellTaskAllocator = NULL;
294
295 /******************************************************************************
296  *              IShellMalloc_QueryInterface        [VTABLE]
297  */
298 static HRESULT WINAPI IShellMalloc_fnQueryInterface(LPMALLOC iface, REFIID refiid, LPVOID *obj)
299 {
300         TRACE("(%s,%p)\n",shdebugstr_guid(refiid),obj);
301         if (IsEqualIID(refiid, &IID_IUnknown) || IsEqualIID(refiid, &IID_IMalloc)) {
302                 *obj = (LPMALLOC) &Shell_Malloc;
303                 return S_OK;
304         }
305         return E_NOINTERFACE;
306 }
307
308 /******************************************************************************
309  *              IShellMalloc_AddRefRelease        [VTABLE]
310  */
311 static ULONG WINAPI IShellMalloc_fnAddRefRelease(LPMALLOC iface)
312 {
313         return 1;
314 }
315
316 /******************************************************************************
317  *              IShellMalloc_Alloc [VTABLE]
318  */
319 static LPVOID WINAPI IShellMalloc_fnAlloc(LPMALLOC iface, DWORD cb)
320 {
321         LPVOID addr;
322
323         addr = (LPVOID) LocalAlloc(LMEM_ZEROINIT, cb);
324         TRACE("(%p,%ld);\n",addr,cb);
325         return addr;
326 }
327
328 /******************************************************************************
329  *              IShellMalloc_Realloc [VTABLE]
330  */
331 static LPVOID WINAPI IShellMalloc_fnRealloc(LPMALLOC iface, LPVOID pv, DWORD cb)
332 {
333         LPVOID addr;
334
335         if (pv) {
336                 if (cb) {
337                         addr = (LPVOID) LocalReAlloc((HANDLE) pv, cb, LMEM_ZEROINIT | LMEM_MOVEABLE);
338                 } else {
339                         LocalFree((HANDLE) pv);
340                         addr = NULL;
341                 }
342         } else {
343                 if (cb) {
344                         addr = (LPVOID) LocalAlloc(LMEM_ZEROINIT, cb);
345                 } else {
346                         addr = NULL;
347                 }
348         }
349
350         TRACE("(%p->%p,%ld)\n",pv,addr,cb);
351         return addr;
352 }
353
354 /******************************************************************************
355  *              IShellMalloc_Free [VTABLE]
356  */
357 static VOID WINAPI IShellMalloc_fnFree(LPMALLOC iface, LPVOID pv)
358 {
359         TRACE("(%p)\n",pv);
360         LocalFree((HANDLE) pv);
361 }
362
363 /******************************************************************************
364  *              IShellMalloc_GetSize [VTABLE]
365  */
366 static DWORD WINAPI IShellMalloc_fnGetSize(LPMALLOC iface, LPVOID pv)
367 {
368         DWORD cb = (DWORD) LocalSize((HANDLE)pv);
369         TRACE("(%p,%ld)\n", pv, cb);
370         return cb;
371 }
372
373 /******************************************************************************
374  *              IShellMalloc_DidAlloc [VTABLE]
375  */
376 static INT WINAPI IShellMalloc_fnDidAlloc(LPMALLOC iface, LPVOID pv)
377 {
378         TRACE("(%p)\n",pv);
379         return -1;
380 }
381
382 /******************************************************************************
383  *              IShellMalloc_HeapMinimize [VTABLE]
384  */
385 static VOID WINAPI IShellMalloc_fnHeapMinimize(LPMALLOC iface)
386 {
387         TRACE("()\n");
388 }
389
390 static const IMallocVtbl VT_Shell_IMalloc32 =
391 {
392         IShellMalloc_fnQueryInterface,
393         IShellMalloc_fnAddRefRelease,
394         IShellMalloc_fnAddRefRelease,
395         IShellMalloc_fnAlloc,
396         IShellMalloc_fnRealloc,
397         IShellMalloc_fnFree,
398         IShellMalloc_fnGetSize,
399         IShellMalloc_fnDidAlloc,
400         IShellMalloc_fnHeapMinimize
401 };
402
403 /*************************************************************************
404  *                       SHGetMalloc                    [SHELL32.@]
405  *
406  * Return the shell IMalloc interface.
407  *
408  * PARAMS
409  *  lpmal [O] Destination for IMalloc interface.
410  *
411  * RETURNS
412  *  Success: S_OK. lpmal contains the shells IMalloc interface.
413  *  Failure. An HRESULT error code.
414  *
415  * NOTES
416  *  This function will use CoGetMalloc() if OLE32.DLL is already loaded.
417  *  If not it uses an internal implementation as a fallback.
418  */
419 HRESULT WINAPI SHGetMalloc(LPMALLOC *lpmal)
420 {
421         TRACE("(%p)\n", lpmal);
422
423         if (!ShellTaskAllocator)
424         {
425                 HMODULE hOle32 = GetModuleHandleA("OLE32.DLL");
426                 /* this is very suspect. we should not being using a different
427                  * allocator from deallocator based on something undeterministic
428                  * like whether ole32 is loaded. as it happens currently, they
429                  * both map to the same allocator deep down, but this could
430                  * change in the future. */
431                 if(hOle32) {
432                         CoGetMalloc(MEMCTX_TASK, &ShellTaskAllocator);
433                         TRACE("got ole32 IMalloc\n");
434                 }
435                 if(!ShellTaskAllocator) {
436                         ShellTaskAllocator = (IMalloc* ) &Shell_Malloc;
437                         TRACE("use fallback allocator\n");
438                 }
439         }
440         *lpmal = ShellTaskAllocator;
441         return  S_OK;
442 }
443
444 /*************************************************************************
445  * SHAlloc                                      [SHELL32.196]
446  *
447  * NOTES
448  *     exported by ordinal
449  */
450 LPVOID WINAPI SHAlloc(DWORD len)
451 {
452         IMalloc * ppv;
453         LPBYTE ret;
454
455         if (!ShellTaskAllocator) SHGetMalloc(&ppv);
456
457         ret = (LPVOID) IMalloc_Alloc(ShellTaskAllocator, len);
458         TRACE("%lu bytes at %p\n",len, ret);
459         return (LPVOID)ret;
460 }
461
462 /*************************************************************************
463  * SHFree                                       [SHELL32.195]
464  *
465  * NOTES
466  *     exported by ordinal
467  */
468 void WINAPI SHFree(LPVOID pv)
469 {
470         IMalloc * ppv;
471
472         TRACE("%p\n",pv);
473         if (!ShellTaskAllocator) SHGetMalloc(&ppv);
474         IMalloc_Free(ShellTaskAllocator, pv);
475 }
476
477 /*************************************************************************
478  * SHGetDesktopFolder                   [SHELL32.@]
479  */
480 HRESULT WINAPI SHGetDesktopFolder(IShellFolder **psf)
481 {
482         HRESULT hres = S_OK;
483         TRACE("\n");
484
485         if(!psf) return E_INVALIDARG;
486         *psf = NULL;
487         hres = ISF_Desktop_Constructor(NULL, &IID_IShellFolder,(LPVOID*)psf);
488
489         TRACE("-- %p->(%p)\n",psf, *psf);
490         return hres;
491 }
492 /**************************************************************************
493  * Default ClassFactory Implementation
494  *
495  * SHCreateDefClassObject
496  *
497  * NOTES
498  *  Helper function for dlls without their own classfactory.
499  *  A generic classfactory is returned.
500  *  When the CreateInstance of the cf is called the callback is executed.
501  */
502
503 typedef struct
504 {
505     const IClassFactoryVtbl    *lpVtbl;
506     LONG                        ref;
507     CLSID                       *rclsid;
508     LPFNCREATEINSTANCE          lpfnCI;
509     const IID *                 riidInst;
510     LONG *                      pcRefDll; /* pointer to refcounter in external dll (ugrrr...) */
511 } IDefClFImpl;
512
513 static const IClassFactoryVtbl dclfvt;
514
515 /**************************************************************************
516  *  IDefClF_fnConstructor
517  */
518
519 IClassFactory * IDefClF_fnConstructor(LPFNCREATEINSTANCE lpfnCI, PLONG pcRefDll, REFIID riidInst)
520 {
521         IDefClFImpl* lpclf;
522
523         lpclf = HeapAlloc(GetProcessHeap(),0,sizeof(IDefClFImpl));
524         lpclf->ref = 1;
525         lpclf->lpVtbl = &dclfvt;
526         lpclf->lpfnCI = lpfnCI;
527         lpclf->pcRefDll = pcRefDll;
528
529         if (pcRefDll) InterlockedIncrement(pcRefDll);
530         lpclf->riidInst = riidInst;
531
532         TRACE("(%p)%s\n",lpclf, shdebugstr_guid(riidInst));
533         return (LPCLASSFACTORY)lpclf;
534 }
535 /**************************************************************************
536  *  IDefClF_fnQueryInterface
537  */
538 static HRESULT WINAPI IDefClF_fnQueryInterface(
539   LPCLASSFACTORY iface, REFIID riid, LPVOID *ppvObj)
540 {
541         IDefClFImpl *This = (IDefClFImpl *)iface;
542
543         TRACE("(%p)->(%s)\n",This,shdebugstr_guid(riid));
544
545         *ppvObj = NULL;
546
547         if(IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IClassFactory)) {
548           *ppvObj = This;
549           InterlockedIncrement(&This->ref);
550           return S_OK;
551         }
552
553         TRACE("-- E_NOINTERFACE\n");
554         return E_NOINTERFACE;
555 }
556 /******************************************************************************
557  * IDefClF_fnAddRef
558  */
559 static ULONG WINAPI IDefClF_fnAddRef(LPCLASSFACTORY iface)
560 {
561         IDefClFImpl *This = (IDefClFImpl *)iface;
562         ULONG refCount = InterlockedIncrement(&This->ref);
563
564         TRACE("(%p)->(count=%lu)\n", This, refCount - 1);
565
566         return refCount;
567 }
568 /******************************************************************************
569  * IDefClF_fnRelease
570  */
571 static ULONG WINAPI IDefClF_fnRelease(LPCLASSFACTORY iface)
572 {
573         IDefClFImpl *This = (IDefClFImpl *)iface;
574         ULONG refCount = InterlockedDecrement(&This->ref);
575         
576         TRACE("(%p)->(count=%lu)\n", This, refCount + 1);
577
578         if (!refCount)
579         {
580           if (This->pcRefDll) InterlockedDecrement(This->pcRefDll);
581
582           TRACE("-- destroying IClassFactory(%p)\n",This);
583           HeapFree(GetProcessHeap(),0,This);
584           return 0;
585         }
586         return refCount;
587 }
588 /******************************************************************************
589  * IDefClF_fnCreateInstance
590  */
591 static HRESULT WINAPI IDefClF_fnCreateInstance(
592   LPCLASSFACTORY iface, LPUNKNOWN pUnkOuter, REFIID riid, LPVOID *ppvObject)
593 {
594         IDefClFImpl *This = (IDefClFImpl *)iface;
595
596         TRACE("%p->(%p,%s,%p)\n",This,pUnkOuter,shdebugstr_guid(riid),ppvObject);
597
598         *ppvObject = NULL;
599
600         if ( This->riidInst==NULL ||
601              IsEqualCLSID(riid, This->riidInst) ||
602              IsEqualCLSID(riid, &IID_IUnknown) )
603         {
604           return This->lpfnCI(pUnkOuter, riid, ppvObject);
605         }
606
607         ERR("unknown IID requested %s\n",shdebugstr_guid(riid));
608         return E_NOINTERFACE;
609 }
610 /******************************************************************************
611  * IDefClF_fnLockServer
612  */
613 static HRESULT WINAPI IDefClF_fnLockServer(LPCLASSFACTORY iface, BOOL fLock)
614 {
615         IDefClFImpl *This = (IDefClFImpl *)iface;
616         TRACE("%p->(0x%x), not implemented\n",This, fLock);
617         return E_NOTIMPL;
618 }
619
620 static const IClassFactoryVtbl dclfvt =
621 {
622     IDefClF_fnQueryInterface,
623     IDefClF_fnAddRef,
624   IDefClF_fnRelease,
625   IDefClF_fnCreateInstance,
626   IDefClF_fnLockServer
627 };
628
629 /******************************************************************************
630  * SHCreateDefClassObject                       [SHELL32.70]
631  */
632 HRESULT WINAPI SHCreateDefClassObject(
633         REFIID  riid,
634         LPVOID* ppv,
635         LPFNCREATEINSTANCE lpfnCI,      /* [in] create instance callback entry */
636         LPDWORD pcRefDll,               /* [in/out] ref count of the dll */
637         REFIID  riidInst)               /* [in] optional interface to the instance */
638 {
639         IClassFactory * pcf;
640
641         TRACE("%s %p %p %p %s\n",
642               shdebugstr_guid(riid), ppv, lpfnCI, pcRefDll, shdebugstr_guid(riidInst));
643
644         if (! IsEqualCLSID(riid, &IID_IClassFactory) ) return E_NOINTERFACE;
645         if (! (pcf = IDefClF_fnConstructor(lpfnCI, pcRefDll, riidInst))) return E_OUTOFMEMORY;
646         *ppv = pcf;
647         return NOERROR;
648 }
649
650 /*************************************************************************
651  *  DragAcceptFiles             [SHELL32.@]
652  */
653 void WINAPI DragAcceptFiles(HWND hWnd, BOOL b)
654 {
655         LONG exstyle;
656
657         if( !IsWindow(hWnd) ) return;
658         exstyle = GetWindowLongA(hWnd,GWL_EXSTYLE);
659         if (b)
660           exstyle |= WS_EX_ACCEPTFILES;
661         else
662           exstyle &= ~WS_EX_ACCEPTFILES;
663         SetWindowLongA(hWnd,GWL_EXSTYLE,exstyle);
664 }
665
666 /*************************************************************************
667  * DragFinish           [SHELL32.@]
668  */
669 void WINAPI DragFinish(HDROP h)
670 {
671         TRACE("\n");
672         GlobalFree((HGLOBAL)h);
673 }
674
675 /*************************************************************************
676  * DragQueryPoint               [SHELL32.@]
677  */
678 BOOL WINAPI DragQueryPoint(HDROP hDrop, POINT *p)
679 {
680         DROPFILES *lpDropFileStruct;
681         BOOL bRet;
682
683         TRACE("\n");
684
685         lpDropFileStruct = (DROPFILES *) GlobalLock(hDrop);
686
687         *p = lpDropFileStruct->pt;
688         bRet = lpDropFileStruct->fNC;
689
690         GlobalUnlock(hDrop);
691         return bRet;
692 }
693
694 /*************************************************************************
695  *  DragQueryFile               [SHELL32.@]
696  *  DragQueryFileA              [SHELL32.@]
697  */
698 UINT WINAPI DragQueryFileA(
699         HDROP hDrop,
700         UINT lFile,
701         LPSTR lpszFile,
702         UINT lLength)
703 {
704         LPSTR lpDrop;
705         UINT i = 0;
706         DROPFILES *lpDropFileStruct = (DROPFILES *) GlobalLock(hDrop);
707
708         TRACE("(%p, %x, %p, %u)\n",     hDrop,lFile,lpszFile,lLength);
709
710         if(!lpDropFileStruct) goto end;
711
712         lpDrop = (LPSTR) lpDropFileStruct + lpDropFileStruct->pFiles;
713
714         if(lpDropFileStruct->fWide) {
715             LPWSTR lpszFileW = NULL;
716
717             if(lpszFile) {
718                 lpszFileW = HeapAlloc(GetProcessHeap(), 0, lLength*sizeof(WCHAR));
719                 if(lpszFileW == NULL) {
720                     goto end;
721                 }
722             }
723             i = DragQueryFileW(hDrop, lFile, lpszFileW, lLength);
724
725             if(lpszFileW) {
726                 WideCharToMultiByte(CP_ACP, 0, lpszFileW, -1, lpszFile, lLength, 0, NULL);
727                 HeapFree(GetProcessHeap(), 0, lpszFileW);
728             }
729             goto end;
730         }
731
732         while (i++ < lFile)
733         {
734           while (*lpDrop++); /* skip filename */
735           if (!*lpDrop)
736           {
737             i = (lFile == 0xFFFFFFFF) ? i : 0;
738             goto end;
739           }
740         }
741
742         i = strlen(lpDrop);
743         i++;
744         if (!lpszFile ) goto end;   /* needed buffer size */
745         i = (lLength > i) ? i : lLength;
746         lstrcpynA (lpszFile,  lpDrop,  i);
747 end:
748         GlobalUnlock(hDrop);
749         return i;
750 }
751
752 /*************************************************************************
753  *  DragQueryFileW              [SHELL32.@]
754  */
755 UINT WINAPI DragQueryFileW(
756         HDROP hDrop,
757         UINT lFile,
758         LPWSTR lpszwFile,
759         UINT lLength)
760 {
761         LPWSTR lpwDrop;
762         UINT i = 0;
763         DROPFILES *lpDropFileStruct = (DROPFILES *) GlobalLock(hDrop);
764
765         TRACE("(%p, %x, %p, %u)\n", hDrop,lFile,lpszwFile,lLength);
766
767         if(!lpDropFileStruct) goto end;
768
769         lpwDrop = (LPWSTR) ((LPSTR)lpDropFileStruct + lpDropFileStruct->pFiles);
770
771         if(lpDropFileStruct->fWide == FALSE) {
772             LPSTR lpszFileA = NULL;
773
774             if(lpszwFile) {
775                 lpszFileA = HeapAlloc(GetProcessHeap(), 0, lLength);
776                 if(lpszFileA == NULL) {
777                     goto end;
778                 }
779             }
780             i = DragQueryFileA(hDrop, lFile, lpszFileA, lLength);
781
782             if(lpszFileA) {
783                 MultiByteToWideChar(CP_ACP, 0, lpszFileA, -1, lpszwFile, lLength);
784                 HeapFree(GetProcessHeap(), 0, lpszFileA);
785             }
786             goto end;
787         }
788
789         i = 0;
790         while (i++ < lFile)
791         {
792           while (*lpwDrop++); /* skip filename */
793           if (!*lpwDrop)
794           {
795             i = (lFile == 0xFFFFFFFF) ? i : 0;
796             goto end;
797           }
798         }
799
800         i = strlenW(lpwDrop);
801         i++;
802         if ( !lpszwFile) goto end;   /* needed buffer size */
803
804         i = (lLength > i) ? i : lLength;
805         lstrcpynW (lpszwFile, lpwDrop, i);
806 end:
807         GlobalUnlock(hDrop);
808         return i;
809 }