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