shellole.c 22.3 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 19
 * 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
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
Alexandre Julliard's avatar
Alexandre Julliard committed
20 21
 */

22 23
#include "config.h"

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

27
#include "shellapi.h"
28
#include "shlobj.h"
29
#include "shlguid.h"
30 31
#include "winreg.h"
#include "winerror.h"
32

33
#include "undocshell.h"
34
#include "wine/unicode.h"
35 36
#include "shell32_main.h"

37
#include "wine/debug.h"
38 39
#include "shlwapi.h"
#include "winuser.h"
40
#include "debughlp.h"
41
#include "wine/obj_dragdrophelper.h"
42

43
WINE_DEFAULT_DEBUG_CHANNEL(shell);
44

45
DWORD WINAPI SHCLSIDFromStringA (LPCSTR clsid, CLSID *id);
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
extern HRESULT WINAPI IFSFolder_Constructor(IUnknown * pUnkOuter, REFIID riid, LPVOID * ppv);

const WCHAR sShell32[12] = {'S','H','E','L','L','3','2','.','D','L','L','\0'};
const WCHAR sOLE32[10] = {'O','L','E','3','2','.','D','L','L','\0'};

HINSTANCE hShellOle32 = 0;
/**************************************************************************
 * Default ClassFactory types
 */
typedef HRESULT (CALLBACK *LPFNCREATEINSTANCE)(IUnknown* pUnkOuter, REFIID riid, LPVOID* ppvObject);
IClassFactory * IDefClF_fnConstructor(LPFNCREATEINSTANCE lpfnCI, PLONG pcRefDll, REFIID riidInst);

/* this table contains all CLSID's of shell32 objects */
struct {
	REFIID			riid;
	LPFNCREATEINSTANCE	lpfnCI;
62
} InterfaceTable[6] = {
63 64 65 66
	{&CLSID_ShellFSFolder,	&IFSFolder_Constructor},
	{&CLSID_MyComputer,	&ISF_MyComputer_Constructor},
	{&CLSID_ShellDesktop,	&ISF_Desktop_Constructor},
	{&CLSID_ShellLink,	&IShellLink_Constructor},
67
	{&CLSID_DragDropHelper, &IDropTargetHelper_Constructor},
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
	{NULL,NULL}
};

/*************************************************************************
 * __CoCreateInstance [internal]
 *
 * NOTES
 *   wraper for late bound call to OLE32.DLL
 *
 */
HRESULT (WINAPI *pCoCreateInstance)(REFCLSID rclsid, LPUNKNOWN pUnkOuter, DWORD dwClsContext, REFIID iid, LPVOID *ppv) = NULL;

void * __GetExternalFunc(HMODULE * phModule, LPCWSTR szModuleName, LPCSTR szProcName)
{
	if (!*phModule) *phModule = GetModuleHandleW(szModuleName);
	if (!*phModule) *phModule = LoadLibraryW(szModuleName);
	if (*phModule) return GetProcAddress(*phModule, szProcName);
	return NULL;
}

HRESULT  __CoCreateInstance(REFCLSID rclsid, LPUNKNOWN pUnkOuter, DWORD dwClsContext, REFIID iid, LPVOID *ppv)
{
	if(!pCoCreateInstance) pCoCreateInstance = __GetExternalFunc(&hShellOle32, sOLE32, "CoCreateInstance");
	if(!pCoCreateInstance) return E_FAIL;
	return pCoCreateInstance(rclsid, pUnkOuter, dwClsContext, iid, ppv);
}
Alexandre Julliard's avatar
Alexandre Julliard committed
94 95 96

/*************************************************************************
 * SHCoCreateInstance [SHELL32.102]
97
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
98 99 100
 * NOTES
 *     exported by ordinal
 */
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119

/* 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] );

}

120 121
/************************************************************************/

122
LRESULT WINAPI SHCoCreateInstance(
123
	LPCSTR aclsid,
124
	REFCLSID clsid,
125
	LPUNKNOWN pUnkOuter,
126 127 128
	REFIID refiid,
	LPVOID *ppv)
{
Alexandre Julliard's avatar
Alexandre Julliard committed
129
	DWORD	hres;
130 131
	IID	iid;
	CLSID * myclsid = (CLSID*)clsid;
132 133 134 135 136 137 138 139 140 141 142 143
	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;

144 145 146
	if(!ppv) return E_POINTER;
	*ppv=NULL;

147
	/* if the clsid is a string, convert it */
148 149 150 151 152
	if (!clsid)
	{
	  if (!aclsid) return REGDB_E_CLASSNOTREG;
	  SHCLSIDFromStringA(aclsid, &iid);
	  myclsid = &iid;
Alexandre Julliard's avatar
Alexandre Julliard committed
153 154
	}

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

158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
	/* 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 );

	    /* if a special registry key is set we loading a shell extension without help of OLE32 */
	    bLoadWithoutCOM = (ERROR_SUCCESS == SHQueryValueExW(hKey, sLoadWithoutCOM, 0, 0, 0, 0));

	    /* if the com object is inside shell32 omit use of ole32 */
	    bLoadFromShell32 = (0==lstrcmpiW( PathFindFileNameW(sDllPath), sShell32));

	    RegCloseKey (hKey);
	} else {
	    /* since we can't find it in the registry we try internally */
	    bLoadFromShell32 = TRUE;
178
	}
179 180 181 182 183 184

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

	/* now we create a instance */
	if (bLoadFromShell32) {
	    if (! SUCCEEDED(SHELL32_DllGetClassObject(myclsid, &IID_IClassFactory,(LPVOID*)&pcf))) {
185
	        ERR("LoadFromShell failed for CLSID=%s\n", shdebugstr_guid(myclsid));
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
	    }
	} else if (bLoadWithoutCOM) {

	    /* load a external dll without ole32 */
	    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))) {
		    TRACE("GetClassObject failed 0x%08lx\n", hres);
		    goto end;
	    }

	} else {

	    /* load a external dll in the usual way */
	    hres = __CoCreateInstance(myclsid, pUnkOuter, CLSCTX_INPROC_SERVER, refiid, ppv);
	    goto end;
213
	}
214

215 216 217 218 219 220
	/* here we should have a ClassFactory */
	if (!pcf) return E_ACCESSDENIED;

	hres = IClassFactory_CreateInstance(pcf, pUnkOuter, refiid, ppv);
	IClassFactory_Release(pcf);
end:
221 222
	if(hres!=S_OK)
	{
223 224
	  ERR("failed (0x%08lx) to create CLSID:%s IID:%s\n",
              hres, shdebugstr_guid(myclsid), shdebugstr_guid(refiid));
225
	  ERR("class not found in registry\n");
Alexandre Julliard's avatar
Alexandre Julliard committed
226
	}
227

228
	TRACE("-- instance: %p\n",*ppv);
Alexandre Julliard's avatar
Alexandre Julliard committed
229 230 231 232
	return hres;
}

/*************************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
233
 * DllGetClassObject   [SHELL32.128]
Alexandre Julliard's avatar
Alexandre Julliard committed
234
 */
235 236 237 238 239
HRESULT WINAPI SHELL32_DllGetClassObject(REFCLSID rclsid, REFIID iid, LPVOID *ppv)
{
	HRESULT	hres = E_OUTOFMEMORY;
	IClassFactory * pcf = NULL;
	int i;
Alexandre Julliard's avatar
Alexandre Julliard committed
240

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

243
	if (!ppv) return E_INVALIDARG;
Alexandre Julliard's avatar
Alexandre Julliard committed
244
	*ppv = NULL;
245

246 247 248 249
	/* search our internal interface table */
	for(i=0;InterfaceTable[i].riid;i++) {
	    if(IsEqualIID(InterfaceTable[i].riid, rclsid)) {
	        TRACE("index[%u]\n", i);
250
	        pcf = IDefClF_fnConstructor(InterfaceTable[i].lpfnCI, NULL, NULL);
251
	    }
Alexandre Julliard's avatar
Alexandre Julliard committed
252
	}
253 254

        if (!pcf) {
255
	    FIXME("failed for CLSID=%s\n", shdebugstr_guid(rclsid));
256
	    return CLASS_E_CLASSNOTAVAILABLE;
Alexandre Julliard's avatar
Alexandre Julliard committed
257
	}
258 259 260 261

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

262
	TRACE("-- pointer to class factory: %p\n",*ppv);
Alexandre Julliard's avatar
Alexandre Julliard committed
263 264 265
	return hres;
}

266 267 268 269 270 271
/*************************************************************************
 * SHCLSIDFromString				[SHELL32.147]
 *
 * NOTES
 *     exported by ordinal
 */
272
DWORD WINAPI SHCLSIDFromStringA (LPCSTR clsid, CLSID *id)
273
{
274 275 276 277 278
    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 );
279
}
280 281 282
DWORD WINAPI SHCLSIDFromStringW (LPWSTR clsid, CLSID *id)
{
	TRACE("(%p(%s) %p)\n", clsid, debugstr_w(clsid), id);
283
	return CLSIDFromString(clsid, id);
284 285 286
}
DWORD WINAPI SHCLSIDFromStringAW (LPVOID clsid, CLSID *id)
{
287
	if (SHELL_OsIsUnicode())
288 289 290
	  return SHCLSIDFromStringW (clsid, id);
	return SHCLSIDFromStringA (clsid, id);
}
291

Alexandre Julliard's avatar
Alexandre Julliard committed
292
/*************************************************************************
293
 *	Shell Memory Allocator
Alexandre Julliard's avatar
Alexandre Julliard committed
294 295
 */

296 297
/* set the vtable later */
extern ICOM_VTABLE(IMalloc) VT_Shell_IMalloc32;
298

299 300 301 302 303
/* this is the static object instance */
typedef struct {
	ICOM_VFIELD(IMalloc);
	DWORD dummy;
} _ShellMalloc;
304

305
_ShellMalloc Shell_Malloc = { &VT_Shell_IMalloc32,1};
306

307 308
/* this is the global allocator of shell32 */
IMalloc * ShellTaskAllocator = NULL;
309

310 311 312 313 314
/******************************************************************************
 *              IShellMalloc_QueryInterface        [VTABLE]
 */
static HRESULT WINAPI IShellMalloc_fnQueryInterface(LPMALLOC iface, REFIID refiid, LPVOID *obj)
{
315
	TRACE("(%s,%p)\n",shdebugstr_guid(refiid),obj);
316 317 318 319 320
	if (IsEqualIID(refiid, &IID_IUnknown) || IsEqualIID(refiid, &IID_IMalloc)) {
		*obj = (LPMALLOC) &Shell_Malloc;
		return S_OK;
	}
	return E_NOINTERFACE;
321 322
}

323 324 325 326
/******************************************************************************
 *              IShellMalloc_AddRefRelease        [VTABLE]
 */
static ULONG WINAPI IShellMalloc_fnAddRefRelease(LPMALLOC iface)
327
{
328 329
        return 1;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
330

331 332
/******************************************************************************
 *		IShellMalloc_Alloc [VTABLE]
Alexandre Julliard's avatar
Alexandre Julliard committed
333
 */
334
static LPVOID WINAPI IShellMalloc_fnAlloc(LPMALLOC iface, DWORD cb)
335
{
336
        LPVOID addr;
337

338 339 340
	addr = (LPVOID) LocalAlloc(GMEM_ZEROINIT, cb);
        TRACE("(%p,%ld);\n",addr,cb);
        return addr;
Alexandre Julliard's avatar
Alexandre Julliard committed
341
}
342 343 344

/******************************************************************************
 *		IShellMalloc_Realloc [VTABLE]
Alexandre Julliard's avatar
Alexandre Julliard committed
345
 */
346
static LPVOID WINAPI IShellMalloc_fnRealloc(LPMALLOC iface, LPVOID pv, DWORD cb)
347
{
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
        LPVOID addr;

	if (pv) {
		if (cb) {
			addr = (LPVOID) LocalReAlloc((HANDLE) pv, cb, GMEM_ZEROINIT | GMEM_MOVEABLE);
		} else {
			LocalFree((HANDLE) pv);
			addr = NULL;
		}
	} else {
		if (cb) {
			addr = (LPVOID) LocalAlloc(GMEM_ZEROINIT, cb);
		} else {
			addr = NULL;
		}
363
	}
Alexandre Julliard's avatar
Alexandre Julliard committed
364

365 366
        TRACE("(%p->%p,%ld)\n",pv,addr,cb);
        return addr;
367
}
368

Alexandre Julliard's avatar
Alexandre Julliard committed
369
/******************************************************************************
370
 *		IShellMalloc_Free [VTABLE]
Alexandre Julliard's avatar
Alexandre Julliard committed
371
 */
372
static VOID WINAPI IShellMalloc_fnFree(LPMALLOC iface, LPVOID pv)
373
{
374 375
        TRACE("(%p)\n",pv);
	LocalFree((HANDLE) pv);
Alexandre Julliard's avatar
Alexandre Julliard committed
376
}
377

Alexandre Julliard's avatar
Alexandre Julliard committed
378
/******************************************************************************
379
 *		IShellMalloc_GetSize [VTABLE]
Alexandre Julliard's avatar
Alexandre Julliard committed
380
 */
381
static DWORD WINAPI IShellMalloc_fnGetSize(LPMALLOC iface, LPVOID pv)
382
{
383 384 385
        DWORD cb = (DWORD) LocalSize((HANDLE)pv);
        TRACE("(%p,%ld)\n", pv, cb);
	return cb;
Alexandre Julliard's avatar
Alexandre Julliard committed
386
}
387

Alexandre Julliard's avatar
Alexandre Julliard committed
388
/******************************************************************************
389
 *		IShellMalloc_DidAlloc [VTABLE]
Alexandre Julliard's avatar
Alexandre Julliard committed
390
 */
391
static INT WINAPI IShellMalloc_fnDidAlloc(LPMALLOC iface, LPVOID pv)
392
{
393 394 395
        TRACE("(%p)\n",pv);
        return -1;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
396

397 398 399 400 401 402 403
/******************************************************************************
 * 		IShellMalloc_HeapMinimize [VTABLE]
 */
static VOID WINAPI IShellMalloc_fnHeapMinimize(LPMALLOC iface)
{
	TRACE("()\n");
}
Alexandre Julliard's avatar
Alexandre Julliard committed
404

405 406 407 408 409 410 411 412 413 414 415 416 417
static ICOM_VTABLE(IMalloc) VT_Shell_IMalloc32 =
{
	ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
	IShellMalloc_fnQueryInterface,
	IShellMalloc_fnAddRefRelease,
	IShellMalloc_fnAddRefRelease,
	IShellMalloc_fnAlloc,
	IShellMalloc_fnRealloc,
	IShellMalloc_fnFree,
	IShellMalloc_fnGetSize,
	IShellMalloc_fnDidAlloc,
	IShellMalloc_fnHeapMinimize
};
418

419 420 421 422 423 424 425 426 427 428 429 430
/*************************************************************************
 *			 SHGetMalloc			[SHELL32.@]
 * returns the interface to shell malloc.
 *
 * NOTES
 *  uses OLE32.CoGetMalloc if OLE32.DLL is already loaded.
 *  if not it uses a internal implementations as fallback.
 */
DWORD WINAPI SHGetMalloc(LPMALLOC *lpmal)
{
	HRESULT (WINAPI *pCoGetMalloc)(DWORD,LPMALLOC *);
	HMODULE hOle32;
Alexandre Julliard's avatar
Alexandre Julliard committed
431

432
	TRACE("(%p)\n", lpmal);
433

434
	if (!ShellTaskAllocator)
435
	{
436 437 438 439 440 441 442 443 444 445
		hOle32 = GetModuleHandleA("OLE32.DLL");
		if(hOle32) {
			pCoGetMalloc = (void*) GetProcAddress(hOle32, "CoGetMalloc");
			if (pCoGetMalloc) pCoGetMalloc(MEMCTX_TASK, &ShellTaskAllocator);
			TRACE("got ole32 IMalloc\n");
		}
		if(!ShellTaskAllocator) {
			ShellTaskAllocator = (IMalloc* ) &Shell_Malloc;
			TRACE("use fallback allocator\n");
		}
Alexandre Julliard's avatar
Alexandre Julliard committed
446
	}
447 448 449
	*lpmal = ShellTaskAllocator;
	return  S_OK;
}
450

451 452 453 454 455 456 457 458 459 460
/*************************************************************************
 * SHAlloc					[SHELL32.196]
 *
 * NOTES
 *     exported by ordinal
 */
LPVOID WINAPI SHAlloc(DWORD len)
{
	IMalloc * ppv;
	LPBYTE ret;
461

462
	if (!ShellTaskAllocator) SHGetMalloc(&ppv);
Alexandre Julliard's avatar
Alexandre Julliard committed
463

464 465 466
	ret = (LPVOID) IMalloc_Alloc(ShellTaskAllocator, len);
	TRACE("%lu bytes at %p\n",len, ret);
	return (LPVOID)ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
467
}
468 469 470 471 472 473

/*************************************************************************
 * SHFree					[SHELL32.195]
 *
 * NOTES
 *     exported by ordinal
Alexandre Julliard's avatar
Alexandre Julliard committed
474
 */
475
void WINAPI SHFree(LPVOID pv)
476
{
477 478 479 480 481
	IMalloc * ppv;

	TRACE("%p\n",pv);
	if (!ShellTaskAllocator) SHGetMalloc(&ppv);
	IMalloc_Free(ShellTaskAllocator, pv);
Alexandre Julliard's avatar
Alexandre Julliard committed
482
}
483

484 485 486 487
/*************************************************************************
 * SHGetDesktopFolder			[SHELL32.@]
 */
DWORD WINAPI SHGetDesktopFolder(IShellFolder **psf)
488
{
489
	HRESULT	hres = S_OK;
490
	TRACE("\n");
491 492 493 494 495 496 497 498

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

	TRACE("-- %p->(%p)\n",psf, *psf);
	return hres;
}
499 500 501 502 503 504 505 506 507 508 509 510 511
/**************************************************************************
 * Default ClassFactory Implementation
 *
 * SHCreateDefClassObject
 *
 * NOTES
 *  helper function for dll's without a own classfactory
 *  a generic classfactory is returned
 *  when the CreateInstance of the cf is called the callback is executed
 */

typedef struct
{
512
    ICOM_VFIELD(IClassFactory);
513 514 515 516
    DWORD                       ref;
    CLSID			*rclsid;
    LPFNCREATEINSTANCE		lpfnCI;
    const IID *			riidInst;
517
    ULONG *			pcRefDll; /* pointer to refcounter in external dll (ugrrr...) */
518 519 520 521 522 523 524 525
} IDefClFImpl;

static ICOM_VTABLE(IClassFactory) dclfvt;

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

526
IClassFactory * IDefClF_fnConstructor(LPFNCREATEINSTANCE lpfnCI, PLONG pcRefDll, REFIID riidInst)
527 528 529 530 531
{
	IDefClFImpl* lpclf;

	lpclf = (IDefClFImpl*)HeapAlloc(GetProcessHeap(),0,sizeof(IDefClFImpl));
	lpclf->ref = 1;
532
	ICOM_VTBL(lpclf) = &dclfvt;
533 534 535
	lpclf->lpfnCI = lpfnCI;
	lpclf->pcRefDll = pcRefDll;

536
	if (pcRefDll) InterlockedIncrement(pcRefDll);
537 538
	lpclf->riidInst = riidInst;

539
	TRACE("(%p)%s\n",lpclf, shdebugstr_guid(riidInst));
540 541 542 543 544 545 546 547 548
	return (LPCLASSFACTORY)lpclf;
}
/**************************************************************************
 *  IDefClF_fnQueryInterface
 */
static HRESULT WINAPI IDefClF_fnQueryInterface(
  LPCLASSFACTORY iface, REFIID riid, LPVOID *ppvObj)
{
	ICOM_THIS(IDefClFImpl,iface);
549

550
	TRACE("(%p)->(%s)\n",This,shdebugstr_guid(riid));
551 552 553

	*ppvObj = NULL;

554 555 556
	if(IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IClassFactory)) {
	  *ppvObj = This;
	  InterlockedIncrement(&This->ref);
557 558
	  return S_OK;
	}
559 560

	TRACE("-- E_NOINTERFACE\n");
561
	return E_NOINTERFACE;
562
}
563 564 565 566 567 568 569 570
/******************************************************************************
 * IDefClF_fnAddRef
 */
static ULONG WINAPI IDefClF_fnAddRef(LPCLASSFACTORY iface)
{
	ICOM_THIS(IDefClFImpl,iface);
	TRACE("(%p)->(count=%lu)\n",This,This->ref);

571
	return InterlockedIncrement(&This->ref);
572 573 574 575 576 577 578 579 580
}
/******************************************************************************
 * IDefClF_fnRelease
 */
static ULONG WINAPI IDefClF_fnRelease(LPCLASSFACTORY iface)
{
	ICOM_THIS(IDefClFImpl,iface);
	TRACE("(%p)->(count=%lu)\n",This,This->ref);

581 582
	if (!InterlockedDecrement(&This->ref))
	{
583
	  if (This->pcRefDll) InterlockedDecrement(This->pcRefDll);
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598

	  TRACE("-- destroying IClassFactory(%p)\n",This);
	  HeapFree(GetProcessHeap(),0,This);
	  return 0;
	}
	return This->ref;
}
/******************************************************************************
 * IDefClF_fnCreateInstance
 */
static HRESULT WINAPI IDefClF_fnCreateInstance(
  LPCLASSFACTORY iface, LPUNKNOWN pUnkOuter, REFIID riid, LPVOID *ppvObject)
{
	ICOM_THIS(IDefClFImpl,iface);

599
	TRACE("%p->(%p,%s,%p)\n",This,pUnkOuter,shdebugstr_guid(riid),ppvObject);
600 601

	*ppvObject = NULL;
602

603 604 605 606 607 608 609
	if ( This->riidInst==NULL ||
	     IsEqualCLSID(riid, This->riidInst) ||
	     IsEqualCLSID(riid, &IID_IUnknown) )
	{
	  return This->lpfnCI(pUnkOuter, riid, ppvObject);
	}

610
	ERR("unknown IID requested %s\n",shdebugstr_guid(riid));
611 612 613 614 615 616 617 618 619 620 621 622
	return E_NOINTERFACE;
}
/******************************************************************************
 * IDefClF_fnLockServer
 */
static HRESULT WINAPI IDefClF_fnLockServer(LPCLASSFACTORY iface, BOOL fLock)
{
	ICOM_THIS(IDefClFImpl,iface);
	TRACE("%p->(0x%x), not implemented\n",This, fLock);
	return E_NOTIMPL;
}

623
static ICOM_VTABLE(IClassFactory) dclfvt =
624 625 626 627 628 629 630 631 632 633 634 635 636
{
    ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
    IDefClF_fnQueryInterface,
    IDefClF_fnAddRef,
  IDefClF_fnRelease,
  IDefClF_fnCreateInstance,
  IDefClF_fnLockServer
};

/******************************************************************************
 * SHCreateDefClassObject			[SHELL32.70]
 */
HRESULT WINAPI SHCreateDefClassObject(
637 638
	REFIID	riid,
	LPVOID*	ppv,
639
	LPFNCREATEINSTANCE lpfnCI,	/* [in] create instance callback entry */
640
	LPDWORD	pcRefDll,		/* [in/out] ref count of the dll */
641
	REFIID	riidInst)		/* [in] optional interface to the instance */
642
{
643 644
	IClassFactory * pcf;

645 646
	TRACE("%s %p %p %p %s\n",
              shdebugstr_guid(riid), ppv, lpfnCI, pcRefDll, shdebugstr_guid(riidInst));
647

648 649 650 651
	if (! IsEqualCLSID(riid, &IID_IClassFactory) ) return E_NOINTERFACE;
	if (! (pcf = IDefClF_fnConstructor(lpfnCI, pcRefDll, riidInst))) return E_OUTOFMEMORY;
	*ppv = pcf;
	return NOERROR;
652 653
}

654 655 656 657 658 659
/*************************************************************************
 *  DragAcceptFiles		[SHELL32.54]
 */
void WINAPI DragAcceptFiles(HWND hWnd, BOOL b)
{
	LONG exstyle;
660

661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683
	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);
}

/*************************************************************************
 * DragFinish		[SHELL32.80]
 */
void WINAPI DragFinish(HDROP h)
{
	TRACE("\n");
	GlobalFree((HGLOBAL)h);
}

/*************************************************************************
 * DragQueryPoint		[SHELL32.135]
 */
BOOL WINAPI DragQueryPoint(HDROP hDrop, POINT *p)
{
684
        DROPFILES *lpDropFileStruct;
685 686 687 688
	BOOL bRet;

	TRACE("\n");

689 690 691 692
	lpDropFileStruct = (DROPFILES *) GlobalLock(hDrop);

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

694 695 696 697 698
	GlobalUnlock(hDrop);
	return bRet;
}

/*************************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
699 700
 *  DragQueryFile 		[SHELL32.81]
 *  DragQueryFileA		[SHELL32.82]
701 702 703 704 705 706 707 708 709
 */
UINT WINAPI DragQueryFileA(
	HDROP hDrop,
	UINT lFile,
	LPSTR lpszFile,
	UINT lLength)
{
	LPSTR lpDrop;
	UINT i = 0;
710
	DROPFILES *lpDropFileStruct = (DROPFILES *) GlobalLock(hDrop);
711

712
	TRACE("(%08x, %x, %p, %u)\n",	hDrop,lFile,lpszFile,lLength);
713

714 715
	if(!lpDropFileStruct) goto end;

716
	lpDrop = (LPSTR) lpDropFileStruct + lpDropFileStruct->pFiles;
717

718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735
        if(lpDropFileStruct->fWide == TRUE) {
            LPWSTR lpszFileW = NULL;

            if(lpszFile) {
                lpszFileW = (LPWSTR) HeapAlloc(GetProcessHeap(), 0, lLength*sizeof(WCHAR));
                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;
        }

736 737 738
	while (i++ < lFile)
	{
	  while (*lpDrop++); /* skip filename */
739
	  if (!*lpDrop)
740
	  {
741
	    i = (lFile == 0xFFFFFFFF) ? i : 0;
742 743 744
	    goto end;
	  }
	}
745

746
	i = strlen(lpDrop);
747 748 749 750 751 752 753 754 755 756
	i++;
	if (!lpszFile ) goto end;   /* needed buffer size */
	i = (lLength > i) ? i : lLength;
	lstrcpynA (lpszFile,  lpDrop,  i);
end:
	GlobalUnlock(hDrop);
	return i;
}

/*************************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
757
 *  DragQueryFileW		[SHELL32.133]
758 759 760 761 762 763 764 765 766
 */
UINT WINAPI DragQueryFileW(
	HDROP hDrop,
	UINT lFile,
	LPWSTR lpszwFile,
	UINT lLength)
{
	LPWSTR lpwDrop;
	UINT i = 0;
767
	DROPFILES *lpDropFileStruct = (DROPFILES *) GlobalLock(hDrop);
768

769
	TRACE("(%08x, %x, %p, %u)\n", hDrop,lFile,lpszwFile,lLength);
770

771 772
	if(!lpDropFileStruct) goto end;

773
	lpwDrop = (LPWSTR) ((LPSTR)lpDropFileStruct + lpDropFileStruct->pFiles);
774

775 776
        if(lpDropFileStruct->fWide == FALSE) {
            LPSTR lpszFileA = NULL;
777

778 779 780 781 782 783 784 785 786 787 788 789 790 791 792
            if(lpszwFile) {
                lpszFileA = (LPSTR) HeapAlloc(GetProcessHeap(), 0, lLength);
                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;
        }

793 794 795 796
	i = 0;
	while (i++ < lFile)
	{
	  while (*lpwDrop++); /* skip filename */
797
	  if (!*lpwDrop)
798
	  {
799
	    i = (lFile == 0xFFFFFFFF) ? i : 0;
800 801 802
	    goto end;
	  }
	}
803

804
	i = strlenW(lpwDrop);
805 806 807 808 809 810 811 812 813
	i++;
	if ( !lpszwFile) goto end;   /* needed buffer size */

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