shellole.c 19.2 KB
Newer Older
Alexandre Julliard's avatar
Alexandre Julliard committed
1
/*
2
 *	handling of SHELL32.DLL OLE-Objects
Alexandre Julliard's avatar
Alexandre Julliard committed
3 4 5 6
 *
 *	Copyright 1997	Marcus Meissner
 *	Copyright 1998	Juergen Schmied  <juergen.schmied@metronet.de>
 *
7 8 9 10 11 12 13 14 15 16 17 18
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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
Alexandre Julliard's avatar
Alexandre Julliard committed
20 21
 */

22 23
#include "config.h"

24
#include <stdarg.h>
Alexandre Julliard's avatar
Alexandre Julliard committed
25 26
#include <stdlib.h>
#include <string.h>
27

28 29
#define COBJMACROS

30 31
#include "windef.h"
#include "winbase.h"
32
#include "shellapi.h"
33 34
#include "wingdi.h"
#include "winuser.h"
35
#include "shlobj.h"
36
#include "shlguid.h"
37 38
#include "winreg.h"
#include "winerror.h"
39

40
#include "undocshell.h"
41
#include "wine/unicode.h"
42 43
#include "shell32_main.h"

44
#include "wine/debug.h"
45
#include "shlwapi.h"
46
#include "debughlp.h"
47

48
WINE_DEFAULT_DEBUG_CHANNEL(shell);
49

50 51
extern HRESULT WINAPI IFSFolder_Constructor(IUnknown * pUnkOuter, REFIID riid, LPVOID * ppv);

52
static const WCHAR sShell32[12] = {'S','H','E','L','L','3','2','.','D','L','L','\0'};
53 54 55 56 57

/**************************************************************************
 * Default ClassFactory types
 */
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 63
	REFIID			riid;
	LPFNCREATEINSTANCE	lpfnCI;
64
} InterfaceTable[] = {
65 66 67 68
	{&CLSID_ShellFSFolder,	&IFSFolder_Constructor},
	{&CLSID_MyComputer,	&ISF_MyComputer_Constructor},
	{&CLSID_ShellDesktop,	&ISF_Desktop_Constructor},
	{&CLSID_ShellLink,	&IShellLink_Constructor},
69
	{&CLSID_DragDropHelper, &IDropTargetHelper_Constructor},
70
	{&CLSID_ControlPanel,	&IControlPanel_Constructor},
71
	{&CLSID_AutoComplete,   &IAutoComplete_Constructor},
72
	{&CLSID_UnixFolder,     &UnixFolder_Constructor},
73
	{&CLSID_UnixDosFolder,  &UnixDosFolder_Constructor},
74
	{&CLSID_FolderShortcut, &FolderShortcut_Constructor},
75
	{&CLSID_MyDocuments,    &MyDocuments_Constructor},
76
	{&CLSID_RecycleBin,     &RecycleBin_Constructor},
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
	{NULL,NULL}
};


/* FIXME: this should be SHLWAPI.24 since we can't yet import by ordinal */

DWORD WINAPI __SHGUIDToStringW (REFGUID guid, LPWSTR str)
{
    WCHAR sFormat[52] = {'{','%','0','8','l','x','-','%','0','4',
		         'x','-','%','0','4','x','-','%','0','2',
                         'x','%','0','2','x','-','%','0','2','x',
			 '%','0','2','x','%','0','2','x','%','0',
			 '2','x','%','0','2','x','%','0','2','x',
			 '}','\0'};

    return wsprintfW ( str, sFormat,
             guid->Data1, guid->Data2, guid->Data3,
             guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
             guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7] );

}

99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
/*************************************************************************
 * SHCoCreateInstance [SHELL32.102]
 *
 * Equivalent to CoCreateInstance. Under Windows 9x this function could sometimes
 * use the shell32 built-in "mini-COM" without the need to load ole32.dll - see
 * SHLoadOLE for details.
 *
 * Under wine if a "LoadWithoutCOM" value is present or the object resides in
 * shell32.dll the function will load the object manually without the help of ole32
 *
 * NOTES
 *     exported by ordinal
 *
 * SEE ALSO
 *     CoCreateInstace, SHLoadOLE
 */
115
HRESULT WINAPI SHCoCreateInstance(
116 117
	LPCWSTR aclsid,
	const CLSID *clsid,
118
	LPUNKNOWN pUnkOuter,
119 120 121
	REFIID refiid,
	LPVOID *ppv)
{
Alexandre Julliard's avatar
Alexandre Julliard committed
122
	DWORD	hres;
123
	IID	iid;
124
	const	CLSID * myclsid = clsid;
125 126 127 128 129 130 131 132 133 134 135 136
	WCHAR	sKeyName[MAX_PATH];
	const	WCHAR sCLSID[7] = {'C','L','S','I','D','\\','\0'};
	WCHAR	sClassID[60];
	const WCHAR sInProcServer32[16] ={'\\','I','n','p','r','o','c','S','e','r','v','e','r','3','2','\0'};
	const WCHAR sLoadWithoutCOM[15] ={'L','o','a','d','W','i','t','h','o','u','t','C','O','M','\0'};
	WCHAR	sDllPath[MAX_PATH];
	HKEY	hKey;
	DWORD	dwSize;
	BOOLEAN bLoadFromShell32 = FALSE;
	BOOLEAN bLoadWithoutCOM = FALSE;
	IClassFactory * pcf = NULL;

137 138 139
	if(!ppv) return E_POINTER;
	*ppv=NULL;

140
	/* if the clsid is a string, convert it */
141 142 143
	if (!clsid)
	{
	  if (!aclsid) return REGDB_E_CLASSNOTREG;
144
	  SHCLSIDFromStringW(aclsid, &iid);
145
	  myclsid = &iid;
Alexandre Julliard's avatar
Alexandre Julliard committed
146 147
	}

148 149
	TRACE("(%p,%s,unk:%p,%s,%p)\n",
		aclsid,shdebugstr_guid(myclsid),pUnkOuter,shdebugstr_guid(refiid),ppv);
Alexandre Julliard's avatar
Alexandre Julliard committed
150

151 152 153 154 155 156 157 158 159 160
	/* we look up the dll path in the registry */
        __SHGUIDToStringW(myclsid, sClassID);
	lstrcpyW(sKeyName, sCLSID);
	lstrcatW(sKeyName, sClassID);
	lstrcatW(sKeyName, sInProcServer32);

	if (ERROR_SUCCESS == RegOpenKeyExW(HKEY_CLASSES_ROOT, sKeyName, 0, KEY_READ, &hKey)) {
	    dwSize = sizeof(sDllPath);
	    SHQueryValueExW(hKey, NULL, 0,0, sDllPath, &dwSize );

Andreas Mohr's avatar
Andreas Mohr committed
161
	    /* if a special registry key is set, we load a shell extension without help of OLE32 */
162 163
	    bLoadWithoutCOM = (ERROR_SUCCESS == SHQueryValueExW(hKey, sLoadWithoutCOM, 0, 0, 0, 0));

Andreas Mohr's avatar
Andreas Mohr committed
164
	    /* if the com object is inside shell32, omit use of ole32 */
165 166 167 168 169 170
	    bLoadFromShell32 = (0==lstrcmpiW( PathFindFileNameW(sDllPath), sShell32));

	    RegCloseKey (hKey);
	} else {
	    /* since we can't find it in the registry we try internally */
	    bLoadFromShell32 = TRUE;
171
	}
172 173 174

	TRACE("WithoutCom=%u FromShell=%u\n", bLoadWithoutCOM, bLoadFromShell32);

175
	/* now we create an instance */
176
	if (bLoadFromShell32) {
177
	    if (! SUCCEEDED(DllGetClassObject(myclsid, &IID_IClassFactory,(LPVOID*)&pcf))) {
178
	        ERR("LoadFromShell failed for CLSID=%s\n", shdebugstr_guid(myclsid));
179 180 181
	    }
	} else if (bLoadWithoutCOM) {

182
	    /* load an external dll without ole32 */
183 184 185 186 187 188 189 190 191 192 193 194 195 196
	    HANDLE hLibrary;
	    typedef HRESULT (CALLBACK *DllGetClassObjectFunc)(REFCLSID clsid, REFIID iid, LPVOID *ppv);
	    DllGetClassObjectFunc DllGetClassObject;

	    if ((hLibrary = LoadLibraryExW(sDllPath, 0, LOAD_WITH_ALTERED_SEARCH_PATH)) == 0) {
	        ERR("couldn't load InprocServer32 dll %s\n", debugstr_w(sDllPath));
		hres = E_ACCESSDENIED;
	        goto end;
	    } else if (!(DllGetClassObject = (DllGetClassObjectFunc)GetProcAddress(hLibrary, "DllGetClassObject"))) {
	        ERR("couldn't find function DllGetClassObject in %s\n", debugstr_w(sDllPath));
	        FreeLibrary( hLibrary );
		hres = E_ACCESSDENIED;
	        goto end;
	    } else if (! SUCCEEDED(hres = DllGetClassObject(myclsid, &IID_IClassFactory, (LPVOID*)&pcf))) {
197
		    TRACE("GetClassObject failed 0x%08x\n", hres);
198 199 200 201 202
		    goto end;
	    }

	} else {

203
	    /* load an external dll in the usual way */
204
	    hres = CoCreateInstance(myclsid, pUnkOuter, CLSCTX_INPROC_SERVER, refiid, ppv);
205
	    goto end;
206
	}
207

208 209 210 211 212 213
	/* here we should have a ClassFactory */
	if (!pcf) return E_ACCESSDENIED;

	hres = IClassFactory_CreateInstance(pcf, pUnkOuter, refiid, ppv);
	IClassFactory_Release(pcf);
end:
214 215
	if(hres!=S_OK)
	{
216
	  ERR("failed (0x%08x) to create CLSID:%s IID:%s\n",
217
              hres, shdebugstr_guid(myclsid), shdebugstr_guid(refiid));
218
	  ERR("class not found in registry\n");
Alexandre Julliard's avatar
Alexandre Julliard committed
219
	}
220

221
	TRACE("-- instance: %p\n",*ppv);
Alexandre Julliard's avatar
Alexandre Julliard committed
222 223 224 225
	return hres;
}

/*************************************************************************
226 227
 * DllGetClassObject     [SHELL32.@]
 * SHDllGetClassObject   [SHELL32.128]
Alexandre Julliard's avatar
Alexandre Julliard committed
228
 */
229
HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID iid, LPVOID *ppv)
230 231 232 233
{
	HRESULT	hres = E_OUTOFMEMORY;
	IClassFactory * pcf = NULL;
	int i;
Alexandre Julliard's avatar
Alexandre Julliard committed
234

235
	TRACE("CLSID:%s,IID:%s\n",shdebugstr_guid(rclsid),shdebugstr_guid(iid));
236

237
	if (!ppv) return E_INVALIDARG;
Alexandre Julliard's avatar
Alexandre Julliard committed
238
	*ppv = NULL;
239

240 241 242 243
	/* search our internal interface table */
	for(i=0;InterfaceTable[i].riid;i++) {
	    if(IsEqualIID(InterfaceTable[i].riid, rclsid)) {
	        TRACE("index[%u]\n", i);
244
	        pcf = IDefClF_fnConstructor(InterfaceTable[i].lpfnCI, NULL, NULL);
245
	    }
Alexandre Julliard's avatar
Alexandre Julliard committed
246
	}
247 248

        if (!pcf) {
249
	    FIXME("failed for CLSID=%s\n", shdebugstr_guid(rclsid));
250
	    return CLASS_E_CLASSNOTAVAILABLE;
Alexandre Julliard's avatar
Alexandre Julliard committed
251
	}
252 253 254 255

	hres = IClassFactory_QueryInterface(pcf, iid, ppv);
	IClassFactory_Release(pcf);

256
	TRACE("-- pointer to class factory: %p\n",*ppv);
Alexandre Julliard's avatar
Alexandre Julliard committed
257 258 259
	return hres;
}

260 261 262
/*************************************************************************
 * SHCLSIDFromString				[SHELL32.147]
 *
263 264 265 266 267
 * Under Windows 9x this was an ANSI version of CLSIDFromString. It also allowed
 * to avoid dependency on ole32.dll (see SHLoadOLE for details).
 *
 * Under Windows NT/2000/XP this is equivalent to CLSIDFromString
 *
268 269
 * NOTES
 *     exported by ordinal
270 271 272
 *
 * SEE ALSO
 *     CLSIDFromString, SHLoadOLE
273
 */
274
DWORD WINAPI SHCLSIDFromStringA (LPCSTR clsid, CLSID *id)
275
{
276 277 278 279 280
    WCHAR buffer[40];
    TRACE("(%p(%s) %p)\n", clsid, clsid, id);
    if (!MultiByteToWideChar( CP_ACP, 0, clsid, -1, buffer, sizeof(buffer)/sizeof(WCHAR) ))
        return CO_E_CLASSSTRING;
    return CLSIDFromString( buffer, id );
281
}
282
DWORD WINAPI SHCLSIDFromStringW (LPCWSTR clsid, CLSID *id)
283 284
{
	TRACE("(%p(%s) %p)\n", clsid, debugstr_w(clsid), id);
285
	return CLSIDFromString((LPWSTR)clsid, id);
286 287 288
}
DWORD WINAPI SHCLSIDFromStringAW (LPVOID clsid, CLSID *id)
{
289
	if (SHELL_OsIsUnicode())
290 291 292
	  return SHCLSIDFromStringW (clsid, id);
	return SHCLSIDFromStringA (clsid, id);
}
293

294 295
/*************************************************************************
 *			 SHGetMalloc			[SHELL32.@]
Jon Griffiths's avatar
Jon Griffiths committed
296
 *
297 298 299
 * Equivalent to CoGetMalloc(MEMCTX_TASK, ...). Under Windows 9x this function
 * could use the shell32 built-in "mini-COM" without the need to load ole32.dll -
 * see SHLoadOLE for details. 
Jon Griffiths's avatar
Jon Griffiths committed
300 301 302 303 304 305 306
 *
 * PARAMS
 *  lpmal [O] Destination for IMalloc interface.
 *
 * RETURNS
 *  Success: S_OK. lpmal contains the shells IMalloc interface.
 *  Failure. An HRESULT error code.
307
 *
308 309
 * SEE ALSO
 *  CoGetMalloc, SHLoadOLE
310
 */
311
HRESULT WINAPI SHGetMalloc(LPMALLOC *lpmal)
312 313
{
	TRACE("(%p)\n", lpmal);
314
	return CoGetMalloc(MEMCTX_TASK, lpmal);
315
}
316

317 318 319
/*************************************************************************
 * SHAlloc					[SHELL32.196]
 *
320 321 322 323
 * Equivalent to CoTaskMemAlloc. Under Windows 9x this function could use
 * the shell32 built-in "mini-COM" without the need to load ole32.dll -
 * see SHLoadOLE for details. 
 *
324 325
 * NOTES
 *     exported by ordinal
326 327 328
 *
 * SEE ALSO
 *     CoTaskMemAlloc, SHLoadOLE
329 330 331
 */
LPVOID WINAPI SHAlloc(DWORD len)
{
332
	LPVOID ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
333

334
	ret = CoTaskMemAlloc(len);
335
	TRACE("%u bytes at %p\n",len, ret);
336
	return ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
337
}
338 339 340 341

/*************************************************************************
 * SHFree					[SHELL32.195]
 *
342 343 344 345
 * Equivalent to CoTaskMemFree. Under Windows 9x this function could use
 * the shell32 built-in "mini-COM" without the need to load ole32.dll -
 * see SHLoadOLE for details. 
 *
346 347
 * NOTES
 *     exported by ordinal
348 349 350
 *
 * SEE ALSO
 *     CoTaskMemFree, SHLoadOLE
Alexandre Julliard's avatar
Alexandre Julliard committed
351
 */
352
void WINAPI SHFree(LPVOID pv)
353
{
354
	TRACE("%p\n",pv);
355
	CoTaskMemFree(pv);
Alexandre Julliard's avatar
Alexandre Julliard committed
356
}
357

358 359 360
/*************************************************************************
 * SHGetDesktopFolder			[SHELL32.@]
 */
361
HRESULT WINAPI SHGetDesktopFolder(IShellFolder **psf)
362
{
363
	HRESULT	hres = S_OK;
364
	TRACE("\n");
365 366 367 368 369 370 371 372

	if(!psf) return E_INVALIDARG;
	*psf = NULL;
	hres = ISF_Desktop_Constructor(NULL, &IID_IShellFolder,(LPVOID*)psf);

	TRACE("-- %p->(%p)\n",psf, *psf);
	return hres;
}
373 374 375 376 377 378
/**************************************************************************
 * Default ClassFactory Implementation
 *
 * SHCreateDefClassObject
 *
 * NOTES
379 380 381
 *  Helper function for dlls without their own classfactory.
 *  A generic classfactory is returned.
 *  When the CreateInstance of the cf is called the callback is executed.
382 383 384 385
 */

typedef struct
{
386
    const IClassFactoryVtbl    *lpVtbl;
Mike McCormack's avatar
Mike McCormack committed
387
    LONG                        ref;
388 389 390
    CLSID			*rclsid;
    LPFNCREATEINSTANCE		lpfnCI;
    const IID *			riidInst;
Mike McCormack's avatar
Mike McCormack committed
391
    LONG *			pcRefDll; /* pointer to refcounter in external dll (ugrrr...) */
392 393
} IDefClFImpl;

394
static const IClassFactoryVtbl dclfvt;
395 396 397 398 399

/**************************************************************************
 *  IDefClF_fnConstructor
 */

400
static IClassFactory * IDefClF_fnConstructor(LPFNCREATEINSTANCE lpfnCI, PLONG pcRefDll, REFIID riidInst)
401 402 403
{
	IDefClFImpl* lpclf;

404
	lpclf = HeapAlloc(GetProcessHeap(),0,sizeof(IDefClFImpl));
405
	lpclf->ref = 1;
406
	lpclf->lpVtbl = &dclfvt;
407 408 409
	lpclf->lpfnCI = lpfnCI;
	lpclf->pcRefDll = pcRefDll;

410
	if (pcRefDll) InterlockedIncrement(pcRefDll);
411 412
	lpclf->riidInst = riidInst;

413
	TRACE("(%p)%s\n",lpclf, shdebugstr_guid(riidInst));
414 415 416 417 418 419 420 421
	return (LPCLASSFACTORY)lpclf;
}
/**************************************************************************
 *  IDefClF_fnQueryInterface
 */
static HRESULT WINAPI IDefClF_fnQueryInterface(
  LPCLASSFACTORY iface, REFIID riid, LPVOID *ppvObj)
{
422
	IDefClFImpl *This = (IDefClFImpl *)iface;
423

424
	TRACE("(%p)->(%s)\n",This,shdebugstr_guid(riid));
425 426 427

	*ppvObj = NULL;

428 429 430
	if(IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IClassFactory)) {
	  *ppvObj = This;
	  InterlockedIncrement(&This->ref);
431 432
	  return S_OK;
	}
433 434

	TRACE("-- E_NOINTERFACE\n");
435
	return E_NOINTERFACE;
436
}
437 438 439 440 441
/******************************************************************************
 * IDefClF_fnAddRef
 */
static ULONG WINAPI IDefClF_fnAddRef(LPCLASSFACTORY iface)
{
442
	IDefClFImpl *This = (IDefClFImpl *)iface;
443
	ULONG refCount = InterlockedIncrement(&This->ref);
444

445
	TRACE("(%p)->(count=%u)\n", This, refCount - 1);
446 447

	return refCount;
448 449 450 451 452 453
}
/******************************************************************************
 * IDefClF_fnRelease
 */
static ULONG WINAPI IDefClF_fnRelease(LPCLASSFACTORY iface)
{
454
	IDefClFImpl *This = (IDefClFImpl *)iface;
455 456
	ULONG refCount = InterlockedDecrement(&This->ref);
	
457
	TRACE("(%p)->(count=%u)\n", This, refCount + 1);
458

459
	if (!refCount)
460
	{
461
	  if (This->pcRefDll) InterlockedDecrement(This->pcRefDll);
462 463 464 465 466

	  TRACE("-- destroying IClassFactory(%p)\n",This);
	  HeapFree(GetProcessHeap(),0,This);
	  return 0;
	}
467
	return refCount;
468 469 470 471 472 473 474
}
/******************************************************************************
 * IDefClF_fnCreateInstance
 */
static HRESULT WINAPI IDefClF_fnCreateInstance(
  LPCLASSFACTORY iface, LPUNKNOWN pUnkOuter, REFIID riid, LPVOID *ppvObject)
{
475
	IDefClFImpl *This = (IDefClFImpl *)iface;
476

477
	TRACE("%p->(%p,%s,%p)\n",This,pUnkOuter,shdebugstr_guid(riid),ppvObject);
478 479

	*ppvObject = NULL;
480

481 482 483 484 485 486 487
	if ( This->riidInst==NULL ||
	     IsEqualCLSID(riid, This->riidInst) ||
	     IsEqualCLSID(riid, &IID_IUnknown) )
	{
	  return This->lpfnCI(pUnkOuter, riid, ppvObject);
	}

488
	ERR("unknown IID requested %s\n",shdebugstr_guid(riid));
489 490 491 492 493 494 495
	return E_NOINTERFACE;
}
/******************************************************************************
 * IDefClF_fnLockServer
 */
static HRESULT WINAPI IDefClF_fnLockServer(LPCLASSFACTORY iface, BOOL fLock)
{
496
	IDefClFImpl *This = (IDefClFImpl *)iface;
497 498 499 500
	TRACE("%p->(0x%x), not implemented\n",This, fLock);
	return E_NOTIMPL;
}

501
static const IClassFactoryVtbl dclfvt =
502 503 504 505 506 507 508 509 510 511 512 513
{
    IDefClF_fnQueryInterface,
    IDefClF_fnAddRef,
  IDefClF_fnRelease,
  IDefClF_fnCreateInstance,
  IDefClF_fnLockServer
};

/******************************************************************************
 * SHCreateDefClassObject			[SHELL32.70]
 */
HRESULT WINAPI SHCreateDefClassObject(
514 515
	REFIID	riid,
	LPVOID*	ppv,
516
	LPFNCREATEINSTANCE lpfnCI,	/* [in] create instance callback entry */
517
	LPDWORD	pcRefDll,		/* [in/out] ref count of the dll */
518
	REFIID	riidInst)		/* [in] optional interface to the instance */
519
{
520 521
	IClassFactory * pcf;

522 523
	TRACE("%s %p %p %p %s\n",
              shdebugstr_guid(riid), ppv, lpfnCI, pcRefDll, shdebugstr_guid(riidInst));
524

525
	if (! IsEqualCLSID(riid, &IID_IClassFactory) ) return E_NOINTERFACE;
526
	if (! (pcf = IDefClF_fnConstructor(lpfnCI, (PLONG)pcRefDll, riidInst))) return E_OUTOFMEMORY;
527 528
	*ppv = pcf;
	return NOERROR;
529 530
}

531
/*************************************************************************
532
 *  DragAcceptFiles		[SHELL32.@]
533 534 535 536
 */
void WINAPI DragAcceptFiles(HWND hWnd, BOOL b)
{
	LONG exstyle;
537

538 539 540 541 542 543 544 545 546 547
	if( !IsWindow(hWnd) ) return;
	exstyle = GetWindowLongA(hWnd,GWL_EXSTYLE);
	if (b)
	  exstyle |= WS_EX_ACCEPTFILES;
	else
	  exstyle &= ~WS_EX_ACCEPTFILES;
	SetWindowLongA(hWnd,GWL_EXSTYLE,exstyle);
}

/*************************************************************************
548
 * DragFinish		[SHELL32.@]
549 550 551 552 553 554 555 556
 */
void WINAPI DragFinish(HDROP h)
{
	TRACE("\n");
	GlobalFree((HGLOBAL)h);
}

/*************************************************************************
557
 * DragQueryPoint		[SHELL32.@]
558 559 560
 */
BOOL WINAPI DragQueryPoint(HDROP hDrop, POINT *p)
{
561
        DROPFILES *lpDropFileStruct;
562 563 564 565
	BOOL bRet;

	TRACE("\n");

566 567 568 569
	lpDropFileStruct = (DROPFILES *) GlobalLock(hDrop);

        *p = lpDropFileStruct->pt;
	bRet = lpDropFileStruct->fNC;
570

571 572 573 574 575
	GlobalUnlock(hDrop);
	return bRet;
}

/*************************************************************************
576
 *  DragQueryFileA		[SHELL32.@]
577
 *  DragQueryFile 		[SHELL32.@]
578 579 580 581 582 583 584 585 586
 */
UINT WINAPI DragQueryFileA(
	HDROP hDrop,
	UINT lFile,
	LPSTR lpszFile,
	UINT lLength)
{
	LPSTR lpDrop;
	UINT i = 0;
587
	DROPFILES *lpDropFileStruct = (DROPFILES *) GlobalLock(hDrop);
588

589
	TRACE("(%p, %x, %p, %u)\n",	hDrop,lFile,lpszFile,lLength);
590

591 592
	if(!lpDropFileStruct) goto end;

593
	lpDrop = (LPSTR) lpDropFileStruct + lpDropFileStruct->pFiles;
594

595
        if(lpDropFileStruct->fWide) {
596 597 598
            LPWSTR lpszFileW = NULL;

            if(lpszFile) {
599
                lpszFileW = HeapAlloc(GetProcessHeap(), 0, lLength*sizeof(WCHAR));
600 601 602 603 604 605 606 607 608 609 610 611 612
                if(lpszFileW == NULL) {
                    goto end;
                }
            }
            i = DragQueryFileW(hDrop, lFile, lpszFileW, lLength);

            if(lpszFileW) {
                WideCharToMultiByte(CP_ACP, 0, lpszFileW, -1, lpszFile, lLength, 0, NULL);
                HeapFree(GetProcessHeap(), 0, lpszFileW);
            }
            goto end;
        }

613 614 615
	while (i++ < lFile)
	{
	  while (*lpDrop++); /* skip filename */
616
	  if (!*lpDrop)
617
	  {
618
	    i = (lFile == 0xFFFFFFFF) ? i : 0;
619 620 621
	    goto end;
	  }
	}
622

623
	i = strlen(lpDrop);
624 625 626 627 628 629 630 631 632 633
	i++;
	if (!lpszFile ) goto end;   /* needed buffer size */
	i = (lLength > i) ? i : lLength;
	lstrcpynA (lpszFile,  lpDrop,  i);
end:
	GlobalUnlock(hDrop);
	return i;
}

/*************************************************************************
634
 *  DragQueryFileW		[SHELL32.@]
635 636 637 638 639 640 641 642 643
 */
UINT WINAPI DragQueryFileW(
	HDROP hDrop,
	UINT lFile,
	LPWSTR lpszwFile,
	UINT lLength)
{
	LPWSTR lpwDrop;
	UINT i = 0;
644
	DROPFILES *lpDropFileStruct = (DROPFILES *) GlobalLock(hDrop);
645

646
	TRACE("(%p, %x, %p, %u)\n", hDrop,lFile,lpszwFile,lLength);
647

648 649
	if(!lpDropFileStruct) goto end;

650
	lpwDrop = (LPWSTR) ((LPSTR)lpDropFileStruct + lpDropFileStruct->pFiles);
651

652 653
        if(lpDropFileStruct->fWide == FALSE) {
            LPSTR lpszFileA = NULL;
654

655
            if(lpszwFile) {
656
                lpszFileA = HeapAlloc(GetProcessHeap(), 0, lLength);
657 658 659 660 661 662 663 664 665 666 667 668 669
                if(lpszFileA == NULL) {
                    goto end;
                }
            }
            i = DragQueryFileA(hDrop, lFile, lpszFileA, lLength);

            if(lpszFileA) {
                MultiByteToWideChar(CP_ACP, 0, lpszFileA, -1, lpszwFile, lLength);
                HeapFree(GetProcessHeap(), 0, lpszFileA);
            }
            goto end;
        }

670 671 672 673
	i = 0;
	while (i++ < lFile)
	{
	  while (*lpwDrop++); /* skip filename */
674
	  if (!*lpwDrop)
675
	  {
676
	    i = (lFile == 0xFFFFFFFF) ? i : 0;
677 678 679
	    goto end;
	  }
	}
680

681
	i = strlenW(lpwDrop);
682 683 684 685 686 687 688 689 690
	i++;
	if ( !lpszwFile) goto end;   /* needed buffer size */

	i = (lLength > i) ? i : lLength;
	lstrcpynW (lpszwFile, lpwDrop, i);
end:
	GlobalUnlock(hDrop);
	return i;
}