This source file includes following definitions.
- dotnet_init
- PHP_FUNCTION
- php_com_dotnet_mshutdown
- php_com_dotnet_rshutdown
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
36
37
38
39
40
41
42
43
44
45
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 REFIID riid,
59 void **ppvObject);
60
61 ULONG ( STDMETHODCALLTYPE *AddRef )(
62 IAppDomain * This);
63
64 ULONG ( STDMETHODCALLTYPE *Release )(
65 IAppDomain * This);
66
67
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
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
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
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
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
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
200 php_com_initialize();
201 stuff = (struct dotnet_runtime_stuff*)COMG(dotnet_runtime_stuff);
202 if (stuff == NULL) {
203 hr = dotnet_init(&where);
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);
211 return;
212 }
213 stuff = (struct dotnet_runtime_stuff*)COMG(dotnet_runtime_stuff);
214
215 } else if (stuff->dotnet_domain == NULL) {
216 where = "ICorRuntimeHost_GetDefaultDomain";
217 hr = ICorRuntimeHost_GetDefaultDomain(stuff->dotnet_host, &unk);
218 if (FAILED(hr)) {
219 char buf[1024];
220 char *err = php_win32_error_to_msg(hr);
221 snprintf(buf, sizeof(buf), "Failed to re-init .Net domain [%s] %s", where, err);
222 if (err)
223 LocalFree(err);
224 php_com_throw_exception(hr, buf);
225 ZVAL_NULL(object);
226 return;
227 }
228
229 where = "QI: System._AppDomain";
230 hr = IUnknown_QueryInterface(unk, &IID_mscorlib_System_AppDomain, (LPVOID*)&stuff->dotnet_domain);
231 if (FAILED(hr)) {
232 char buf[1024];
233 char *err = php_win32_error_to_msg(hr);
234 snprintf(buf, sizeof(buf), "Failed to re-init .Net domain [%s] %s", where, err);
235 if (err)
236 LocalFree(err);
237 php_com_throw_exception(hr, buf);
238 ZVAL_NULL(object);
239 return;
240 }
241 }
242
243 obj = CDNO_FETCH(object);
244
245 if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "ss|l",
246 &assembly_name, &assembly_name_len,
247 &datatype_name, &datatype_name_len,
248 &obj->code_page)) {
249 php_com_throw_exception(E_INVALIDARG, "Could not create .Net object - invalid arguments!");
250 return;
251 }
252
253 oletype = php_com_string_to_olestring(datatype_name, datatype_name_len, obj->code_page);
254 oleassembly = php_com_string_to_olestring(assembly_name, assembly_name_len, obj->code_page);
255 oletype_sys = SysAllocString(oletype);
256 oleassembly_sys = SysAllocString(oleassembly);
257 where = "CreateInstance";
258 hr = stuff->dotnet_domain->lpVtbl->CreateInstance(stuff->dotnet_domain, oleassembly_sys, oletype_sys, &unk);
259 efree(oletype);
260 efree(oleassembly);
261 SysFreeString(oletype_sys);
262 SysFreeString(oleassembly_sys);
263
264 if (SUCCEEDED(hr)) {
265 VARIANT unwrapped;
266 IObjectHandle *handle = NULL;
267
268 where = "QI: IObjectHandle";
269 hr = IUnknown_QueryInterface(unk, &IID_IObjectHandle, &handle);
270
271 if (SUCCEEDED(hr)) {
272 where = "IObjectHandle_Unwrap";
273 hr = IObjectHandle_Unwrap(handle, &unwrapped);
274 if (SUCCEEDED(hr)) {
275
276 if (V_VT(&unwrapped) == VT_UNKNOWN) {
277 where = "Unwrapped, QI for IDispatch";
278 hr = IUnknown_QueryInterface(V_UNKNOWN(&unwrapped), &IID_IDispatch, &V_DISPATCH(&obj->v));
279
280 if (SUCCEEDED(hr)) {
281 V_VT(&obj->v) = VT_DISPATCH;
282
283
284 IDispatch_GetTypeInfo(V_DISPATCH(&obj->v), 0, LANG_NEUTRAL, &obj->typeinfo);
285 ret = SUCCESS;
286 }
287 } else if (V_VT(&unwrapped) == VT_DISPATCH) {
288
289 V_DISPATCH(&obj->v) = V_DISPATCH(&unwrapped);
290 V_VT(&obj->v) = VT_DISPATCH;
291
292
293 IDispatch_GetTypeInfo(V_DISPATCH(&obj->v), 0, LANG_NEUTRAL, &obj->typeinfo);
294
295 ret = SUCCESS;
296 } else {
297
298 VariantClear(&unwrapped);
299 hr = E_INVALIDARG;
300 }
301 }
302 IObjectHandle_Release(handle);
303 }
304 IUnknown_Release(unk);
305 }
306
307 if (ret == FAILURE) {
308 char buf[1024];
309 char *err = php_win32_error_to_msg(hr);
310 snprintf(buf, sizeof(buf), "Failed to instantiate .Net object [%s] [0x%08x] %s", where, hr, err);
311 if (err && err[0]) {
312 LocalFree(err);
313 }
314 php_com_throw_exception(hr, buf);
315 return;
316 }
317 }
318
319
320 void php_com_dotnet_mshutdown(void)
321 {
322 struct dotnet_runtime_stuff *stuff = COMG(dotnet_runtime_stuff);
323
324 if (stuff->dotnet_domain) {
325 IDispatch_Release(stuff->dotnet_domain);
326 }
327 if (stuff->dotnet_host) {
328 ICorRuntimeHost_Stop(stuff->dotnet_host);
329 ICorRuntimeHost_Release(stuff->dotnet_host);
330 stuff->dotnet_host = NULL;
331 }
332 free(stuff);
333 COMG(dotnet_runtime_stuff) = NULL;
334 }
335
336 void php_com_dotnet_rshutdown(void)
337 {
338 struct dotnet_runtime_stuff *stuff = COMG(dotnet_runtime_stuff);
339
340 if (stuff->dotnet_domain) {
341 IDispatch_Release(stuff->dotnet_domain);
342 stuff->dotnet_domain = NULL;
343 }
344 }
345
346 #endif