summaryrefslogtreecommitdiff
path: root/ext/com_dotnet/com_dotnet.c
blob: cbcc7bbbe4748d690011cd6b7a05865d2052bc28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
94
95
96
97
98
99
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
/*
   +----------------------------------------------------------------------+
   | PHP Version 5                                                        |
   +----------------------------------------------------------------------+
   | Copyright (c) 1997-2004 The PHP Group                                |
   +----------------------------------------------------------------------+
   | This source file is subject to version 3.0 of the PHP license,       |
   | that is bundled with this package in the file LICENSE, and is        |
   | available through the world-wide-web at the following url:           |
   | http://www.php.net/license/3_0.txt.                                  |
   | 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>                          |
   +----------------------------------------------------------------------+
 */

/* $Id$ */

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

#include "php.h"

#if HAVE_MSCOREE_H
# include "php_ini.h"
# include "ext/standard/info.h"
# include "php_com_dotnet.h"
# include "php_com_dotnet_internal.h"
# include "Zend/zend_default_classes.h"
# include <mscoree.h>

struct dotnet_runtime_stuff {
	ICorRuntimeHost *dotnet_host;
	IDispatch *dotnet_domain;
	DISPID create_instance;
};

/* Since there is no official public mscorlib.h header file, and since
 * generating your own version from the binary .tlb file results in a 3MB
 * header file (!), we opt for the Dispatch-able approach.  This is slightly
 * slower for creating new objects, but not too bad */
static int dotnet_init(TSRMLS_D)
{
	HRESULT hr;
	struct dotnet_runtime_stuff *stuff;
	IUnknown *unk = NULL;
	OLECHAR *olename;

	stuff = malloc(sizeof(*stuff));
	memset(stuff, 0, sizeof(*stuff));

	if (SUCCEEDED(CoCreateInstance(&CLSID_CorRuntimeHost, NULL, CLSCTX_ALL,
			&IID_ICorRuntimeHost, (LPVOID*)&stuff->dotnet_host))) {

		/* fire up the host and get the domain object */
		if (SUCCEEDED(ICorRuntimeHost_Start(stuff->dotnet_host)) &&
				SUCCEEDED(ICorRuntimeHost_GetDefaultDomain(stuff->dotnet_host, &unk)) &&
				SUCCEEDED(IUnknown_QueryInterface(unk, &IID_IDispatch, (LPVOID*)&stuff->dotnet_domain))) {

			/* locate the create-instance member */
			olename = php_com_string_to_olestring("CreateInstance", sizeof("CreateInstance")-1, CP_ACP TSRMLS_CC);
			hr = IDispatch_GetIDsOfNames(stuff->dotnet_domain, &IID_NULL, &olename, 1, LOCALE_SYSTEM_DEFAULT, &stuff->create_instance);
			efree(olename);

			if (SUCCEEDED(hr)) {
				COMG(dotnet_runtime_stuff) = stuff;
			}
		}

		if (unk) {
			IUnknown_Release(unk);
		}
	}

	if (COMG(dotnet_runtime_stuff) == NULL) {
		/* clean up */
		if (stuff->dotnet_domain) {
			IDispatch_Release(stuff->dotnet_domain);
		}
		if (stuff->dotnet_host) {
			ICorRuntimeHost_Stop(stuff->dotnet_host);
			ICorRuntimeHost_Release(stuff->dotnet_host);
		}
		free(stuff);

		return FAILURE;
	}

	return SUCCESS;
}

/* {{{ com_dotnet_create_instance - ctor for DOTNET class */
PHP_FUNCTION(com_dotnet_create_instance)
{
	zval *object = getThis();
	php_com_dotnet_object *obj;
	char *assembly_name, *datatype_name;
	long assembly_name_len, datatype_name_len;
	struct dotnet_runtime_stuff *stuff;
	IObjectHandle *handle;
	DISPPARAMS params;
	VARIANT vargs[2];
	VARIANT retval;
	HRESULT hr;
	int ret = FAILURE;

	if (COMG(dotnet_runtime_stuff) == NULL) {
		if (FAILURE == dotnet_init(TSRMLS_C)) {
			php_com_throw_exception(E_ERROR, "Failed to initialize .Net runtime" TSRMLS_CC);
			ZVAL_NULL(object);
			return;
		}
	}

	stuff = (struct dotnet_runtime_stuff*)COMG(dotnet_runtime_stuff);

	obj = CDNO_FETCH(object);

	if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|l",
			&assembly_name, &assembly_name_len,
			&datatype_name, &datatype_name_len,
			&obj->code_page)) {
		php_com_throw_exception(E_INVALIDARG, "Could not create .Net object - invalid arguments!" TSRMLS_CC);
		ZVAL_NULL(object);
		return;
	}

	params.cArgs = 2;
	params.cNamedArgs = 0;
	params.rgdispidNamedArgs = NULL;
	params.rgvarg = vargs;

	VariantInit(&vargs[0]);
	VariantInit(&vargs[1]);
	VariantInit(&retval);

	V_VT(&vargs[0]) = VT_BSTR;
	V_BSTR(&vargs[0]) = php_com_string_to_olestring(datatype_name, datatype_name_len, obj->code_page TSRMLS_CC);

	V_VT(&vargs[1]) = VT_BSTR;
	V_BSTR(&vargs[1]) = php_com_string_to_olestring(assembly_name, assembly_name_len, obj->code_page TSRMLS_CC);

	hr = IDispatch_Invoke(stuff->dotnet_domain, stuff->create_instance, &IID_NULL, LOCALE_SYSTEM_DEFAULT,
		DISPATCH_METHOD, &params, &retval, NULL, NULL);

	if (SUCCEEDED(hr)) {
		/* retval should now be an IUnknown/IDispatch representation of an IObjectHandle interface */
		if ((V_VT(&retval) == VT_UNKNOWN || V_VT(&retval) == VT_DISPATCH) &&
				SUCCEEDED(IUnknown_QueryInterface(V_UNKNOWN(&retval), &IID_IObjectHandle, &handle))) {
			VARIANT unwrapped;

			if (SUCCEEDED(IObjectHandle_Unwrap(handle, &unwrapped))) {
				/* unwrapped is now the dispatch pointer we want */
				V_DISPATCH(&obj->v) = V_DISPATCH(&unwrapped);
				V_VT(&obj->v) = VT_DISPATCH;

				/* get its type-info */
				IDispatch_GetTypeInfo(V_DISPATCH(&obj->v), 0, LANG_NEUTRAL, &obj->typeinfo);

				ret = SUCCESS;
			}
			IObjectHandle_Release(handle);
		}
		VariantClear(&retval);
	}

	VariantClear(&vargs[0]);
	VariantClear(&vargs[1]);

	if (ret == FAILURE) {
		php_com_throw_exception(hr, "Failed to instantiate .Net object" TSRMLS_CC);
		ZVAL_NULL(object);
		return;
	}
}
/* }}} */

void php_com_dotnet_mshutdown(TSRMLS_D)
{
	struct dotnet_runtime_stuff *stuff = COMG(dotnet_runtime_stuff);
	
	if (stuff->dotnet_domain) {
		IDispatch_Release(stuff->dotnet_domain);
	}
	if (stuff->dotnet_host) {
		ICorRuntimeHost_Stop(stuff->dotnet_host);
		ICorRuntimeHost_Release(stuff->dotnet_host);
		stuff->dotnet_host = NULL;
	}
	free(stuff);
	COMG(dotnet_runtime_stuff) = NULL;
}

void php_com_dotnet_rshutdown(TSRMLS_D)
{
	struct dotnet_runtime_stuff *stuff = COMG(dotnet_runtime_stuff);
	
	if (stuff->dotnet_domain) {
		IDispatch_Release(stuff->dotnet_domain);
		stuff->dotnet_domain = NULL;
	}
}

#endif /* HAVE_MSCOREE_H */