1 /*
2 +----------------------------------------------------------------------+
3 | PHP Version 7 |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 1997-2018 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)124 static HRESULT dotnet_init(char **p_where)
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 size_t 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 zend_long cp = GetACP();
200 const struct php_win32_cp *cp_it;
201
202 php_com_initialize();
203 stuff = (struct dotnet_runtime_stuff*)COMG(dotnet_runtime_stuff);
204 if (stuff == NULL) {
205 hr = dotnet_init(&where);
206 if (FAILED(hr)) {
207 char buf[1024];
208 char *err = php_win32_error_to_msg(hr);
209 snprintf(buf, sizeof(buf), "Failed to init .Net runtime [%s] %s", where, err);
210 if (err)
211 LocalFree(err);
212 php_com_throw_exception(hr, buf);
213 return;
214 }
215 stuff = (struct dotnet_runtime_stuff*)COMG(dotnet_runtime_stuff);
216
217 } else if (stuff->dotnet_domain == NULL) {
218 where = "ICorRuntimeHost_GetDefaultDomain";
219 hr = ICorRuntimeHost_GetDefaultDomain(stuff->dotnet_host, &unk);
220 if (FAILED(hr)) {
221 char buf[1024];
222 char *err = php_win32_error_to_msg(hr);
223 snprintf(buf, sizeof(buf), "Failed to re-init .Net domain [%s] %s", where, err);
224 if (err)
225 LocalFree(err);
226 php_com_throw_exception(hr, buf);
227 ZVAL_NULL(object);
228 return;
229 }
230
231 where = "QI: System._AppDomain";
232 hr = IUnknown_QueryInterface(unk, &IID_mscorlib_System_AppDomain, (LPVOID*)&stuff->dotnet_domain);
233 if (FAILED(hr)) {
234 char buf[1024];
235 char *err = php_win32_error_to_msg(hr);
236 snprintf(buf, sizeof(buf), "Failed to re-init .Net domain [%s] %s", where, err);
237 if (err)
238 LocalFree(err);
239 php_com_throw_exception(hr, buf);
240 ZVAL_NULL(object);
241 return;
242 }
243 }
244
245 obj = CDNO_FETCH(object);
246
247 if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "ss|l",
248 &assembly_name, &assembly_name_len,
249 &datatype_name, &datatype_name_len,
250 &cp)) {
251 php_com_throw_exception(E_INVALIDARG, "Could not create .Net object - invalid arguments!");
252 return;
253 }
254
255 cp_it = php_win32_cp_get_by_id((DWORD)cp);
256 if (!cp_it) {
257 php_com_throw_exception(E_INVALIDARG, "Could not create .Net object - invalid codepage!");
258 return;
259 }
260 obj->code_page = (int)cp_it->id;
261
262 oletype = php_com_string_to_olestring(datatype_name, datatype_name_len, obj->code_page);
263 oleassembly = php_com_string_to_olestring(assembly_name, assembly_name_len, obj->code_page);
264 oletype_sys = SysAllocString(oletype);
265 oleassembly_sys = SysAllocString(oleassembly);
266 where = "CreateInstance";
267 hr = stuff->dotnet_domain->lpVtbl->CreateInstance(stuff->dotnet_domain, oleassembly_sys, oletype_sys, &unk);
268 efree(oletype);
269 efree(oleassembly);
270 SysFreeString(oletype_sys);
271 SysFreeString(oleassembly_sys);
272
273 if (SUCCEEDED(hr)) {
274 VARIANT unwrapped;
275 IObjectHandle *handle = NULL;
276
277 where = "QI: IObjectHandle";
278 hr = IUnknown_QueryInterface(unk, &IID_IObjectHandle, &handle);
279
280 if (SUCCEEDED(hr)) {
281 where = "IObjectHandle_Unwrap";
282 hr = IObjectHandle_Unwrap(handle, &unwrapped);
283 if (SUCCEEDED(hr)) {
284
285 if (V_VT(&unwrapped) == VT_UNKNOWN) {
286 where = "Unwrapped, QI for IDispatch";
287 hr = IUnknown_QueryInterface(V_UNKNOWN(&unwrapped), &IID_IDispatch, &V_DISPATCH(&obj->v));
288
289 if (SUCCEEDED(hr)) {
290 V_VT(&obj->v) = VT_DISPATCH;
291
292 /* get its type-info */
293 IDispatch_GetTypeInfo(V_DISPATCH(&obj->v), 0, LANG_NEUTRAL, &obj->typeinfo);
294 ret = SUCCESS;
295 }
296 } else if (V_VT(&unwrapped) == VT_DISPATCH) {
297 /* unwrapped is now the dispatch pointer we want */
298 V_DISPATCH(&obj->v) = V_DISPATCH(&unwrapped);
299 V_VT(&obj->v) = VT_DISPATCH;
300
301 /* get its type-info */
302 IDispatch_GetTypeInfo(V_DISPATCH(&obj->v), 0, LANG_NEUTRAL, &obj->typeinfo);
303
304 ret = SUCCESS;
305 } else {
306 /* shouldn't happen, but let's be ready for it */
307 VariantClear(&unwrapped);
308 hr = E_INVALIDARG;
309 }
310 }
311 IObjectHandle_Release(handle);
312 }
313 IUnknown_Release(unk);
314 }
315
316 if (ret == FAILURE) {
317 char buf[1024];
318 char *err = php_win32_error_to_msg(hr);
319 snprintf(buf, sizeof(buf), "Failed to instantiate .Net object [%s] [0x%08x] %s", where, hr, err);
320 if (err && err[0]) {
321 LocalFree(err);
322 }
323 php_com_throw_exception(hr, buf);
324 return;
325 }
326 }
327 /* }}} */
328
php_com_dotnet_mshutdown(void)329 void php_com_dotnet_mshutdown(void)
330 {
331 struct dotnet_runtime_stuff *stuff = COMG(dotnet_runtime_stuff);
332
333 if (stuff->dotnet_domain) {
334 IDispatch_Release(stuff->dotnet_domain);
335 }
336 if (stuff->dotnet_host) {
337 ICorRuntimeHost_Stop(stuff->dotnet_host);
338 ICorRuntimeHost_Release(stuff->dotnet_host);
339 stuff->dotnet_host = NULL;
340 }
341 free(stuff);
342 COMG(dotnet_runtime_stuff) = NULL;
343 }
344
php_com_dotnet_rshutdown(void)345 void php_com_dotnet_rshutdown(void)
346 {
347 struct dotnet_runtime_stuff *stuff = COMG(dotnet_runtime_stuff);
348
349 if (stuff->dotnet_domain) {
350 IDispatch_Release(stuff->dotnet_domain);
351 stuff->dotnet_domain = NULL;
352 }
353 }
354
355 #endif /* HAVE_MSCOREE_H */
356