com_dotnet.c 10 KB

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