diff options
author | Christoph M. Becker <cmbecker69@gmx.de> | 2020-08-06 18:57:18 +0200 |
---|---|---|
committer | Christoph M. Becker <cmbecker69@gmx.de> | 2020-08-22 12:42:34 +0200 |
commit | e6044d4455d7fbaced7f3a6ac28172d963017e51 (patch) | |
tree | 31a91bbbc78709a3c339680231188a2558846b5e /ext/com_dotnet/com_dotnet.c | |
parent | 969a432fd8b34a0ec4e48e903370f0659aef6b04 (diff) | |
download | php-git-e6044d4455d7fbaced7f3a6ac28172d963017e51.tar.gz |
Fix #55847: DOTNET .NET 4.0 GAC new location
If we do not specify the exact version of the .NET framework to use,
the default CLR is loaded, which is typically CLR 2, which is very old.
Therefore, we introduce a `PHP_INI_SYSTEM` setting, which allows users
to choose the desired .NET framework version. The value of the setting
are the first three parts of the framework's version number, separated
by dots, and prefixed with "v", e.g. "v4.0.30319". If the value of the
INI setting is `NULL` (the default) or an empty string, the default CLR
is used.
Internally, we switch from the most generic `CoCreateInstance()` to
`CorBindToRuntime()` which is implemented in mscoree.dll. To avoid the
hard dependency to that library, we load dynamically.
So this fix is supposed to be fully backwards compatible.
Closes GH-5949
Diffstat (limited to 'ext/com_dotnet/com_dotnet.c')
-rw-r--r-- | ext/com_dotnet/com_dotnet.c | 44 |
1 files changed, 40 insertions, 4 deletions
diff --git a/ext/com_dotnet/com_dotnet.c b/ext/com_dotnet/com_dotnet.c index 6bd492ef1d..1b0ba2220c 100644 --- a/ext/com_dotnet/com_dotnet.c +++ b/ext/com_dotnet/com_dotnet.c @@ -117,6 +117,44 @@ struct dotnet_runtime_stuff { DISPID create_instance; }; +/* We link dynamically to mscoree.dll to avoid the hard dependency on .NET + * framework, which is only required if a dotnet instance is to be created. + */ +static HRESULT dotnet_bind_runtime(LPVOID FAR *ppv) +{ + HRESULT hr; + HMODULE mscoree; + typedef HRESULT (STDAPICALLTYPE *cbtr_t)(LPCWSTR pwszVersion, LPCWSTR pwszBuildFlavor, REFCLSID rclsid, REFIID riid, LPVOID FAR *ppv); + cbtr_t CorBindToRuntime; + OLECHAR *oleversion; + char *version; + + mscoree = LoadLibraryA("mscoree.dll"); + if (mscoree == NULL) { + return S_FALSE; + } + + CorBindToRuntime = (cbtr_t) GetProcAddress(mscoree, "CorBindToRuntime"); + if (CorBindToRuntime == NULL) { + FreeLibrary(mscoree); + return S_FALSE; + } + + version = INI_STR("com.dotnet_version"); + if (version == NULL || *version == '\0') { + oleversion = NULL; + } else { + oleversion = php_com_string_to_olestring(version, strlen(version), COMG(code_page)); + } + + hr = CorBindToRuntime(oleversion, NULL, &CLSID_CorRuntimeHost, &IID_ICorRuntimeHost, ppv); + + efree(oleversion); + FreeLibrary(mscoree); + + return hr; +} + static HRESULT dotnet_init(char **p_where) { HRESULT hr; @@ -130,10 +168,8 @@ static HRESULT dotnet_init(char **p_where) } memset(stuff, 0, sizeof(*stuff)); - where = "CoCreateInstance"; - hr = CoCreateInstance(&CLSID_CorRuntimeHost, NULL, CLSCTX_ALL, - &IID_ICorRuntimeHost, (LPVOID*)&stuff->dotnet_host); - + where = "dotnet_bind_runtime"; + hr = dotnet_bind_runtime((LPVOID*)&stuff->dotnet_host); if (FAILED(hr)) goto out; |