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