root/ext/com_dotnet/com_dotnet.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. dotnet_init
  2. PHP_FUNCTION
  3. php_com_dotnet_mshutdown
  4. php_com_dotnet_rshutdown

   1 /*
   2    +----------------------------------------------------------------------+
   3    | PHP Version 7                                                        |
   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 
 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 */
 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                                                 /* get its type-info */
 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                                         /* unwrapped is now the dispatch pointer we want */
 289                                         V_DISPATCH(&obj->v) = V_DISPATCH(&unwrapped);
 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 
 295                                         ret = SUCCESS;
 296                                 } else {
 297                                         /* shouldn't happen, but let's be ready for it */
 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 /* HAVE_MSCOREE_H */

/* [<][>][^][v][top][bottom][index][help] */