com_wrapper.c 16.7 KB
Newer Older
1 2 3 4
/*
   +----------------------------------------------------------------------+
   | PHP Version 5                                                        |
   +----------------------------------------------------------------------+
5
   | Copyright (c) 1997-2012 The PHP Group                                |
6
   +----------------------------------------------------------------------+
7
   | This source file is subject to version 3.01 of the PHP license,      |
8 9
   | that is bundled with this package in the file LICENSE, and is        |
   | available through the world-wide-web at the following url:           |
10
   | http://www.php.net/license/3_01.txt                                  |
11 12 13 14 15 16 17 18
   | If you did not receive a copy of the PHP license and are unable to   |
   | obtain it through the world-wide-web, please send a note to          |
   | license@php.net so we can mail you a copy immediately.               |
   +----------------------------------------------------------------------+
   | Author: Wez Furlong  <wez@thebrainroom.com>                          |
   +----------------------------------------------------------------------+
 */

19
/* $Id$ */
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

/* This module exports a PHP object as a COM object by wrapping it
 * using IDispatchEx */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_com_dotnet.h"
#include "php_com_dotnet_internal.h"

typedef struct {
	/* This first part MUST match the declaration
	 * of interface IDispatchEx */
	CONST_VTBL struct IDispatchExVtbl *lpVtbl;

	/* now the PHP stuff */
	
41
	DWORD engine_thread; /* for sanity checking */
42 43 44 45 46 47 48 49 50 51 52 53 54
	zval *object;			/* the object exported */
	LONG refcount;			/* COM reference count */

	HashTable *dispid_to_name;	/* keep track of dispid -> name mappings */
	HashTable *name_to_dispid;	/* keep track of name -> dispid mappings */

	GUID sinkid;	/* iid that we "implement" for event sinking */
	
	int id;
} php_dispatchex;

static int le_dispatch;

55
static void disp_destructor(php_dispatchex *disp TSRMLS_DC);
56 57 58 59

static void dispatch_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC)
{
	php_dispatchex *disp = (php_dispatchex *)rsrc->ptr;
60
	disp_destructor(disp TSRMLS_CC);
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
}

int php_com_wrapper_minit(INIT_FUNC_ARGS)
{
	le_dispatch = zend_register_list_destructors_ex(dispatch_dtor,
		NULL, "com_dotnet_dispatch_wrapper", module_number);
	return le_dispatch;
}


/* {{{ trace */
static inline void trace(char *fmt, ...)
{
	va_list ap;
	char buf[4096];

77
	snprintf(buf, sizeof(buf), "T=%08x ", GetCurrentThreadId());
78 79 80 81 82 83 84 85 86 87 88
	OutputDebugString(buf);
	
	va_start(ap, fmt);
	vsnprintf(buf, sizeof(buf), fmt, ap);

	OutputDebugString(buf);
	
	va_end(ap);
}
/* }}} */

89 90
#define FETCH_DISP(methname)																			\
	php_dispatchex *disp = (php_dispatchex*)This; 														\
91
	TSRMLS_FETCH();																						\
92 93 94 95 96 97 98 99
	if (COMG(rshutdown_started)) {																		\
		trace(" PHP Object:%p (name:unknown) %s\n", disp->object,  methname); 							\
	} else {																							\
		trace(" PHP Object:%p (name:%s) %s\n", disp->object, Z_OBJCE_P(disp->object)->name, methname); 	\
	}																									\
	if (GetCurrentThreadId() != disp->engine_thread) {													\
		return RPC_E_WRONG_THREAD;																		\
	}
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 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 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228

static HRESULT STDMETHODCALLTYPE disp_queryinterface( 
	IDispatchEx *This,
	/* [in] */ REFIID riid,
	/* [iid_is][out] */ void **ppvObject)
{
	FETCH_DISP("QueryInterface");

	if (IsEqualGUID(&IID_IUnknown, riid) ||
			IsEqualGUID(&IID_IDispatch, riid) ||
			IsEqualGUID(&IID_IDispatchEx, riid) ||
			IsEqualGUID(&disp->sinkid, riid)) {
		*ppvObject = This;
		InterlockedIncrement(&disp->refcount);
		return S_OK;
	}

	*ppvObject = NULL;
	return E_NOINTERFACE;
}
        
static ULONG STDMETHODCALLTYPE disp_addref(IDispatchEx *This)
{
	FETCH_DISP("AddRef");

	return InterlockedIncrement(&disp->refcount);
}
        
static ULONG STDMETHODCALLTYPE disp_release(IDispatchEx *This)
{
	ULONG ret;
	FETCH_DISP("Release");

	ret = InterlockedDecrement(&disp->refcount);
	trace("-- refcount now %d\n", ret);
	if (ret == 0) {
		/* destroy it */
		if (disp->id)
			zend_list_delete(disp->id);
	}
	return ret;
}

static HRESULT STDMETHODCALLTYPE disp_gettypeinfocount( 
	IDispatchEx *This,
	/* [out] */ UINT *pctinfo)
{
	FETCH_DISP("GetTypeInfoCount");

	*pctinfo = 0;
	return S_OK;
}
        
static HRESULT STDMETHODCALLTYPE disp_gettypeinfo( 
	IDispatchEx *This,
	/* [in] */ UINT iTInfo,
	/* [in] */ LCID lcid,
	/* [out] */ ITypeInfo **ppTInfo)
{
	FETCH_DISP("GetTypeInfo");
	
	*ppTInfo = NULL;
	return DISP_E_BADINDEX;
}

static HRESULT STDMETHODCALLTYPE disp_getidsofnames( 
	IDispatchEx *This,
	/* [in] */ REFIID riid,
	/* [size_is][in] */ LPOLESTR *rgszNames,
	/* [in] */ UINT cNames,
	/* [in] */ LCID lcid,
	/* [size_is][out] */ DISPID *rgDispId)
{
	UINT i;
	HRESULT ret = S_OK;
	FETCH_DISP("GetIDsOfNames");

	for (i = 0; i < cNames; i++) {
		char *name;
		unsigned int namelen;
		zval **tmp;
		
		name = php_com_olestring_to_string(rgszNames[i], &namelen, COMG(code_page) TSRMLS_CC);
		
		/* Lookup the name in the hash */
		if (zend_hash_find(disp->name_to_dispid, name, namelen+1, (void**)&tmp) == FAILURE) {
			ret = DISP_E_UNKNOWNNAME;
			rgDispId[i] = 0;
		} else {
			rgDispId[i] = Z_LVAL_PP(tmp);
		}

		efree(name);

	}
	
	return ret;
}

static HRESULT STDMETHODCALLTYPE disp_invoke( 
	IDispatchEx *This,
	/* [in] */ DISPID dispIdMember,
	/* [in] */ REFIID riid,
	/* [in] */ LCID lcid,
	/* [in] */ WORD wFlags,
	/* [out][in] */ DISPPARAMS *pDispParams,
	/* [out] */ VARIANT *pVarResult,
	/* [out] */ EXCEPINFO *pExcepInfo,
	/* [out] */ UINT *puArgErr)
{
	return This->lpVtbl->InvokeEx(This, dispIdMember,
			lcid, wFlags, pDispParams,
			pVarResult, pExcepInfo, NULL);
}

static HRESULT STDMETHODCALLTYPE disp_getdispid( 
	IDispatchEx *This,
	/* [in] */ BSTR bstrName,
	/* [in] */ DWORD grfdex,
	/* [out] */ DISPID *pid)
{
	HRESULT ret = DISP_E_UNKNOWNNAME;
	char *name;
	unsigned int namelen;
	zval **tmp;
	FETCH_DISP("GetDispID");

	name = php_com_olestring_to_string(bstrName, &namelen, COMG(code_page) TSRMLS_CC);

229 230
	trace("Looking for %s, namelen=%d in %p\n", name, namelen, disp->name_to_dispid);
	
231 232
	/* Lookup the name in the hash */
	if (zend_hash_find(disp->name_to_dispid, name, namelen+1, (void**)&tmp) == SUCCESS) {
233
		trace("found it\n");
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
		*pid = Z_LVAL_PP(tmp);
		ret = S_OK;
	}

	efree(name);
	
	return ret;
}

static HRESULT STDMETHODCALLTYPE disp_invokeex( 
	IDispatchEx *This,
	/* [in] */ DISPID id,
	/* [in] */ LCID lcid,
	/* [in] */ WORD wFlags,
	/* [in] */ DISPPARAMS *pdp,
	/* [out] */ VARIANT *pvarRes,
	/* [out] */ EXCEPINFO *pei,
	/* [unique][in] */ IServiceProvider *pspCaller)
{
	zval **name;
	UINT i;
	zval *retval = NULL;
	zval ***params = NULL;
	HRESULT ret = DISP_E_MEMBERNOTFOUND;
	FETCH_DISP("InvokeEx");

	if (SUCCESS == zend_hash_index_find(disp->dispid_to_name, id, (void**)&name)) {
		/* TODO: add support for overloaded objects */

263
		trace("-- Invoke: %d %20s [%d] flags=%08x args=%d\n", id, Z_STRVAL_PP(name), Z_STRLEN_PP(name), wFlags, pdp->cArgs);
264 265 266
		
		/* convert args into zvals.
		 * Args are in reverse order */
267 268 269 270 271 272 273
		if (pdp->cArgs) {
			params = (zval ***)safe_emalloc(sizeof(zval **), pdp->cArgs, 0);
			for (i = 0; i < pdp->cArgs; i++) {
				VARIANT *arg;
				zval *zarg;

				arg = &pdp->rgvarg[ pdp->cArgs - 1 - i];
274

275
				trace("alloc zval for arg %d VT=%08x\n", i, V_VT(arg));
276

277 278 279 280 281
				ALLOC_INIT_ZVAL(zarg);
				php_com_wrap_variant(zarg, arg, COMG(code_page) TSRMLS_CC);
				params[i] = (zval**)emalloc(sizeof(zval**));
				*params[i] = zarg;
			}
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
		}

		trace("arguments processed, prepare to do some work\n");	
	
		/* TODO: if PHP raises an exception here, we should catch it
		 * and expose it as a COM exception */
		
		if (wFlags & DISPATCH_PROPERTYGET) {
			retval = zend_read_property(Z_OBJCE_P(disp->object), disp->object, Z_STRVAL_PP(name), Z_STRLEN_PP(name)+1, 1 TSRMLS_CC);
		} else if (wFlags & DISPATCH_PROPERTYPUT) {
			zend_update_property(Z_OBJCE_P(disp->object), disp->object, Z_STRVAL_PP(name), Z_STRLEN_PP(name)+1, *params[0] TSRMLS_CC);
		} else if (wFlags & DISPATCH_METHOD) {
			zend_try {
				if (SUCCESS == call_user_function_ex(EG(function_table), &disp->object, *name,
							&retval, pdp->cArgs, params, 1, NULL TSRMLS_CC)) {
					ret = S_OK;
298
					trace("function called ok\n");
299 300 301 302 303 304 305 306 307 308 309

					/* Copy any modified values to callers copy of variant*/
					for (i = 0; i < pdp->cArgs; i++) {
						php_com_dotnet_object *obj = CDNO_FETCH(*params[i]);
						VARIANT *srcvar = &obj->v;
						VARIANT *dstvar = &pdp->rgvarg[ pdp->cArgs - 1 - i];
						if ((V_VT(dstvar) & VT_BYREF) && obj->modified ) {
							trace("percolate modified value for arg %d VT=%08x\n", i, V_VT(dstvar));
							php_com_copy_variant(dstvar, srcvar TSRMLS_CC);   
						}
					}
310
				} else {
311
					trace("failed to call func\n");
312 313 314
					ret = DISP_E_EXCEPTION;
				}
			} zend_catch {
315
				trace("something blew up\n");
316 317 318 319 320 321 322
				ret = DISP_E_EXCEPTION;
			} zend_end_try();
		} else {
			trace("Don't know how to handle this invocation %08x\n", wFlags);
		}
	
		/* release arguments */
323 324 325 326 327 328 329
		if (params) {
			for (i = 0; i < pdp->cArgs; i++) {
				zval_ptr_dtor(params[i]);
				efree(params[i]);
			}
			efree(params);
		}
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
		
		/* return value */
		if (retval) {
			if (pvarRes) {
				VariantInit(pvarRes);
				php_com_variant_from_zval(pvarRes, retval, COMG(code_page) TSRMLS_CC);
			}
			zval_ptr_dtor(&retval);
		} else if (pvarRes) {
			VariantInit(pvarRes);
		}
		
	} else {
		trace("InvokeEx: I don't support DISPID=%d\n", id);
	}

	return ret;
}

static HRESULT STDMETHODCALLTYPE disp_deletememberbyname( 
	IDispatchEx *This,
	/* [in] */ BSTR bstrName,
	/* [in] */ DWORD grfdex)
{
	FETCH_DISP("DeleteMemberByName");

	/* TODO: unset */

	return S_FALSE;
}

static HRESULT STDMETHODCALLTYPE disp_deletememberbydispid( 
	IDispatchEx *This,
	/* [in] */ DISPID id)
{
	FETCH_DISP("DeleteMemberByDispID");
	
	/* TODO: unset */
	
	return S_FALSE;
}

static HRESULT STDMETHODCALLTYPE disp_getmemberproperties( 
	IDispatchEx *This,
	/* [in] */ DISPID id,
	/* [in] */ DWORD grfdexFetch,
	/* [out] */ DWORD *pgrfdex)
{
	FETCH_DISP("GetMemberProperties");

	return DISP_E_UNKNOWNNAME;
}

static HRESULT STDMETHODCALLTYPE disp_getmembername( 
	IDispatchEx *This,
	/* [in] */ DISPID id,
	/* [out] */ BSTR *pbstrName)
{
	zval *name;
	FETCH_DISP("GetMemberName");

	if (SUCCESS == zend_hash_index_find(disp->dispid_to_name, id, (void**)&name)) {
		OLECHAR *olestr = php_com_string_to_olestring(Z_STRVAL_P(name), Z_STRLEN_P(name), COMG(code_page) TSRMLS_CC);
		*pbstrName = SysAllocString(olestr);
		efree(olestr);
		return S_OK;
	} else {
		return DISP_E_UNKNOWNNAME;
	}
}

static HRESULT STDMETHODCALLTYPE disp_getnextdispid( 
	IDispatchEx *This,
	/* [in] */ DWORD grfdex,
	/* [in] */ DISPID id,
	/* [out] */ DISPID *pid)
{
	ulong next = id+1;
	FETCH_DISP("GetNextDispID");

	while(!zend_hash_index_exists(disp->dispid_to_name, next))
		next++;

	if (zend_hash_index_exists(disp->dispid_to_name, next)) {
		*pid = next;
		return S_OK;
	}
	return S_FALSE;
}

static HRESULT STDMETHODCALLTYPE disp_getnamespaceparent( 
	IDispatchEx *This,
	/* [out] */ IUnknown **ppunk)
{
	FETCH_DISP("GetNameSpaceParent");

	*ppunk = NULL;
	return E_NOTIMPL;
}
        
static struct IDispatchExVtbl php_dispatch_vtbl = {
	disp_queryinterface,
	disp_addref,
	disp_release,
	disp_gettypeinfocount,
	disp_gettypeinfo,
	disp_getidsofnames,
	disp_invoke,
	disp_getdispid,
	disp_invokeex,
	disp_deletememberbyname,
	disp_deletememberbydispid,
	disp_getmemberproperties,
	disp_getmembername,
	disp_getnextdispid,
	disp_getnamespaceparent
};


/* enumerate functions and properties of the object and assign
 * dispatch ids */
static void generate_dispids(php_dispatchex *disp TSRMLS_DC)
{
	HashPosition pos;
	char *name = NULL;
	zval *tmp;
	int namelen;
	int keytype;
	ulong pid;

	if (disp->dispid_to_name == NULL) {
		ALLOC_HASHTABLE(disp->dispid_to_name);
		ALLOC_HASHTABLE(disp->name_to_dispid);
		zend_hash_init(disp->name_to_dispid, 0, NULL, ZVAL_PTR_DTOR, 0);
		zend_hash_init(disp->dispid_to_name, 0, NULL, ZVAL_PTR_DTOR, 0);
	}

	/* properties */
	if (Z_OBJPROP_P(disp->object)) {
		zend_hash_internal_pointer_reset_ex(Z_OBJPROP_P(disp->object), &pos);
		while (HASH_KEY_NON_EXISTANT != (keytype =
				zend_hash_get_current_key_ex(Z_OBJPROP_P(disp->object), &name,
			   	&namelen, &pid, 0, &pos))) {
			char namebuf[32];
			if (keytype == HASH_KEY_IS_LONG) {
475
				snprintf(namebuf, sizeof(namebuf), "%d", pid);
476
				name = namebuf;
477
				namelen = strlen(namebuf)+1;
478 479 480 481 482
			}

			zend_hash_move_forward_ex(Z_OBJPROP_P(disp->object), &pos);

			/* Find the existing id */
483
			if (zend_hash_find(disp->name_to_dispid, name, namelen, (void**)&tmp) == SUCCESS)
484 485 486 487
				continue;

			/* add the mappings */
			MAKE_STD_ZVAL(tmp);
488
			ZVAL_STRINGL(tmp, name, namelen-1, 1);
489
			pid = zend_hash_next_free_element(disp->dispid_to_name);
490 491 492 493
			zend_hash_index_update(disp->dispid_to_name, pid, (void*)&tmp, sizeof(zval *), NULL);

			MAKE_STD_ZVAL(tmp);
			ZVAL_LONG(tmp, pid);
494
			zend_hash_update(disp->name_to_dispid, name, namelen, (void*)&tmp, sizeof(zval *), NULL);
495 496 497 498 499 500 501 502 503 504 505 506
		}
	}
	
	/* functions */
	if (Z_OBJCE_P(disp->object)) {
		zend_hash_internal_pointer_reset_ex(&Z_OBJCE_P(disp->object)->function_table, &pos);
		while (HASH_KEY_NON_EXISTANT != (keytype =
				zend_hash_get_current_key_ex(&Z_OBJCE_P(disp->object)->function_table,
			 	&name, &namelen, &pid, 0, &pos))) {

			char namebuf[32];
			if (keytype == HASH_KEY_IS_LONG) {
507
				snprintf(namebuf, sizeof(namebuf), "%d", pid);
508
				name = namebuf;
509
				namelen = strlen(namebuf) + 1;
510 511 512 513 514
			}

			zend_hash_move_forward_ex(Z_OBJPROP_P(disp->object), &pos);

			/* Find the existing id */
515
			if (zend_hash_find(disp->name_to_dispid, name, namelen, (void**)&tmp) == SUCCESS)
516 517 518 519
				continue;

			/* add the mappings */
			MAKE_STD_ZVAL(tmp);
520
			ZVAL_STRINGL(tmp, name, namelen-1, 1);
521
			pid = zend_hash_next_free_element(disp->dispid_to_name);
522 523 524 525
			zend_hash_index_update(disp->dispid_to_name, pid, (void*)&tmp, sizeof(zval *), NULL);

			MAKE_STD_ZVAL(tmp);
			ZVAL_LONG(tmp, pid);
526
			zend_hash_update(disp->name_to_dispid, name, namelen, (void*)&tmp, sizeof(zval *), NULL);
527 528 529 530 531 532 533 534
		}
	}
}

static php_dispatchex *disp_constructor(zval *object TSRMLS_DC)
{
	php_dispatchex *disp = (php_dispatchex*)CoTaskMemAlloc(sizeof(php_dispatchex));

535
	trace("constructing a COM wrapper for PHP object %p (%s)\n", object, Z_OBJCE_P(object)->name);
536 537 538 539 540 541
	
	if (disp == NULL)
		return NULL;

	memset(disp, 0, sizeof(php_dispatchex));

542
	disp->engine_thread = GetCurrentThreadId();
543 544 545 546 547
	disp->lpVtbl = &php_dispatch_vtbl;
	disp->refcount = 1;


	if (object)
548
		Z_ADDREF_P(object);
549 550
	disp->object = object;

551
	disp->id = zend_list_insert(disp, le_dispatch TSRMLS_CC);
552 553 554 555
	
	return disp;
}

556 557
static void disp_destructor(php_dispatchex *disp TSRMLS_DC)
{	
558 559 560 561 562 563 564
	/* Object store not available during request shutdown */
	if (COMG(rshutdown_started)) {
		trace("destroying COM wrapper for PHP object %p (name:unknown)\n", disp->object);
	} else {
		trace("destroying COM wrapper for PHP object %p (name:%s)\n", disp->object, Z_OBJCE_P(disp->object)->name);
	}
	
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
	disp->id = 0;
	
	if (disp->refcount > 0)
		CoDisconnectObject((IUnknown*)disp, 0);

	zend_hash_destroy(disp->dispid_to_name);
	zend_hash_destroy(disp->name_to_dispid);
	FREE_HASHTABLE(disp->dispid_to_name);
	FREE_HASHTABLE(disp->name_to_dispid);
			
	if (disp->object)
		zval_ptr_dtor(&disp->object);

	CoTaskMemFree(disp);
}

581
PHP_COM_DOTNET_API IDispatch *php_com_wrapper_export_as_sink(zval *val, GUID *sinkid,
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
	   HashTable *id_to_name TSRMLS_DC)
{
	php_dispatchex *disp = disp_constructor(val TSRMLS_CC);
	HashPosition pos;
	char *name = NULL;
	zval *tmp, **ntmp;
	int namelen;
	int keytype;
	ulong pid;

	disp->dispid_to_name = id_to_name;

	memcpy(&disp->sinkid, sinkid, sizeof(disp->sinkid));
	
	/* build up the reverse mapping */
	ALLOC_HASHTABLE(disp->name_to_dispid);
	zend_hash_init(disp->name_to_dispid, 0, NULL, ZVAL_PTR_DTOR, 0);
	
	zend_hash_internal_pointer_reset_ex(id_to_name, &pos);
	while (HASH_KEY_NON_EXISTANT != (keytype =
				zend_hash_get_current_key_ex(id_to_name, &name, &namelen, &pid, 0, &pos))) {

		if (keytype == HASH_KEY_IS_LONG) {

			zend_hash_get_current_data_ex(id_to_name, (void**)&ntmp, &pos);
			
			MAKE_STD_ZVAL(tmp);
			ZVAL_LONG(tmp, pid);
			zend_hash_update(disp->name_to_dispid, Z_STRVAL_PP(ntmp),
				Z_STRLEN_PP(ntmp)+1, (void*)&tmp, sizeof(zval *), NULL);
		}

		zend_hash_move_forward_ex(id_to_name, &pos);
	}

	return (IDispatch*)disp;
}

620
PHP_COM_DOTNET_API IDispatch *php_com_wrapper_export(zval *val TSRMLS_DC)
621 622 623
{
	php_dispatchex *disp = NULL;

624
	if (Z_TYPE_P(val) != IS_OBJECT) {
625
		return NULL;
626
	}
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649

	if (php_com_is_valid_object(val TSRMLS_CC)) {
		/* pass back its IDispatch directly */
		php_com_dotnet_object *obj = CDNO_FETCH(val);
		
		if (obj == NULL)
			return NULL;

		if (V_VT(&obj->v) == VT_DISPATCH && V_DISPATCH(&obj->v)) {
			IDispatch_AddRef(V_DISPATCH(&obj->v));
			return V_DISPATCH(&obj->v);
		}
			
		return NULL;
	}

	disp = disp_constructor(val TSRMLS_CC);
	generate_dispids(disp TSRMLS_CC);

	return (IDispatch*)disp;
}