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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 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 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
|
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2016 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 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_01.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_exceptions.h"
# include <mscoree.h>
/* Since there is no official public mscorlib.h header file, and since
* generating your own version from the elusive binary .tlb file takes a lot of
* hacking and results in a 3MB header file (!), we opt for this slightly
* voodoo approach. The following is just enough definition to be able to
* reach the _AppDomain::CreateInstance method that we need to use to be able
* to fire up .Net objects. We used to use IDispatch for this, but it would
* not always work.
*
* The following info was obtained using OleView to export the IDL from
* mscorlib.tlb. Note that OleView is unable to generate C headers for this
* particular tlb... hence this mess.
*/
const GUID IID_mscorlib_System_AppDomain = {
0x05F696DC, 0x2B29, 0x3663, {0xAD, 0x8B, 0xC4, 0x38, 0x9C, 0xF2, 0xA7, 0x13 }};
typedef struct _Imscorlib_System_AppDomain IAppDomain;
struct _Imscorlib_System_AppDomainVtbl {
BEGIN_INTERFACE
HRESULT ( STDMETHODCALLTYPE *QueryInterface )(
IAppDomain * This,
/* [in] */ REFIID riid,
/* [iid_is][out] */ void **ppvObject);
ULONG ( STDMETHODCALLTYPE *AddRef )(
IAppDomain * This);
ULONG ( STDMETHODCALLTYPE *Release )(
IAppDomain * This);
/* this is padding to get CreateInstance into the correct position */
#define DUMMY_METHOD(x) HRESULT ( STDMETHODCALLTYPE *dummy_##x )(IAppDomain *This)
DUMMY_METHOD(GetTypeInfoCount);
DUMMY_METHOD(GetTypeInfo);
DUMMY_METHOD(GetIDsOfNames);
DUMMY_METHOD(Invoke);
DUMMY_METHOD(ToString);
DUMMY_METHOD(Equals);
DUMMY_METHOD(GetHashCode);
DUMMY_METHOD(GetType);
DUMMY_METHOD(InitializeLifetimeService);
DUMMY_METHOD(GetLifetimeService);
DUMMY_METHOD(Evidence);
DUMMY_METHOD(add_DomainUnload);
DUMMY_METHOD(remove_DomainUnload);
DUMMY_METHOD(add_AssemblyLoad);
DUMMY_METHOD(remove_AssemblyLoad);
DUMMY_METHOD(add_ProcessExit);
DUMMY_METHOD(remove_ProcessExit);
DUMMY_METHOD(add_TypeResolve);
DUMMY_METHOD(remove_TypeResolve);
DUMMY_METHOD(add_ResourceResolve);
DUMMY_METHOD(remove_ResourceResolve);
DUMMY_METHOD(add_AssemblyResolve);
DUMMY_METHOD(remove_AssemblyResolve);
DUMMY_METHOD(add_UnhandledException);
DUMMY_METHOD(remove_UnhandledException);
DUMMY_METHOD(DefineDynamicAssembly);
DUMMY_METHOD(DefineDynamicAssembly_2);
DUMMY_METHOD(DefineDynamicAssembly_3);
DUMMY_METHOD(DefineDynamicAssembly_4);
DUMMY_METHOD(DefineDynamicAssembly_5);
DUMMY_METHOD(DefineDynamicAssembly_6);
DUMMY_METHOD(DefineDynamicAssembly_7);
DUMMY_METHOD(DefineDynamicAssembly_8);
DUMMY_METHOD(DefineDynamicAssembly_9);
HRESULT ( STDMETHODCALLTYPE *CreateInstance )(IAppDomain * This, BSTR AssemblyName, BSTR typeName, IUnknown **pRetVal);
HRESULT ( STDMETHODCALLTYPE *CreateInstanceFrom )(IAppDomain * This, BSTR AssemblyFile, BSTR typeName, IUnknown **pRetVal);
/* more methods live here */
END_INTERFACE
};
struct _Imscorlib_System_AppDomain {
struct _Imscorlib_System_AppDomainVtbl *lpVtbl;
};
struct dotnet_runtime_stuff {
ICorRuntimeHost *dotnet_host;
IAppDomain *dotnet_domain;
DISPID create_instance;
};
static HRESULT dotnet_init(char **p_where TSRMLS_DC)
{
HRESULT hr;
struct dotnet_runtime_stuff *stuff;
IUnknown *unk = NULL;
char *where = "";
stuff = malloc(sizeof(*stuff));
if (!stuff) {
return S_FALSE;
}
memset(stuff, 0, sizeof(*stuff));
where = "CoCreateInstance";
hr = CoCreateInstance(&CLSID_CorRuntimeHost, NULL, CLSCTX_ALL,
&IID_ICorRuntimeHost, (LPVOID*)&stuff->dotnet_host);
if (FAILED(hr))
goto out;
/* fire up the host and get the domain object */
where = "ICorRuntimeHost_Start\n";
hr = ICorRuntimeHost_Start(stuff->dotnet_host);
if (FAILED(hr))
goto out;
where = "ICorRuntimeHost_GetDefaultDomain";
hr = ICorRuntimeHost_GetDefaultDomain(stuff->dotnet_host, &unk);
if (FAILED(hr))
goto out;
where = "QI: System._AppDomain";
hr = IUnknown_QueryInterface(unk, &IID_mscorlib_System_AppDomain, (LPVOID*)&stuff->dotnet_domain);
if (FAILED(hr))
goto out;
COMG(dotnet_runtime_stuff) = stuff;
out:
if (unk) {
IUnknown_Release(unk);
}
if (COMG(dotnet_runtime_stuff) == NULL) {
/* clean up */
if (stuff->dotnet_domain) {
IUnknown_Release(stuff->dotnet_domain);
}
if (stuff->dotnet_host) {
ICorRuntimeHost_Stop(stuff->dotnet_host);
ICorRuntimeHost_Release(stuff->dotnet_host);
}
free(stuff);
*p_where = where;
return hr;
}
return S_OK;
}
/* {{{ 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;
int assembly_name_len, datatype_name_len;
struct dotnet_runtime_stuff *stuff;
OLECHAR *oleassembly, *oletype;
BSTR oleassembly_sys, oletype_sys;
HRESULT hr;
int ret = FAILURE;
char *where = "";
IUnknown *unk = NULL;
php_com_initialize(TSRMLS_C);
stuff = (struct dotnet_runtime_stuff*)COMG(dotnet_runtime_stuff);
if (stuff == NULL) {
hr = dotnet_init(&where TSRMLS_CC);
if (FAILED(hr)) {
char buf[1024];
char *err = php_win32_error_to_msg(hr);
snprintf(buf, sizeof(buf), "Failed to init .Net runtime [%s] %s", where, err);
if (err)
LocalFree(err);
php_com_throw_exception(hr, buf TSRMLS_CC);
ZVAL_NULL(object);
return;
}
stuff = (struct dotnet_runtime_stuff*)COMG(dotnet_runtime_stuff);
} else if (stuff->dotnet_domain == NULL) {
where = "ICorRuntimeHost_GetDefaultDomain";
hr = ICorRuntimeHost_GetDefaultDomain(stuff->dotnet_host, &unk);
if (FAILED(hr)) {
char buf[1024];
char *err = php_win32_error_to_msg(hr);
snprintf(buf, sizeof(buf), "Failed to re-init .Net domain [%s] %s", where, err);
if (err)
LocalFree(err);
php_com_throw_exception(hr, buf TSRMLS_CC);
ZVAL_NULL(object);
return;
}
where = "QI: System._AppDomain";
hr = IUnknown_QueryInterface(unk, &IID_mscorlib_System_AppDomain, (LPVOID*)&stuff->dotnet_domain);
if (FAILED(hr)) {
char buf[1024];
char *err = php_win32_error_to_msg(hr);
snprintf(buf, sizeof(buf), "Failed to re-init .Net domain [%s] %s", where, err);
if (err)
LocalFree(err);
php_com_throw_exception(hr, buf TSRMLS_CC);
ZVAL_NULL(object);
return;
}
}
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;
}
oletype = php_com_string_to_olestring(datatype_name, datatype_name_len, obj->code_page TSRMLS_CC);
oleassembly = php_com_string_to_olestring(assembly_name, assembly_name_len, obj->code_page TSRMLS_CC);
oletype_sys = SysAllocString(oletype);
oleassembly_sys = SysAllocString(oleassembly);
where = "CreateInstance";
hr = stuff->dotnet_domain->lpVtbl->CreateInstance(stuff->dotnet_domain, oleassembly_sys, oletype_sys, &unk);
efree(oletype);
efree(oleassembly);
SysFreeString(oletype_sys);
SysFreeString(oleassembly_sys);
if (SUCCEEDED(hr)) {
VARIANT unwrapped;
IObjectHandle *handle = NULL;
where = "QI: IObjectHandle";
hr = IUnknown_QueryInterface(unk, &IID_IObjectHandle, &handle);
if (SUCCEEDED(hr)) {
where = "IObjectHandle_Unwrap";
hr = IObjectHandle_Unwrap(handle, &unwrapped);
if (SUCCEEDED(hr)) {
if (V_VT(&unwrapped) == VT_UNKNOWN) {
where = "Unwrapped, QI for IDispatch";
hr = IUnknown_QueryInterface(V_UNKNOWN(&unwrapped), &IID_IDispatch, &V_DISPATCH(&obj->v));
if (SUCCEEDED(hr)) {
V_VT(&obj->v) = VT_DISPATCH;
/* get its type-info */
IDispatch_GetTypeInfo(V_DISPATCH(&obj->v), 0, LANG_NEUTRAL, &obj->typeinfo);
ret = SUCCESS;
}
} else if (V_VT(&unwrapped) == VT_DISPATCH) {
/* 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;
} else {
/* shouldn't happen, but let's be ready for it */
VariantClear(&unwrapped);
hr = E_INVALIDARG;
}
}
IObjectHandle_Release(handle);
}
IUnknown_Release(unk);
}
if (ret == FAILURE) {
char buf[1024];
char *err = php_win32_error_to_msg(hr);
snprintf(buf, sizeof(buf), "Failed to instantiate .Net object [%s] [0x%08x] %s", where, hr, err);
if (err && err[0]) {
LocalFree(err);
}
php_com_throw_exception(hr, buf 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 */
|