com_dotnet.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | https://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Author: Wez Furlong <wez@thebrainroom.com> |
  14. +----------------------------------------------------------------------+
  15. */
  16. #ifdef HAVE_CONFIG_H
  17. #include "config.h"
  18. #endif
  19. #include "php.h"
  20. #if HAVE_MSCOREE_H
  21. # include "php_ini.h"
  22. # include "ext/standard/info.h"
  23. # include "php_com_dotnet.h"
  24. # include "php_com_dotnet_internal.h"
  25. # include "Zend/zend_exceptions.h"
  26. # include <mscoree.h>
  27. /* Since there is no official public mscorlib.h header file, and since
  28. * generating your own version from the elusive binary .tlb file takes a lot of
  29. * hacking and results in a 3MB header file (!), we opt for this slightly
  30. * voodoo approach. The following is just enough definition to be able to
  31. * reach the _AppDomain::CreateInstance method that we need to use to be able
  32. * to fire up .Net objects. We used to use IDispatch for this, but it would
  33. * not always work.
  34. *
  35. * The following info was obtained using OleView to export the IDL from
  36. * mscorlib.tlb. Note that OleView is unable to generate C headers for this
  37. * particular tlb... hence this mess.
  38. */
  39. const GUID IID_mscorlib_System_AppDomain = {
  40. 0x05F696DC, 0x2B29, 0x3663, {0xAD, 0x8B, 0xC4, 0x38, 0x9C, 0xF2, 0xA7, 0x13 }};
  41. typedef struct _Imscorlib_System_AppDomain IAppDomain;
  42. struct _Imscorlib_System_AppDomainVtbl {
  43. BEGIN_INTERFACE
  44. HRESULT ( STDMETHODCALLTYPE *QueryInterface )(
  45. IAppDomain * This,
  46. /* [in] */ REFIID riid,
  47. /* [iid_is][out] */ void **ppvObject);
  48. ULONG ( STDMETHODCALLTYPE *AddRef )(
  49. IAppDomain * This);
  50. ULONG ( STDMETHODCALLTYPE *Release )(
  51. IAppDomain * This);
  52. /* this is padding to get CreateInstance into the correct position */
  53. #define DUMMY_METHOD(x) HRESULT ( STDMETHODCALLTYPE *dummy_##x )(IAppDomain *This)
  54. DUMMY_METHOD(GetTypeInfoCount);
  55. DUMMY_METHOD(GetTypeInfo);
  56. DUMMY_METHOD(GetIDsOfNames);
  57. DUMMY_METHOD(Invoke);
  58. DUMMY_METHOD(ToString);
  59. DUMMY_METHOD(Equals);
  60. DUMMY_METHOD(GetHashCode);
  61. DUMMY_METHOD(GetType);
  62. DUMMY_METHOD(InitializeLifetimeService);
  63. DUMMY_METHOD(GetLifetimeService);
  64. DUMMY_METHOD(Evidence);
  65. DUMMY_METHOD(add_DomainUnload);
  66. DUMMY_METHOD(remove_DomainUnload);
  67. DUMMY_METHOD(add_AssemblyLoad);
  68. DUMMY_METHOD(remove_AssemblyLoad);
  69. DUMMY_METHOD(add_ProcessExit);
  70. DUMMY_METHOD(remove_ProcessExit);
  71. DUMMY_METHOD(add_TypeResolve);
  72. DUMMY_METHOD(remove_TypeResolve);
  73. DUMMY_METHOD(add_ResourceResolve);
  74. DUMMY_METHOD(remove_ResourceResolve);
  75. DUMMY_METHOD(add_AssemblyResolve);
  76. DUMMY_METHOD(remove_AssemblyResolve);
  77. DUMMY_METHOD(add_UnhandledException);
  78. DUMMY_METHOD(remove_UnhandledException);
  79. DUMMY_METHOD(DefineDynamicAssembly);
  80. DUMMY_METHOD(DefineDynamicAssembly_2);
  81. DUMMY_METHOD(DefineDynamicAssembly_3);
  82. DUMMY_METHOD(DefineDynamicAssembly_4);
  83. DUMMY_METHOD(DefineDynamicAssembly_5);
  84. DUMMY_METHOD(DefineDynamicAssembly_6);
  85. DUMMY_METHOD(DefineDynamicAssembly_7);
  86. DUMMY_METHOD(DefineDynamicAssembly_8);
  87. DUMMY_METHOD(DefineDynamicAssembly_9);
  88. HRESULT ( STDMETHODCALLTYPE *CreateInstance )(IAppDomain * This, BSTR AssemblyName, BSTR typeName, IUnknown **pRetVal);
  89. HRESULT ( STDMETHODCALLTYPE *CreateInstanceFrom )(IAppDomain * This, BSTR AssemblyFile, BSTR typeName, IUnknown **pRetVal);
  90. /* more methods live here */
  91. END_INTERFACE
  92. };
  93. struct _Imscorlib_System_AppDomain {
  94. struct _Imscorlib_System_AppDomainVtbl *lpVtbl;
  95. };
  96. struct dotnet_runtime_stuff {
  97. ICorRuntimeHost *dotnet_host;
  98. IAppDomain *dotnet_domain;
  99. DISPID create_instance;
  100. };
  101. /* We link dynamically to mscoree.dll to avoid the hard dependency on .NET
  102. * framework, which is only required if a dotnet instance is to be created.
  103. */
  104. static HRESULT dotnet_bind_runtime(LPVOID FAR *ppv)
  105. {
  106. HRESULT hr;
  107. HMODULE mscoree;
  108. typedef HRESULT (STDAPICALLTYPE *cbtr_t)(LPCWSTR pwszVersion, LPCWSTR pwszBuildFlavor, REFCLSID rclsid, REFIID riid, LPVOID FAR *ppv);
  109. cbtr_t CorBindToRuntime;
  110. OLECHAR *oleversion;
  111. char *version;
  112. mscoree = LoadLibraryA("mscoree.dll");
  113. if (mscoree == NULL) {
  114. return S_FALSE;
  115. }
  116. CorBindToRuntime = (cbtr_t) GetProcAddress(mscoree, "CorBindToRuntime");
  117. if (CorBindToRuntime == NULL) {
  118. FreeLibrary(mscoree);
  119. return S_FALSE;
  120. }
  121. version = INI_STR("com.dotnet_version");
  122. if (version == NULL || *version == '\0') {
  123. oleversion = NULL;
  124. } else {
  125. oleversion = php_com_string_to_olestring(version, strlen(version), COMG(code_page));
  126. }
  127. hr = CorBindToRuntime(oleversion, NULL, &CLSID_CorRuntimeHost, &IID_ICorRuntimeHost, ppv);
  128. efree(oleversion);
  129. FreeLibrary(mscoree);
  130. return hr;
  131. }
  132. static HRESULT dotnet_init(char **p_where)
  133. {
  134. HRESULT hr;
  135. struct dotnet_runtime_stuff *stuff;
  136. IUnknown *unk = NULL;
  137. char *where = "";
  138. stuff = malloc(sizeof(*stuff));
  139. if (!stuff) {
  140. return S_FALSE;
  141. }
  142. memset(stuff, 0, sizeof(*stuff));
  143. where = "dotnet_bind_runtime";
  144. hr = dotnet_bind_runtime((LPVOID*)&stuff->dotnet_host);
  145. if (FAILED(hr))
  146. goto out;
  147. /* fire up the host and get the domain object */
  148. where = "ICorRuntimeHost_Start\n";
  149. hr = ICorRuntimeHost_Start(stuff->dotnet_host);
  150. if (FAILED(hr))
  151. goto out;
  152. where = "ICorRuntimeHost_GetDefaultDomain";
  153. hr = ICorRuntimeHost_GetDefaultDomain(stuff->dotnet_host, &unk);
  154. if (FAILED(hr))
  155. goto out;
  156. where = "QI: System._AppDomain";
  157. hr = IUnknown_QueryInterface(unk, &IID_mscorlib_System_AppDomain, (LPVOID*)&stuff->dotnet_domain);
  158. if (FAILED(hr))
  159. goto out;
  160. COMG(dotnet_runtime_stuff) = stuff;
  161. out:
  162. if (unk) {
  163. IUnknown_Release(unk);
  164. }
  165. if (COMG(dotnet_runtime_stuff) == NULL) {
  166. /* clean up */
  167. if (stuff->dotnet_domain) {
  168. IUnknown_Release(stuff->dotnet_domain);
  169. }
  170. if (stuff->dotnet_host) {
  171. ICorRuntimeHost_Stop(stuff->dotnet_host);
  172. ICorRuntimeHost_Release(stuff->dotnet_host);
  173. }
  174. free(stuff);
  175. *p_where = where;
  176. return hr;
  177. }
  178. return S_OK;
  179. }
  180. /* {{{ com_dotnet_create_instance - ctor for DOTNET class */
  181. PHP_METHOD(dotnet, __construct)
  182. {
  183. zval *object = getThis();
  184. php_com_dotnet_object *obj;
  185. char *assembly_name, *datatype_name;
  186. size_t assembly_name_len, datatype_name_len;
  187. struct dotnet_runtime_stuff *stuff;
  188. OLECHAR *oleassembly, *oletype;
  189. BSTR oleassembly_sys, oletype_sys;
  190. HRESULT hr;
  191. int ret = FAILURE;
  192. char *where = "";
  193. IUnknown *unk = NULL;
  194. zend_long cp = GetACP();
  195. const struct php_win32_cp *cp_it;
  196. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "ss|l",
  197. &assembly_name, &assembly_name_len,
  198. &datatype_name, &datatype_name_len,
  199. &cp)) {
  200. RETURN_THROWS();
  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. php_win32_error_msg_free(err);
  211. php_com_throw_exception(hr, buf);
  212. RETURN_THROWS();
  213. }
  214. stuff = (struct dotnet_runtime_stuff*)COMG(dotnet_runtime_stuff);
  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. php_win32_error_msg_free(err);
  223. php_com_throw_exception(hr, buf);
  224. ZVAL_NULL(object);
  225. RETURN_THROWS();
  226. }
  227. where = "QI: System._AppDomain";
  228. hr = IUnknown_QueryInterface(unk, &IID_mscorlib_System_AppDomain, (LPVOID*)&stuff->dotnet_domain);
  229. if (FAILED(hr)) {
  230. char buf[1024];
  231. char *err = php_win32_error_to_msg(hr);
  232. snprintf(buf, sizeof(buf), "Failed to re-init .Net domain [%s] %s", where, err);
  233. php_win32_error_msg_free(err);
  234. php_com_throw_exception(hr, buf);
  235. ZVAL_NULL(object);
  236. RETURN_THROWS();
  237. }
  238. }
  239. obj = CDNO_FETCH(object);
  240. cp_it = php_win32_cp_get_by_id((DWORD)cp);
  241. if (!cp_it) {
  242. php_com_throw_exception(E_INVALIDARG, "Could not create .Net object - invalid codepage!");
  243. RETURN_THROWS();
  244. }
  245. obj->code_page = (int)cp_it->id;
  246. oletype = php_com_string_to_olestring(datatype_name, datatype_name_len, obj->code_page);
  247. oleassembly = php_com_string_to_olestring(assembly_name, assembly_name_len, obj->code_page);
  248. oletype_sys = SysAllocString(oletype);
  249. oleassembly_sys = SysAllocString(oleassembly);
  250. where = "CreateInstance";
  251. hr = stuff->dotnet_domain->lpVtbl->CreateInstance(stuff->dotnet_domain, oleassembly_sys, oletype_sys, &unk);
  252. efree(oletype);
  253. efree(oleassembly);
  254. SysFreeString(oletype_sys);
  255. SysFreeString(oleassembly_sys);
  256. if (SUCCEEDED(hr)) {
  257. VARIANT unwrapped;
  258. IObjectHandle *handle = NULL;
  259. where = "QI: IObjectHandle";
  260. hr = IUnknown_QueryInterface(unk, &IID_IObjectHandle, &handle);
  261. if (SUCCEEDED(hr)) {
  262. where = "IObjectHandle_Unwrap";
  263. hr = IObjectHandle_Unwrap(handle, &unwrapped);
  264. if (SUCCEEDED(hr)) {
  265. if (V_VT(&unwrapped) == VT_UNKNOWN) {
  266. where = "Unwrapped, QI for IDispatch";
  267. hr = IUnknown_QueryInterface(V_UNKNOWN(&unwrapped), &IID_IDispatch, &V_DISPATCH(&obj->v));
  268. if (SUCCEEDED(hr)) {
  269. V_VT(&obj->v) = VT_DISPATCH;
  270. /* get its type-info */
  271. IDispatch_GetTypeInfo(V_DISPATCH(&obj->v), 0, LANG_NEUTRAL, &obj->typeinfo);
  272. ret = SUCCESS;
  273. }
  274. } else if (V_VT(&unwrapped) == VT_DISPATCH) {
  275. /* unwrapped is now the dispatch pointer we want */
  276. V_DISPATCH(&obj->v) = V_DISPATCH(&unwrapped);
  277. V_VT(&obj->v) = VT_DISPATCH;
  278. /* get its type-info */
  279. IDispatch_GetTypeInfo(V_DISPATCH(&obj->v), 0, LANG_NEUTRAL, &obj->typeinfo);
  280. ret = SUCCESS;
  281. } else {
  282. /* shouldn't happen, but let's be ready for it */
  283. VariantClear(&unwrapped);
  284. hr = E_INVALIDARG;
  285. }
  286. }
  287. IObjectHandle_Release(handle);
  288. }
  289. IUnknown_Release(unk);
  290. }
  291. if (ret == FAILURE) {
  292. char buf[1024];
  293. char *err = php_win32_error_to_msg(hr);
  294. snprintf(buf, sizeof(buf), "Failed to instantiate .Net object [%s] [0x%08x] %s", where, hr, err);
  295. php_win32_error_msg_free(err);
  296. php_com_throw_exception(hr, buf);
  297. RETURN_THROWS();
  298. }
  299. }
  300. /* }}} */
  301. void php_com_dotnet_mshutdown(void)
  302. {
  303. struct dotnet_runtime_stuff *stuff = COMG(dotnet_runtime_stuff);
  304. if (stuff->dotnet_domain) {
  305. IDispatch_Release(stuff->dotnet_domain);
  306. }
  307. if (stuff->dotnet_host) {
  308. ICorRuntimeHost_Stop(stuff->dotnet_host);
  309. ICorRuntimeHost_Release(stuff->dotnet_host);
  310. stuff->dotnet_host = NULL;
  311. }
  312. free(stuff);
  313. COMG(dotnet_runtime_stuff) = NULL;
  314. }
  315. void php_com_dotnet_rshutdown(void)
  316. {
  317. struct dotnet_runtime_stuff *stuff = COMG(dotnet_runtime_stuff);
  318. if (stuff->dotnet_domain) {
  319. IDispatch_Release(stuff->dotnet_domain);
  320. stuff->dotnet_domain = NULL;
  321. }
  322. }
  323. #endif /* HAVE_MSCOREE_H */