[PATCH] mscoree: Implement ICorRuntimeHost::CreateDomain{, Ex}(). (try 3)

Charles Davis cdavis5x at gmail.com
Mon Feb 15 19:37:18 CST 2016


Try 3:
* Get the actual domain instead of the proxy object that Mono gives us.
  Based on a suggestion by Vincent Povirk.
* Add a test to ensure this actually works, based on code by Vincent.

Signed-off-by: Charles Davis <cdavis5x at gmail.com>
---
 dlls/mscoree/corruntimehost.c  | 130 +++++++++++++++++++++++++++++++++++++++--
 dlls/mscoree/metahost.c        |   2 +
 dlls/mscoree/mscoree_private.h |   1 +
 dlls/mscoree/tests/mscoree.c   |  90 +++++++++++++++++++++++++++-
 4 files changed, 215 insertions(+), 8 deletions(-)

diff --git a/dlls/mscoree/corruntimehost.c b/dlls/mscoree/corruntimehost.c
index 8c03875..e57c7f5 100644
--- a/dlls/mscoree/corruntimehost.c
+++ b/dlls/mscoree/corruntimehost.c
@@ -68,7 +68,7 @@ struct dll_fixup
     void *tokens; /* pointer into process heap */
 };
 
-static HRESULT RuntimeHost_AddDomain(RuntimeHost *This, MonoDomain **result)
+static HRESULT RuntimeHost_AddDefaultDomain(RuntimeHost *This, MonoDomain **result)
 {
     struct DomainEntry *entry;
     HRESULT res=S_OK;
@@ -115,7 +115,7 @@ static HRESULT RuntimeHost_GetDefaultDomain(RuntimeHost *This, const WCHAR *conf
 
     if (This->default_domain) goto end;
 
-    res = RuntimeHost_AddDomain(This, &This->default_domain);
+    res = RuntimeHost_AddDefaultDomain(This, &This->default_domain);
 
     if (!config_path)
     {
@@ -257,6 +257,104 @@ static HRESULT RuntimeHost_Invoke(RuntimeHost *This, MonoDomain *domain,
     return S_OK;
 }
 
+static HRESULT RuntimeHost_GetObjectForIUnknown(RuntimeHost *This, MonoDomain *domain,
+    IUnknown *unk, MonoObject **obj)
+{
+    HRESULT hr;
+    void *args[1];
+    MonoObject *result;
+
+    args[0] = &unk;
+    hr = RuntimeHost_Invoke(This, domain, "mscorlib", "System.Runtime.InteropServices", "Marshal", "GetObjectForIUnknown",
+        NULL, args, 1, &result);
+
+    if (SUCCEEDED(hr))
+    {
+        *obj = result;
+    }
+    return hr;
+}
+
+static HRESULT RuntimeHost_AddDomain(RuntimeHost *This, const WCHAR *name, IUnknown *setup,
+    IUnknown *evidence, MonoDomain **result)
+{
+    HRESULT res;
+    char *nameA;
+    MonoDomain *domain;
+    void *args[3];
+    MonoObject *new_domain, *id;
+
+    res = RuntimeHost_GetDefaultDomain(This, NULL, &domain);
+    if (FAILED(res))
+    {
+        return res;
+    }
+
+    nameA = WtoA(name);
+    if (!nameA)
+    {
+        return E_OUTOFMEMORY;
+    }
+
+    args[0] = mono_string_new(domain, nameA);
+    HeapFree(GetProcessHeap(), 0, nameA);
+
+    if (!args[0])
+    {
+        return E_OUTOFMEMORY;
+    }
+
+    if (evidence)
+    {
+        res = RuntimeHost_GetObjectForIUnknown(This, domain, evidence, (MonoObject **)&args[1]);
+        if (FAILED(res))
+        {
+            return res;
+        }
+    }
+    else
+    {
+        args[1] = NULL;
+    }
+
+    if (setup)
+    {
+        res = RuntimeHost_GetObjectForIUnknown(This, domain, setup, (MonoObject **)&args[2]);
+        if (FAILED(res))
+        {
+            return res;
+        }
+    }
+    else
+    {
+        args[2] = NULL;
+    }
+
+    res = RuntimeHost_Invoke(This, domain, "mscorlib", "System", "AppDomain", "CreateDomain",
+        NULL, args, 3, &new_domain);
+
+    if (FAILED(res))
+    {
+        return res;
+    }
+
+    /* new_domain is not the AppDomain itself, but a transparent proxy.
+     * So, we'll retrieve its ID, and use that to get the real domain object.
+     */
+
+    res = RuntimeHost_Invoke(This, domain, "mscorlib", "System", "AppDomain", "get_ID",
+        new_domain, NULL, 0, &id);
+
+    if (FAILED(res))
+    {
+        return res;
+    }
+
+    *result = mono_domain_get_by_id(*(int32_t *)mono_object_unbox(id));
+
+    return S_OK;
+}
+
 static HRESULT RuntimeHost_GetIUnknownForDomain(RuntimeHost *This, MonoDomain *domain, IUnknown **punk)
 {
     HRESULT hr;
@@ -432,8 +530,7 @@ static HRESULT WINAPI corruntimehost_CreateDomain(
     IUnknown *identityArray,
     IUnknown **appDomain)
 {
-    FIXME("stub %p\n", iface);
-    return E_NOTIMPL;
+    return ICorRuntimeHost_CreateDomainEx(iface, friendlyName, NULL, NULL, appDomain);
 }
 
 static HRESULT WINAPI corruntimehost_GetDefaultDomain(
@@ -488,8 +585,29 @@ static HRESULT WINAPI corruntimehost_CreateDomainEx(
     IUnknown *evidence,
     IUnknown **appDomain)
 {
-    FIXME("stub %p\n", iface);
-    return E_NOTIMPL;
+    RuntimeHost *This = impl_from_ICorRuntimeHost( iface );
+    HRESULT hr;
+    MonoDomain *domain;
+
+    if (!friendlyName || !appDomain)
+    {
+        return E_POINTER;
+    }
+    if (!is_mono_started)
+    {
+        return E_FAIL;
+    }
+
+    TRACE("(%p)\n", iface);
+
+    hr = RuntimeHost_AddDomain(This, friendlyName, setup, evidence, &domain);
+
+    if (SUCCEEDED(hr))
+    {
+        hr = RuntimeHost_GetIUnknownForDomain(This, domain, appDomain);
+    }
+
+    return hr;
 }
 
 static HRESULT WINAPI corruntimehost_CreateDomainSetup(
diff --git a/dlls/mscoree/metahost.c b/dlls/mscoree/metahost.c
index 3d663f1..676b92c 100644
--- a/dlls/mscoree/metahost.c
+++ b/dlls/mscoree/metahost.c
@@ -79,6 +79,7 @@ MonoClass* (CDECL *mono_class_from_name)(MonoImage *image, const char* name_spac
 MonoMethod* (CDECL *mono_class_get_method_from_name)(MonoClass *klass, const char *name, int param_count);
 static void (CDECL *mono_config_parse)(const char *filename);
 MonoAssembly* (CDECL *mono_domain_assembly_open)(MonoDomain *domain, const char *name);
+MonoDomain* (CDECL *mono_domain_get_by_id)(int32_t id);
 void (CDECL *mono_domain_set_config)(MonoDomain *domain,const char *base_dir,const char *config_file_name);
 static void (CDECL *mono_free)(void *);
 static MonoImage* (CDECL *mono_image_open)(const char *fname, MonoImageOpenStatus *status);
@@ -175,6 +176,7 @@ static HRESULT load_mono(LPCWSTR mono_path)
         LOAD_MONO_FUNCTION(mono_class_from_name);
         LOAD_MONO_FUNCTION(mono_class_get_method_from_name);
         LOAD_MONO_FUNCTION(mono_domain_assembly_open);
+        LOAD_MONO_FUNCTION(mono_domain_get_by_id);
         LOAD_MONO_FUNCTION(mono_domain_set_config);
         LOAD_MONO_FUNCTION(mono_free);
         LOAD_MONO_FUNCTION(mono_image_open);
diff --git a/dlls/mscoree/mscoree_private.h b/dlls/mscoree/mscoree_private.h
index 8226463..f236141 100644
--- a/dlls/mscoree/mscoree_private.h
+++ b/dlls/mscoree/mscoree_private.h
@@ -146,6 +146,7 @@ extern MonoClass* (CDECL *mono_class_from_mono_type)(MonoType *type) DECLSPEC_HI
 extern MonoClass* (CDECL *mono_class_from_name)(MonoImage *image, const char* name_space, const char *name) DECLSPEC_HIDDEN;
 extern MonoMethod* (CDECL *mono_class_get_method_from_name)(MonoClass *klass, const char *name, int param_count) DECLSPEC_HIDDEN;
 extern MonoAssembly* (CDECL *mono_domain_assembly_open)(MonoDomain *domain, const char *name) DECLSPEC_HIDDEN;
+extern MonoDomain* (CDECL *mono_domain_get_by_id)(int32_t id);
 extern void (CDECL *mono_domain_set_config)(MonoDomain *domain,const char *base_dir,const char *config_file_name) DECLSPEC_HIDDEN;
 extern int (CDECL *mono_jit_exec)(MonoDomain *domain, MonoAssembly *assembly, int argc, char *argv[]) DECLSPEC_HIDDEN;
 extern MonoDomain* (CDECL *mono_jit_init_version)(const char *domain_name, const char *runtime_version) DECLSPEC_HIDDEN;
diff --git a/dlls/mscoree/tests/mscoree.c b/dlls/mscoree/tests/mscoree.c
index 30b068d..80726ae 100644
--- a/dlls/mscoree/tests/mscoree.c
+++ b/dlls/mscoree/tests/mscoree.c
@@ -25,6 +25,10 @@
 #include "shlwapi.h"
 #include "wine/test.h"
 
+DEFINE_GUID(IID__AppDomain, 0x05f696dc,0x2b29,0x3663,0xad,0x8b,0xc4,0x38,0x9c,0xf2,0xa7,0x13);
+
+static const WCHAR v4_0[] = {'v','4','.','0','.','3','0','3','1','9',0};
+
 static HMODULE hmscoree;
 
 static HRESULT (WINAPI *pGetCORVersion)(LPWSTR, DWORD, DWORD*);
@@ -33,6 +37,7 @@ static HRESULT (WINAPI *pGetRequestedRuntimeInfo)(LPCWSTR, LPCWSTR, LPCWSTR, DWO
 static HRESULT (WINAPI *pLoadLibraryShim)(LPCWSTR, LPCWSTR, LPVOID, HMODULE*);
 static HRESULT (WINAPI *pCreateConfigStream)(LPCWSTR, IStream**);
 static HRESULT (WINAPI *pCreateInterface)(REFCLSID, REFIID, VOID**);
+static HRESULT (WINAPI *pCLRCreateInstance)(REFCLSID, REFIID, VOID**);
 
 static BOOL no_legacy_runtimes;
 
@@ -52,9 +57,10 @@ static BOOL init_functionpointers(void)
     pLoadLibraryShim = (void *)GetProcAddress(hmscoree, "LoadLibraryShim");
     pCreateConfigStream = (void *)GetProcAddress(hmscoree, "CreateConfigStream");
     pCreateInterface =  (void *)GetProcAddress(hmscoree, "CreateInterface");
+    pCLRCreateInstance = (void *)GetProcAddress(hmscoree, "CLRCreateInstance");
 
     if (!pGetCORVersion || !pGetCORSystemDirectory || !pGetRequestedRuntimeInfo || !pLoadLibraryShim ||
-        !pCreateInterface)
+        !pCreateInterface || !pCLRCreateInstance)
     {
         win_skip("functions not available\n");
         FreeLibrary(hmscoree);
@@ -195,7 +201,6 @@ static void test_versioninfo(void)
 
 static void test_loadlibraryshim(void)
 {
-    const WCHAR v4_0[] = {'v','4','.','0','.','3','0','3','1','9',0};
     const WCHAR v2_0[] = {'v','2','.','0','.','5','0','7','2','7',0};
     const WCHAR v1_1[] = {'v','1','.','1','.','4','3','2','2',0};
     const WCHAR vbogus[] = {'v','b','o','g','u','s',0};
@@ -425,6 +430,87 @@ static void test_createinstance(void)
     }
 }
 
+static void test_createdomain(void)
+{
+    static const WCHAR test_name[] = {'t','e','s','t',0};
+    static const WCHAR test2_name[] = {'t','e','s','t','2',0};
+    ICLRMetaHost *metahost;
+    ICLRRuntimeInfo *runtimeinfo;
+    ICorRuntimeHost *runtimehost;
+    IUnknown *domain, *defaultdomain_unk, *defaultdomain, *newdomain_unk, *newdomain, *domainsetup,
+        *newdomain2_unk, *newdomain2;
+    HRESULT hr;
+
+    if (!pCLRCreateInstance)
+    {
+        win_skip("Function CLRCreateInstance not found.");
+        return;
+    }
+
+    hr = pCLRCreateInstance(&CLSID_CLRMetaHost, &IID_ICLRMetaHost, (void **)&metahost);
+    ok(SUCCEEDED(hr), "CLRCreateInstance failed, hr=%#.8x\n", hr);
+
+    hr = ICLRMetaHost_GetRuntime(metahost, v4_0, &IID_ICLRRuntimeInfo, (void **)&runtimeinfo);
+    ok(SUCCEEDED(hr), "ICLRMetaHost::GetRuntime failed, hr=%#.8x\n", hr);
+
+    hr = ICLRRuntimeInfo_GetInterface(runtimeinfo, &CLSID_CorRuntimeHost, &IID_ICorRuntimeHost,
+        (void **)&runtimehost);
+    ok(SUCCEEDED(hr), "ICLRRuntimeInfo::GetInterface failed, hr=%#.8x\n", hr);
+
+    hr = ICorRuntimeHost_Start(runtimehost);
+    ok(SUCCEEDED(hr), "ICorRuntimeHost::Start failed, hr=%#.8x\n", hr);
+
+    hr = ICorRuntimeHost_GetDefaultDomain(runtimehost, &domain);
+    ok(SUCCEEDED(hr), "ICorRuntimeHost::GetDefaultDomain failed, hr=%#.8x\n", hr);
+
+    hr = IUnknown_QueryInterface(domain, &IID_IUnknown, (void **)&defaultdomain_unk);
+    ok(SUCCEEDED(hr), "COM object doesn't support IUnknown?!\n");
+
+    hr = IUnknown_QueryInterface(domain, &IID__AppDomain, (void **)&defaultdomain);
+    ok(SUCCEEDED(hr), "AppDomain object doesn't support _AppDomain interface\n");
+
+    IUnknown_Release(domain);
+
+    hr = ICorRuntimeHost_CreateDomain(runtimehost, test_name, NULL, &domain);
+    ok(SUCCEEDED(hr), "ICorRuntimeHost::CreateDomain failed, hr=%#.8x\n", hr);
+
+    hr = IUnknown_QueryInterface(domain, &IID_IUnknown, (void **)&newdomain_unk);
+    ok(SUCCEEDED(hr), "COM object doesn't support IUnknown?!\n");
+
+    hr = IUnknown_QueryInterface(domain, &IID__AppDomain, (void **)&newdomain);
+    ok(SUCCEEDED(hr), "AppDomain object doesn't support _AppDomain interface\n");
+
+    IUnknown_Release(domain);
+
+    hr = ICorRuntimeHost_CreateDomainSetup(runtimehost, &domainsetup);
+    ok(SUCCEEDED(hr), "ICorRuntimeHost::CreateDomainSetup failed, hr=%#.8x\n", hr);
+
+    hr = ICorRuntimeHost_CreateDomainEx(runtimehost, test2_name, domainsetup, NULL, &domain);
+    ok(SUCCEEDED(hr), "ICorRuntimeHost::CreateDomainEx failed, hr=%#.8x\n", hr);
+
+    hr = IUnknown_QueryInterface(domain, &IID_IUnknown, (void **)&newdomain2_unk);
+    ok(SUCCEEDED(hr), "COM object doesn't support IUnknown?!\n");
+
+    hr = IUnknown_QueryInterface(domain, &IID__AppDomain, (void **)&newdomain2);
+    ok(SUCCEEDED(hr), "AppDomain object doesn't support _AppDomain interface\n");
+
+    IUnknown_Release(domain);
+
+    IUnknown_Release(newdomain2);
+    IUnknown_Release(newdomain2_unk);
+    IUnknown_Release(domainsetup);
+    IUnknown_Release(newdomain);
+    IUnknown_Release(newdomain_unk);
+    IUnknown_Release(defaultdomain);
+    IUnknown_Release(defaultdomain_unk);
+
+    ICorRuntimeHost_Release(runtimehost);
+
+    ICLRRuntimeInfo_Release(runtimeinfo);
+
+    ICLRMetaHost_Release(metahost);
+}
+
 START_TEST(mscoree)
 {
     if (!init_functionpointers())
-- 
2.7.1




More information about the wine-patches mailing list