1 /*
2 +----------------------------------------------------------------------+
3 | PHP Version 5 |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 1997-2016 The PHP Group |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
15 | Author: Wez Furlong <wez@thebrainroom.com> |
16 +----------------------------------------------------------------------+
17 */
18
19 /* $Id$ */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include "php.h"
26
27 #if HAVE_MSCOREE_H
28 # include "php_ini.h"
29 # include "ext/standard/info.h"
30 # include "php_com_dotnet.h"
31 # include "php_com_dotnet_internal.h"
32 # include "Zend/zend_exceptions.h"
33 # include <mscoree.h>
34
35 /* Since there is no official public mscorlib.h header file, and since
36 * generating your own version from the elusive binary .tlb file takes a lot of
37 * hacking and results in a 3MB header file (!), we opt for this slightly
38 * voodoo approach. The following is just enough definition to be able to
39 * reach the _AppDomain::CreateInstance method that we need to use to be able
40 * to fire up .Net objects. We used to use IDispatch for this, but it would
41 * not always work.
42 *
43 * The following info was obtained using OleView to export the IDL from
44 * mscorlib.tlb. Note that OleView is unable to generate C headers for this
45 * particular tlb... hence this mess.
46 */
47
48 const GUID IID_mscorlib_System_AppDomain = {
49 0x05F696DC, 0x2B29, 0x3663, {0xAD, 0x8B, 0xC4, 0x38, 0x9C, 0xF2, 0xA7, 0x13 }};
50
51 typedef struct _Imscorlib_System_AppDomain IAppDomain;
52
53 struct _Imscorlib_System_AppDomainVtbl {
54 BEGIN_INTERFACE
55
56 HRESULT ( STDMETHODCALLTYPE *QueryInterface )(
57 IAppDomain * This,
58 /* [in] */ REFIID riid,
59 /* [iid_is][out] */ void **ppvObject);
60
61 ULONG ( STDMETHODCALLTYPE *AddRef )(
62 IAppDomain * This);
63
64 ULONG ( STDMETHODCALLTYPE *Release )(
65 IAppDomain * This);
66
67 /* this is padding to get CreateInstance into the correct position */
68 #define DUMMY_METHOD(x) HRESULT ( STDMETHODCALLTYPE *dummy_##x )(IAppDomain *This)
69
70 DUMMY_METHOD(GetTypeInfoCount);
71 DUMMY_METHOD(GetTypeInfo);
72 DUMMY_METHOD(GetIDsOfNames);
73 DUMMY_METHOD(Invoke);
74 DUMMY_METHOD(ToString);
75 DUMMY_METHOD(Equals);
76 DUMMY_METHOD(GetHashCode);
77 DUMMY_METHOD(GetType);
78 DUMMY_METHOD(InitializeLifetimeService);
79 DUMMY_METHOD(GetLifetimeService);
80 DUMMY_METHOD(Evidence);
81 DUMMY_METHOD(add_DomainUnload);
82 DUMMY_METHOD(remove_DomainUnload);
83 DUMMY_METHOD(add_AssemblyLoad);
84 DUMMY_METHOD(remove_AssemblyLoad);
85 DUMMY_METHOD(add_ProcessExit);
86 DUMMY_METHOD(remove_ProcessExit);
87 DUMMY_METHOD(add_TypeResolve);
88 DUMMY_METHOD(remove_TypeResolve);
89 DUMMY_METHOD(add_ResourceResolve);
90 DUMMY_METHOD(remove_ResourceResolve);
91 DUMMY_METHOD(add_AssemblyResolve);
92 DUMMY_METHOD(remove_AssemblyResolve);
93 DUMMY_METHOD(add_UnhandledException);
94 DUMMY_METHOD(remove_UnhandledException);
95 DUMMY_METHOD(DefineDynamicAssembly);
96 DUMMY_METHOD(DefineDynamicAssembly_2);
97 DUMMY_METHOD(DefineDynamicAssembly_3);
98 DUMMY_METHOD(DefineDynamicAssembly_4);
99 DUMMY_METHOD(DefineDynamicAssembly_5);
100 DUMMY_METHOD(DefineDynamicAssembly_6);
101 DUMMY_METHOD(DefineDynamicAssembly_7);
102 DUMMY_METHOD(DefineDynamicAssembly_8);
103 DUMMY_METHOD(DefineDynamicAssembly_9);
104
105 HRESULT ( STDMETHODCALLTYPE *CreateInstance )(IAppDomain * This, BSTR AssemblyName, BSTR typeName, IUnknown **pRetVal);
106 HRESULT ( STDMETHODCALLTYPE *CreateInstanceFrom )(IAppDomain * This, BSTR AssemblyFile, BSTR typeName, IUnknown **pRetVal);
107
108 /* more methods live here */
109
110 END_INTERFACE
111 };
112
113 struct _Imscorlib_System_AppDomain {
114 struct _Imscorlib_System_AppDomainVtbl *lpVtbl;
115 };
116
117
118 struct dotnet_runtime_stuff {
119 ICorRuntimeHost *dotnet_host;
120 IAppDomain *dotnet_domain;
121 DISPID create_instance;
122 };
123
dotnet_init(char ** p_where TSRMLS_DC)124 static HRESULT dotnet_init(char **p_where TSRMLS_DC)
125 {
126 HRESULT hr;
127 struct dotnet_runtime_stuff *stuff;
128 IUnknown *unk = NULL;
129 char *where = "";
130
131 stuff = malloc(sizeof(*stuff));
132 if (!stuff) {
133 return S_FALSE;
134 }
135 memset(stuff, 0, sizeof(*stuff));
136
137 where = "CoCreateInstance";
138 hr = CoCreateInstance(&CLSID_CorRuntimeHost, NULL, CLSCTX_ALL,
139 &IID_ICorRuntimeHost, (LPVOID*)&stuff->dotnet_host);
140
141 if (FAILED(hr))
142 goto out;
143
144 /* fire up the host and get the domain object */
145 where = "ICorRuntimeHost_Start\n";
146 hr = ICorRuntimeHost_Start(stuff->dotnet_host);
147 if (FAILED(hr))
148 goto out;
149
150 where = "ICorRuntimeHost_GetDefaultDomain";
151 hr = ICorRuntimeHost_GetDefaultDomain(stuff->dotnet_host, &unk);
152 if (FAILED(hr))
153 goto out;
154
155 where = "QI: System._AppDomain";
156 hr = IUnknown_QueryInterface(unk, &IID_mscorlib_System_AppDomain, (LPVOID*)&stuff->dotnet_domain);
157 if (FAILED(hr))
158 goto out;
159
160 COMG(dotnet_runtime_stuff) = stuff;
161
162 out:
163 if (unk) {
164 IUnknown_Release(unk);
165 }
166 if (COMG(dotnet_runtime_stuff) == NULL) {
167 /* clean up */
168 if (stuff->dotnet_domain) {
169 IUnknown_Release(stuff->dotnet_domain);
170 }
171 if (stuff->dotnet_host) {
172 ICorRuntimeHost_Stop(stuff->dotnet_host);
173 ICorRuntimeHost_Release(stuff->dotnet_host);
174 }
175 free(stuff);
176
177 *p_where = where;
178
179 return hr;
180 }
181
182 return S_OK;
183 }
184
185 /* {{{ com_dotnet_create_instance - ctor for DOTNET class */
PHP_FUNCTION(com_dotnet_create_instance)186 PHP_FUNCTION(com_dotnet_create_instance)
187 {
188 zval *object = getThis();
189 php_com_dotnet_object *obj;
190 char *assembly_name, *datatype_name;
191 int assembly_name_len, datatype_name_len;
192 struct dotnet_runtime_stuff *stuff;
193 OLECHAR *oleassembly, *oletype;
194 BSTR oleassembly_sys, oletype_sys;
195 HRESULT hr;
196 int ret = FAILURE;
197 char *where = "";
198 IUnknown *unk = NULL;
199
200 php_com_initialize(TSRMLS_C);
201 stuff = (struct dotnet_runtime_stuff*)COMG(dotnet_runtime_stuff);
202 if (stuff == NULL) {
203 hr = dotnet_init(&where TSRMLS_CC);
204 if (FAILED(hr)) {
205 char buf[1024];
206 char *err = php_win32_error_to_msg(hr);
207 snprintf(buf, sizeof(buf), "Failed to init .Net runtime [%s] %s", where, err);
208 if (err)
209 LocalFree(err);
210 php_com_throw_exception(hr, buf TSRMLS_CC);
211 ZVAL_NULL(object);
212 return;
213 }
214 stuff = (struct dotnet_runtime_stuff*)COMG(dotnet_runtime_stuff);
215
216 } else if (stuff->dotnet_domain == NULL) {
217 where = "ICorRuntimeHost_GetDefaultDomain";
218 hr = ICorRuntimeHost_GetDefaultDomain(stuff->dotnet_host, &unk);
219 if (FAILED(hr)) {
220 char buf[1024];
221 char *err = php_win32_error_to_msg(hr);
222 snprintf(buf, sizeof(buf), "Failed to re-init .Net domain [%s] %s", where, err);
223 if (err)
224 LocalFree(err);
225 php_com_throw_exception(hr, buf TSRMLS_CC);
226 ZVAL_NULL(object);
227 return;
228 }
229
230 where = "QI: System._AppDomain";
231 hr = IUnknown_QueryInterface(unk, &IID_mscorlib_System_AppDomain, (LPVOID*)&stuff->dotnet_domain);
232 if (FAILED(hr)) {
233 char buf[1024];
234 char *err = php_win32_error_to_msg(hr);
235 snprintf(buf, sizeof(buf), "Failed to re-init .Net domain [%s] %s", where, err);
236 if (err)
237 LocalFree(err);
238 php_com_throw_exception(hr, buf TSRMLS_CC);
239 ZVAL_NULL(object);
240 return;
241 }
242 }
243
244 obj = CDNO_FETCH(object);
245
246 if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|l",
247 &assembly_name, &assembly_name_len,
248 &datatype_name, &datatype_name_len,
249 &obj->code_page)) {
250 php_com_throw_exception(E_INVALIDARG, "Could not create .Net object - invalid arguments!" TSRMLS_CC);
251 ZVAL_NULL(object);
252 return;
253 }
254
255 oletype = php_com_string_to_olestring(datatype_name, datatype_name_len, obj->code_page TSRMLS_CC);
256 oleassembly = php_com_string_to_olestring(assembly_name, assembly_name_len, obj->code_page TSRMLS_CC);
257 oletype_sys = SysAllocString(oletype);
258 oleassembly_sys = SysAllocString(oleassembly);
259 where = "CreateInstance";
260 hr = stuff->dotnet_domain->lpVtbl->CreateInstance(stuff->dotnet_domain, oleassembly_sys, oletype_sys, &unk);
261 efree(oletype);
262 efree(oleassembly);
263 SysFreeString(oletype_sys);
264 SysFreeString(oleassembly_sys);
265
266 if (SUCCEEDED(hr)) {
267 VARIANT unwrapped;
268 IObjectHandle *handle = NULL;
269
270 where = "QI: IObjectHandle";
271 hr = IUnknown_QueryInterface(unk, &IID_IObjectHandle, &handle);
272
273 if (SUCCEEDED(hr)) {
274 where = "IObjectHandle_Unwrap";
275 hr = IObjectHandle_Unwrap(handle, &unwrapped);
276 if (SUCCEEDED(hr)) {
277
278 if (V_VT(&unwrapped) == VT_UNKNOWN) {
279 where = "Unwrapped, QI for IDispatch";
280 hr = IUnknown_QueryInterface(V_UNKNOWN(&unwrapped), &IID_IDispatch, &V_DISPATCH(&obj->v));
281
282 if (SUCCEEDED(hr)) {
283 V_VT(&obj->v) = VT_DISPATCH;
284
285 /* get its type-info */
286 IDispatch_GetTypeInfo(V_DISPATCH(&obj->v), 0, LANG_NEUTRAL, &obj->typeinfo);
287 ret = SUCCESS;
288 }
289 } else if (V_VT(&unwrapped) == VT_DISPATCH) {
290 /* unwrapped is now the dispatch pointer we want */
291 V_DISPATCH(&obj->v) = V_DISPATCH(&unwrapped);
292 V_VT(&obj->v) = VT_DISPATCH;
293
294 /* get its type-info */
295 IDispatch_GetTypeInfo(V_DISPATCH(&obj->v), 0, LANG_NEUTRAL, &obj->typeinfo);
296
297 ret = SUCCESS;
298 } else {
299 /* shouldn't happen, but let's be ready for it */
300 VariantClear(&unwrapped);
301 hr = E_INVALIDARG;
302 }
303 }
304 IObjectHandle_Release(handle);
305 }
306 IUnknown_Release(unk);
307 }
308
309 if (ret == FAILURE) {
310 char buf[1024];
311 char *err = php_win32_error_to_msg(hr);
312 snprintf(buf, sizeof(buf), "Failed to instantiate .Net object [%s] [0x%08x] %s", where, hr, err);
313 if (err && err[0]) {
314 LocalFree(err);
315 }
316 php_com_throw_exception(hr, buf TSRMLS_CC);
317 ZVAL_NULL(object);
318 return;
319 }
320 }
321 /* }}} */
322
php_com_dotnet_mshutdown(TSRMLS_D)323 void php_com_dotnet_mshutdown(TSRMLS_D)
324 {
325 struct dotnet_runtime_stuff *stuff = COMG(dotnet_runtime_stuff);
326
327 if (stuff->dotnet_domain) {
328 IDispatch_Release(stuff->dotnet_domain);
329 }
330 if (stuff->dotnet_host) {
331 ICorRuntimeHost_Stop(stuff->dotnet_host);
332 ICorRuntimeHost_Release(stuff->dotnet_host);
333 stuff->dotnet_host = NULL;
334 }
335 free(stuff);
336 COMG(dotnet_runtime_stuff) = NULL;
337 }
338
php_com_dotnet_rshutdown(TSRMLS_D)339 void php_com_dotnet_rshutdown(TSRMLS_D)
340 {
341 struct dotnet_runtime_stuff *stuff = COMG(dotnet_runtime_stuff);
342
343 if (stuff->dotnet_domain) {
344 IDispatch_Release(stuff->dotnet_domain);
345 stuff->dotnet_domain = NULL;
346 }
347 }
348
349 #endif /* HAVE_MSCOREE_H */
350