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