mshtml: Added IMG element tests.
[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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 static const WCHAR sShell32[12] = {'S','H','E','L','L','3','2','.','D','L','L','\0'};
53
54 /**************************************************************************
55  * Default ClassFactory types
56  */
57 typedef HRESULT (CALLBACK *LPFNCREATEINSTANCE)(IUnknown* pUnkOuter, REFIID riid, LPVOID* ppvObject);
58 static IClassFactory * IDefClF_fnConstructor(LPFNCREATEINSTANCE lpfnCI, PLONG pcRefDll, REFIID riidInst);
59
60 /* this table contains all CLSID's of shell32 objects */
61 static const struct {
62         REFIID                  riid;
63         LPFNCREATEINSTANCE      lpfnCI;
64 } InterfaceTable[] = {
65         {&CLSID_ShellFSFolder,  &IFSFolder_Constructor},
66         {&CLSID_MyComputer,     &ISF_MyComputer_Constructor},
67         {&CLSID_NetworkPlaces,  &ISF_NetworkPlaces_Constructor},
68         {&CLSID_ShellDesktop,   &ISF_Desktop_Constructor},
69         {&CLSID_ShellLink,      &IShellLink_Constructor},
70         {&CLSID_DragDropHelper, &IDropTargetHelper_Constructor},
71         {&CLSID_ControlPanel,   &IControlPanel_Constructor},
72         {&CLSID_AutoComplete,   &IAutoComplete_Constructor},
73         {&CLSID_UnixFolder,     &UnixFolder_Constructor},
74         {&CLSID_UnixDosFolder,  &UnixDosFolder_Constructor},
75         {&CLSID_FolderShortcut, &FolderShortcut_Constructor},
76         {&CLSID_MyDocuments,    &MyDocuments_Constructor},
77         {&CLSID_RecycleBin,     &RecycleBin_Constructor},
78         {NULL,NULL}
79 };
80
81
82 /* FIXME: this should be SHLWAPI.24 since we can't yet import by ordinal */
83
84 DWORD WINAPI __SHGUIDToStringW (REFGUID guid, LPWSTR str)
85 {
86     WCHAR sFormat[52] = {'{','%','0','8','l','x','-','%','0','4',
87                          'x','-','%','0','4','x','-','%','0','2',
88                          'x','%','0','2','x','-','%','0','2','x',
89                          '%','0','2','x','%','0','2','x','%','0',
90                          '2','x','%','0','2','x','%','0','2','x',
91                          '}','\0'};
92
93     return wsprintfW ( str, sFormat,
94              guid->Data1, guid->Data2, guid->Data3,
95              guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
96              guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7] );
97
98 }
99
100 /*************************************************************************
101  * SHCoCreateInstance [SHELL32.102]
102  *
103  * Equivalent to CoCreateInstance. Under Windows 9x this function could sometimes
104  * use the shell32 built-in "mini-COM" without the need to load ole32.dll - see
105  * SHLoadOLE for details.
106  *
107  * Under wine if a "LoadWithoutCOM" value is present or the object resides in
108  * shell32.dll the function will load the object manually without the help of ole32
109  *
110  * NOTES
111  *     exported by ordinal
112  *
113  * SEE ALSO
114  *     CoCreateInstace, SHLoadOLE
115  */
116 HRESULT WINAPI SHCoCreateInstance(
117         LPCWSTR aclsid,
118         const CLSID *clsid,
119         LPUNKNOWN pUnkOuter,
120         REFIID refiid,
121         LPVOID *ppv)
122 {
123         DWORD   hres;
124         IID     iid;
125         const   CLSID * myclsid = clsid;
126         WCHAR   sKeyName[MAX_PATH];
127         const   WCHAR sCLSID[7] = {'C','L','S','I','D','\\','\0'};
128         WCHAR   sClassID[60];
129         const WCHAR sInProcServer32[16] ={'\\','I','n','p','r','o','c','S','e','r','v','e','r','3','2','\0'};
130         const WCHAR sLoadWithoutCOM[15] ={'L','o','a','d','W','i','t','h','o','u','t','C','O','M','\0'};
131         WCHAR   sDllPath[MAX_PATH];
132         HKEY    hKey = 0;
133         DWORD   dwSize;
134         IClassFactory * pcf = NULL;
135
136         if(!ppv) return E_POINTER;
137         *ppv=NULL;
138
139         /* if the clsid is a string, convert it */
140         if (!clsid)
141         {
142           if (!aclsid) return REGDB_E_CLASSNOTREG;
143           SHCLSIDFromStringW(aclsid, &iid);
144           myclsid = &iid;
145         }
146
147         TRACE("(%p,%s,unk:%p,%s,%p)\n",
148                 aclsid,shdebugstr_guid(myclsid),pUnkOuter,shdebugstr_guid(refiid),ppv);
149
150         if (SUCCEEDED(DllGetClassObject(myclsid, &IID_IClassFactory,(LPVOID*)&pcf)))
151         {
152             hres = IClassFactory_CreateInstance(pcf, pUnkOuter, refiid, ppv);
153             IClassFactory_Release(pcf);
154             goto end;
155         }
156
157         /* we look up the dll path in the registry */
158         __SHGUIDToStringW(myclsid, sClassID);
159         lstrcpyW(sKeyName, sCLSID);
160         lstrcatW(sKeyName, sClassID);
161         lstrcatW(sKeyName, sInProcServer32);
162
163         if (RegOpenKeyExW(HKEY_CLASSES_ROOT, sKeyName, 0, KEY_READ, &hKey))
164             return E_ACCESSDENIED;
165
166         /* if a special registry key is set, we load a shell extension without help of OLE32 */
167         if (!SHQueryValueExW(hKey, sLoadWithoutCOM, 0, 0, 0, 0))
168         {
169             /* load an external dll without ole32 */
170             HANDLE hLibrary;
171             typedef HRESULT (CALLBACK *DllGetClassObjectFunc)(REFCLSID clsid, REFIID iid, LPVOID *ppv);
172             DllGetClassObjectFunc DllGetClassObject;
173
174             dwSize = sizeof(sDllPath);
175             SHQueryValueExW(hKey, NULL, 0,0, sDllPath, &dwSize );
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%08x\n", hres);
188                     goto end;
189             }
190
191             hres = IClassFactory_CreateInstance(pcf, pUnkOuter, refiid, ppv);
192             IClassFactory_Release(pcf);
193         } else {
194
195             /* load an external dll in the usual way */
196             hres = CoCreateInstance(myclsid, pUnkOuter, CLSCTX_INPROC_SERVER, refiid, ppv);
197         }
198
199 end:
200         if (hKey) RegCloseKey(hKey);
201         if(hres!=S_OK)
202         {
203           ERR("failed (0x%08x) to create CLSID:%s IID:%s\n",
204               hres, shdebugstr_guid(myclsid), shdebugstr_guid(refiid));
205           ERR("class not found in registry\n");
206         }
207
208         TRACE("-- instance: %p\n",*ppv);
209         return hres;
210 }
211
212 /*************************************************************************
213  * DllGetClassObject     [SHELL32.@]
214  * SHDllGetClassObject   [SHELL32.128]
215  */
216 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID iid, LPVOID *ppv)
217 {
218         HRESULT hres = E_OUTOFMEMORY;
219         IClassFactory * pcf = NULL;
220         int i;
221
222         TRACE("CLSID:%s,IID:%s\n",shdebugstr_guid(rclsid),shdebugstr_guid(iid));
223
224         if (!ppv) return E_INVALIDARG;
225         *ppv = NULL;
226
227         /* search our internal interface table */
228         for(i=0;InterfaceTable[i].riid;i++) {
229             if(IsEqualIID(InterfaceTable[i].riid, rclsid)) {
230                 TRACE("index[%u]\n", i);
231                 pcf = IDefClF_fnConstructor(InterfaceTable[i].lpfnCI, NULL, NULL);
232             }
233         }
234
235         if (!pcf) {
236             FIXME("failed for CLSID=%s\n", shdebugstr_guid(rclsid));
237             return CLASS_E_CLASSNOTAVAILABLE;
238         }
239
240         hres = IClassFactory_QueryInterface(pcf, iid, ppv);
241         IClassFactory_Release(pcf);
242
243         TRACE("-- pointer to class factory: %p\n",*ppv);
244         return hres;
245 }
246
247 /*************************************************************************
248  * SHCLSIDFromString                            [SHELL32.147]
249  *
250  * Under Windows 9x this was an ANSI version of CLSIDFromString. It also allowed
251  * to avoid dependency on ole32.dll (see SHLoadOLE for details).
252  *
253  * Under Windows NT/2000/XP this is equivalent to CLSIDFromString
254  *
255  * NOTES
256  *     exported by ordinal
257  *
258  * SEE ALSO
259  *     CLSIDFromString, SHLoadOLE
260  */
261 DWORD WINAPI SHCLSIDFromStringA (LPCSTR clsid, CLSID *id)
262 {
263     WCHAR buffer[40];
264     TRACE("(%p(%s) %p)\n", clsid, clsid, id);
265     if (!MultiByteToWideChar( CP_ACP, 0, clsid, -1, buffer, sizeof(buffer)/sizeof(WCHAR) ))
266         return CO_E_CLASSSTRING;
267     return CLSIDFromString( buffer, id );
268 }
269 DWORD WINAPI SHCLSIDFromStringW (LPCWSTR clsid, CLSID *id)
270 {
271         TRACE("(%p(%s) %p)\n", clsid, debugstr_w(clsid), id);
272         return CLSIDFromString((LPWSTR)clsid, id);
273 }
274 DWORD WINAPI SHCLSIDFromStringAW (LPCVOID clsid, CLSID *id)
275 {
276         if (SHELL_OsIsUnicode())
277           return SHCLSIDFromStringW (clsid, id);
278         return SHCLSIDFromStringA (clsid, id);
279 }
280
281 /*************************************************************************
282  *                       SHGetMalloc                    [SHELL32.@]
283  *
284  * Equivalent to CoGetMalloc(MEMCTX_TASK, ...). Under Windows 9x this function
285  * could use the shell32 built-in "mini-COM" without the need to load ole32.dll -
286  * see SHLoadOLE for details. 
287  *
288  * PARAMS
289  *  lpmal [O] Destination for IMalloc interface.
290  *
291  * RETURNS
292  *  Success: S_OK. lpmal contains the shells IMalloc interface.
293  *  Failure. An HRESULT error code.
294  *
295  * SEE ALSO
296  *  CoGetMalloc, SHLoadOLE
297  */
298 HRESULT WINAPI SHGetMalloc(LPMALLOC *lpmal)
299 {
300         TRACE("(%p)\n", lpmal);
301         return CoGetMalloc(MEMCTX_TASK, lpmal);
302 }
303
304 /*************************************************************************
305  * SHAlloc                                      [SHELL32.196]
306  *
307  * Equivalent to CoTaskMemAlloc. Under Windows 9x this function could use
308  * the shell32 built-in "mini-COM" without the need to load ole32.dll -
309  * see SHLoadOLE for details. 
310  *
311  * NOTES
312  *     exported by ordinal
313  *
314  * SEE ALSO
315  *     CoTaskMemAlloc, SHLoadOLE
316  */
317 LPVOID WINAPI SHAlloc(DWORD len)
318 {
319         LPVOID ret;
320
321         ret = CoTaskMemAlloc(len);
322         TRACE("%u bytes at %p\n",len, ret);
323         return ret;
324 }
325
326 /*************************************************************************
327  * SHFree                                       [SHELL32.195]
328  *
329  * Equivalent to CoTaskMemFree. Under Windows 9x this function could use
330  * the shell32 built-in "mini-COM" without the need to load ole32.dll -
331  * see SHLoadOLE for details. 
332  *
333  * NOTES
334  *     exported by ordinal
335  *
336  * SEE ALSO
337  *     CoTaskMemFree, SHLoadOLE
338  */
339 void WINAPI SHFree(LPVOID pv)
340 {
341         TRACE("%p\n",pv);
342         CoTaskMemFree(pv);
343 }
344
345 /*************************************************************************
346  * SHGetDesktopFolder                   [SHELL32.@]
347  */
348 HRESULT WINAPI SHGetDesktopFolder(IShellFolder **psf)
349 {
350         HRESULT hres = S_OK;
351         TRACE("\n");
352
353         if(!psf) return E_INVALIDARG;
354         *psf = NULL;
355         hres = ISF_Desktop_Constructor(NULL, &IID_IShellFolder,(LPVOID*)psf);
356
357         TRACE("-- %p->(%p)\n",psf, *psf);
358         return hres;
359 }
360 /**************************************************************************
361  * Default ClassFactory Implementation
362  *
363  * SHCreateDefClassObject
364  *
365  * NOTES
366  *  Helper function for dlls without their own classfactory.
367  *  A generic classfactory is returned.
368  *  When the CreateInstance of the cf is called the callback is executed.
369  */
370
371 typedef struct
372 {
373     const IClassFactoryVtbl    *lpVtbl;
374     LONG                        ref;
375     CLSID                       *rclsid;
376     LPFNCREATEINSTANCE          lpfnCI;
377     const IID *                 riidInst;
378     LONG *                      pcRefDll; /* pointer to refcounter in external dll (ugrrr...) */
379 } IDefClFImpl;
380
381 static const IClassFactoryVtbl dclfvt;
382
383 /**************************************************************************
384  *  IDefClF_fnConstructor
385  */
386
387 static IClassFactory * IDefClF_fnConstructor(LPFNCREATEINSTANCE lpfnCI, PLONG pcRefDll, REFIID riidInst)
388 {
389         IDefClFImpl* lpclf;
390
391         lpclf = HeapAlloc(GetProcessHeap(),0,sizeof(IDefClFImpl));
392         lpclf->ref = 1;
393         lpclf->lpVtbl = &dclfvt;
394         lpclf->lpfnCI = lpfnCI;
395         lpclf->pcRefDll = pcRefDll;
396
397         if (pcRefDll) InterlockedIncrement(pcRefDll);
398         lpclf->riidInst = riidInst;
399
400         TRACE("(%p)%s\n",lpclf, shdebugstr_guid(riidInst));
401         return (LPCLASSFACTORY)lpclf;
402 }
403 /**************************************************************************
404  *  IDefClF_fnQueryInterface
405  */
406 static HRESULT WINAPI IDefClF_fnQueryInterface(
407   LPCLASSFACTORY iface, REFIID riid, LPVOID *ppvObj)
408 {
409         IDefClFImpl *This = (IDefClFImpl *)iface;
410
411         TRACE("(%p)->(%s)\n",This,shdebugstr_guid(riid));
412
413         *ppvObj = NULL;
414
415         if(IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IClassFactory)) {
416           *ppvObj = This;
417           InterlockedIncrement(&This->ref);
418           return S_OK;
419         }
420
421         TRACE("-- E_NOINTERFACE\n");
422         return E_NOINTERFACE;
423 }
424 /******************************************************************************
425  * IDefClF_fnAddRef
426  */
427 static ULONG WINAPI IDefClF_fnAddRef(LPCLASSFACTORY iface)
428 {
429         IDefClFImpl *This = (IDefClFImpl *)iface;
430         ULONG refCount = InterlockedIncrement(&This->ref);
431
432         TRACE("(%p)->(count=%u)\n", This, refCount - 1);
433
434         return refCount;
435 }
436 /******************************************************************************
437  * IDefClF_fnRelease
438  */
439 static ULONG WINAPI IDefClF_fnRelease(LPCLASSFACTORY iface)
440 {
441         IDefClFImpl *This = (IDefClFImpl *)iface;
442         ULONG refCount = InterlockedDecrement(&This->ref);
443         
444         TRACE("(%p)->(count=%u)\n", This, refCount + 1);
445
446         if (!refCount)
447         {
448           if (This->pcRefDll) InterlockedDecrement(This->pcRefDll);
449
450           TRACE("-- destroying IClassFactory(%p)\n",This);
451           HeapFree(GetProcessHeap(),0,This);
452           return 0;
453         }
454         return refCount;
455 }
456 /******************************************************************************
457  * IDefClF_fnCreateInstance
458  */
459 static HRESULT WINAPI IDefClF_fnCreateInstance(
460   LPCLASSFACTORY iface, LPUNKNOWN pUnkOuter, REFIID riid, LPVOID *ppvObject)
461 {
462         IDefClFImpl *This = (IDefClFImpl *)iface;
463
464         TRACE("%p->(%p,%s,%p)\n",This,pUnkOuter,shdebugstr_guid(riid),ppvObject);
465
466         *ppvObject = NULL;
467
468         if ( This->riidInst==NULL ||
469              IsEqualCLSID(riid, This->riidInst) ||
470              IsEqualCLSID(riid, &IID_IUnknown) )
471         {
472           return This->lpfnCI(pUnkOuter, riid, ppvObject);
473         }
474
475         ERR("unknown IID requested %s\n",shdebugstr_guid(riid));
476         return E_NOINTERFACE;
477 }
478 /******************************************************************************
479  * IDefClF_fnLockServer
480  */
481 static HRESULT WINAPI IDefClF_fnLockServer(LPCLASSFACTORY iface, BOOL fLock)
482 {
483         IDefClFImpl *This = (IDefClFImpl *)iface;
484         TRACE("%p->(0x%x), not implemented\n",This, fLock);
485         return E_NOTIMPL;
486 }
487
488 static const IClassFactoryVtbl dclfvt =
489 {
490     IDefClF_fnQueryInterface,
491     IDefClF_fnAddRef,
492   IDefClF_fnRelease,
493   IDefClF_fnCreateInstance,
494   IDefClF_fnLockServer
495 };
496
497 /******************************************************************************
498  * SHCreateDefClassObject                       [SHELL32.70]
499  */
500 HRESULT WINAPI SHCreateDefClassObject(
501         REFIID  riid,
502         LPVOID* ppv,
503         LPFNCREATEINSTANCE lpfnCI,      /* [in] create instance callback entry */
504         LPDWORD pcRefDll,               /* [in/out] ref count of the dll */
505         REFIID  riidInst)               /* [in] optional interface to the instance */
506 {
507         IClassFactory * pcf;
508
509         TRACE("%s %p %p %p %s\n",
510               shdebugstr_guid(riid), ppv, lpfnCI, pcRefDll, shdebugstr_guid(riidInst));
511
512         if (! IsEqualCLSID(riid, &IID_IClassFactory) ) return E_NOINTERFACE;
513         if (! (pcf = IDefClF_fnConstructor(lpfnCI, (PLONG)pcRefDll, riidInst))) return E_OUTOFMEMORY;
514         *ppv = pcf;
515         return NOERROR;
516 }
517
518 /*************************************************************************
519  *  DragAcceptFiles             [SHELL32.@]
520  */
521 void WINAPI DragAcceptFiles(HWND hWnd, BOOL b)
522 {
523         LONG exstyle;
524
525         if( !IsWindow(hWnd) ) return;
526         exstyle = GetWindowLongA(hWnd,GWL_EXSTYLE);
527         if (b)
528           exstyle |= WS_EX_ACCEPTFILES;
529         else
530           exstyle &= ~WS_EX_ACCEPTFILES;
531         SetWindowLongA(hWnd,GWL_EXSTYLE,exstyle);
532 }
533
534 /*************************************************************************
535  * DragFinish           [SHELL32.@]
536  */
537 void WINAPI DragFinish(HDROP h)
538 {
539         TRACE("\n");
540         GlobalFree((HGLOBAL)h);
541 }
542
543 /*************************************************************************
544  * DragQueryPoint               [SHELL32.@]
545  */
546 BOOL WINAPI DragQueryPoint(HDROP hDrop, POINT *p)
547 {
548         DROPFILES *lpDropFileStruct;
549         BOOL bRet;
550
551         TRACE("\n");
552
553         lpDropFileStruct = (DROPFILES *) GlobalLock(hDrop);
554
555         *p = lpDropFileStruct->pt;
556         bRet = lpDropFileStruct->fNC;
557
558         GlobalUnlock(hDrop);
559         return bRet;
560 }
561
562 /*************************************************************************
563  *  DragQueryFileA              [SHELL32.@]
564  *  DragQueryFile               [SHELL32.@]
565  */
566 UINT WINAPI DragQueryFileA(
567         HDROP hDrop,
568         UINT lFile,
569         LPSTR lpszFile,
570         UINT lLength)
571 {
572         LPSTR lpDrop;
573         UINT i = 0;
574         DROPFILES *lpDropFileStruct = (DROPFILES *) GlobalLock(hDrop);
575
576         TRACE("(%p, %x, %p, %u)\n",     hDrop,lFile,lpszFile,lLength);
577
578         if(!lpDropFileStruct) goto end;
579
580         lpDrop = (LPSTR) lpDropFileStruct + lpDropFileStruct->pFiles;
581
582         if(lpDropFileStruct->fWide) {
583             LPWSTR lpszFileW = NULL;
584
585             if(lpszFile) {
586                 lpszFileW = HeapAlloc(GetProcessHeap(), 0, lLength*sizeof(WCHAR));
587                 if(lpszFileW == NULL) {
588                     goto end;
589                 }
590             }
591             i = DragQueryFileW(hDrop, lFile, lpszFileW, lLength);
592
593             if(lpszFileW) {
594                 WideCharToMultiByte(CP_ACP, 0, lpszFileW, -1, lpszFile, lLength, 0, NULL);
595                 HeapFree(GetProcessHeap(), 0, lpszFileW);
596             }
597             goto end;
598         }
599
600         while (i++ < lFile)
601         {
602           while (*lpDrop++); /* skip filename */
603           if (!*lpDrop)
604           {
605             i = (lFile == 0xFFFFFFFF) ? i : 0;
606             goto end;
607           }
608         }
609
610         i = strlen(lpDrop);
611         if (!lpszFile ) goto end;   /* needed buffer size */
612         lstrcpynA (lpszFile, lpDrop, lLength);
613 end:
614         GlobalUnlock(hDrop);
615         return i;
616 }
617
618 /*************************************************************************
619  *  DragQueryFileW              [SHELL32.@]
620  */
621 UINT WINAPI DragQueryFileW(
622         HDROP hDrop,
623         UINT lFile,
624         LPWSTR lpszwFile,
625         UINT lLength)
626 {
627         LPWSTR lpwDrop;
628         UINT i = 0;
629         DROPFILES *lpDropFileStruct = (DROPFILES *) GlobalLock(hDrop);
630
631         TRACE("(%p, %x, %p, %u)\n", hDrop,lFile,lpszwFile,lLength);
632
633         if(!lpDropFileStruct) goto end;
634
635         lpwDrop = (LPWSTR) ((LPSTR)lpDropFileStruct + lpDropFileStruct->pFiles);
636
637         if(lpDropFileStruct->fWide == FALSE) {
638             LPSTR lpszFileA = NULL;
639
640             if(lpszwFile) {
641                 lpszFileA = HeapAlloc(GetProcessHeap(), 0, lLength);
642                 if(lpszFileA == NULL) {
643                     goto end;
644                 }
645             }
646             i = DragQueryFileA(hDrop, lFile, lpszFileA, lLength);
647
648             if(lpszFileA) {
649                 MultiByteToWideChar(CP_ACP, 0, lpszFileA, -1, lpszwFile, lLength);
650                 HeapFree(GetProcessHeap(), 0, lpszFileA);
651             }
652             goto end;
653         }
654
655         i = 0;
656         while (i++ < lFile)
657         {
658           while (*lpwDrop++); /* skip filename */
659           if (!*lpwDrop)
660           {
661             i = (lFile == 0xFFFFFFFF) ? i : 0;
662             goto end;
663           }
664         }
665
666         i = strlenW(lpwDrop);
667         if ( !lpszwFile) goto end;   /* needed buffer size */
668         lstrcpynW (lpszwFile, lpwDrop, lLength);
669 end:
670         GlobalUnlock(hDrop);
671         return i;
672 }