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