mscoree: Implement ClrCreateManagedInstance.

This commit is contained in:
Vincent Povirk 2010-10-05 17:15:08 -05:00 committed by Alexandre Julliard
parent d2d743efbb
commit 3b76a0bed1
4 changed files with 193 additions and 2 deletions

View File

@ -496,6 +496,138 @@ static const struct ICLRRuntimeHostVtbl CLRHostVtbl =
CLRRuntimeHost_ExecuteInDefaultAppDomain CLRRuntimeHost_ExecuteInDefaultAppDomain
}; };
/* Create an instance of a type given its name, by calling its constructor with
* no arguments. Note that result MUST be in the stack, or the garbage
* collector may free it prematurely. */
HRESULT RuntimeHost_CreateManagedInstance(RuntimeHost *This, LPCWSTR name,
MonoDomain *domain, MonoObject **result)
{
HRESULT hr=S_OK;
char *nameA=NULL;
MonoType *type;
MonoClass *klass;
MonoObject *obj;
if (!domain)
hr = RuntimeHost_GetDefaultDomain(This, &domain);
if (SUCCEEDED(hr))
{
nameA = WtoA(name);
if (!nameA)
hr = E_OUTOFMEMORY;
}
if (SUCCEEDED(hr))
{
type = This->mono->mono_reflection_type_from_name(nameA, NULL);
if (!type)
{
ERR("Cannot find type %s\n", debugstr_w(name));
hr = E_FAIL;
}
}
if (SUCCEEDED(hr))
{
klass = This->mono->mono_class_from_mono_type(type);
if (!klass)
{
ERR("Cannot convert type %s to a class\n", debugstr_w(name));
hr = E_FAIL;
}
}
if (SUCCEEDED(hr))
{
obj = This->mono->mono_object_new(domain, klass);
if (!obj)
{
ERR("Cannot allocate object of type %s\n", debugstr_w(name));
hr = E_FAIL;
}
}
if (SUCCEEDED(hr))
{
/* FIXME: Detect exceptions from the constructor? */
This->mono->mono_runtime_object_init(obj);
*result = obj;
}
HeapFree(GetProcessHeap(), 0, nameA);
return hr;
}
/* Get an IUnknown pointer for a Mono object.
*
* This is just a "light" wrapper around
* System.Runtime.InteropServices.Marshal:GetIUnknownForObject
*
* NOTE: The IUnknown* is created with a reference to the object.
* Until they have a reference, objects must be in the stack to prevent the
* garbage collector from freeing them. */
HRESULT RuntimeHost_GetIUnknownForObject(RuntimeHost *This, MonoObject *obj,
IUnknown **ppUnk)
{
MonoDomain *domain;
MonoAssembly *assembly;
MonoImage *image;
MonoClass *klass;
MonoMethod *method;
MonoObject *result;
void *args[2];
domain = This->mono->mono_object_get_domain(obj);
assembly = This->mono->mono_domain_assembly_open(domain, "mscorlib");
if (!assembly)
{
ERR("Cannot load mscorlib\n");
return E_FAIL;
}
image = This->mono->mono_assembly_get_image(assembly);
if (!image)
{
ERR("Couldn't get assembly image\n");
return E_FAIL;
}
klass = This->mono->mono_class_from_name(image, "System.Runtime.InteropServices", "Marshal");
if (!klass)
{
ERR("Couldn't get class from image\n");
return E_FAIL;
}
method = This->mono->mono_class_get_method_from_name(klass, "GetIUnknownForObject", 1);
if (!method)
{
ERR("Couldn't get method from class\n");
return E_FAIL;
}
args[0] = obj;
args[1] = NULL;
result = This->mono->mono_runtime_invoke(method, NULL, args, NULL);
if (!result)
{
ERR("Couldn't get result pointer\n");
return E_FAIL;
}
*ppUnk = *(IUnknown**)This->mono->mono_object_unbox(result);
if (!*ppUnk)
{
ERR("GetIUnknownForObject returned 0\n");
return E_FAIL;
}
return S_OK;
}
static void get_utf8_args(int *argc, char ***argv) static void get_utf8_args(int *argc, char ***argv)
{ {
WCHAR **argvw; WCHAR **argvw;

View File

@ -138,14 +138,24 @@ static HRESULT load_mono(CLRRuntimeInfo *This, loaded_mono **result)
} \ } \
} while (0); } while (0);
LOAD_MONO_FUNCTION(mono_assembly_get_image);
LOAD_MONO_FUNCTION(mono_assembly_open); LOAD_MONO_FUNCTION(mono_assembly_open);
LOAD_MONO_FUNCTION(mono_config_parse); LOAD_MONO_FUNCTION(mono_config_parse);
LOAD_MONO_FUNCTION(mono_class_from_mono_type);
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_assembly_open);
LOAD_MONO_FUNCTION(mono_install_assembly_preload_hook); LOAD_MONO_FUNCTION(mono_install_assembly_preload_hook);
LOAD_MONO_FUNCTION(mono_jit_cleanup); LOAD_MONO_FUNCTION(mono_jit_cleanup);
LOAD_MONO_FUNCTION(mono_jit_exec); LOAD_MONO_FUNCTION(mono_jit_exec);
LOAD_MONO_FUNCTION(mono_jit_init); LOAD_MONO_FUNCTION(mono_jit_init);
LOAD_MONO_FUNCTION(mono_jit_set_trace_options); LOAD_MONO_FUNCTION(mono_jit_set_trace_options);
LOAD_MONO_FUNCTION(mono_object_get_domain);
LOAD_MONO_FUNCTION(mono_object_new);
LOAD_MONO_FUNCTION(mono_object_unbox);
LOAD_MONO_FUNCTION(mono_reflection_type_from_name);
LOAD_MONO_FUNCTION(mono_runtime_invoke);
LOAD_MONO_FUNCTION(mono_runtime_object_init);
LOAD_MONO_FUNCTION(mono_set_dirs); LOAD_MONO_FUNCTION(mono_set_dirs);
LOAD_MONO_FUNCTION(mono_stringify_assembly_name); LOAD_MONO_FUNCTION(mono_stringify_assembly_name);

View File

@ -369,8 +369,37 @@ HRESULT WINAPI CorBindToCurrentRuntime(LPCWSTR filename, REFCLSID rclsid, REFIID
STDAPI ClrCreateManagedInstance(LPCWSTR pTypeName, REFIID riid, void **ppObject) STDAPI ClrCreateManagedInstance(LPCWSTR pTypeName, REFIID riid, void **ppObject)
{ {
FIXME("(%s,%s,%p)\n", debugstr_w(pTypeName), debugstr_guid(riid), ppObject); HRESULT ret;
return E_NOTIMPL; ICLRRuntimeInfo *info;
RuntimeHost *host;
MonoObject *obj;
IUnknown *unk;
TRACE("(%s,%s,%p)\n", debugstr_w(pTypeName), debugstr_guid(riid), ppObject);
/* FIXME: How to determine which runtime version to use? */
ret = get_runtime_info(NULL, NULL, NULL, 0, RUNTIME_INFO_UPGRADE_VERSION, TRUE, &info);
if (SUCCEEDED(ret))
{
ret = ICLRRuntimeInfo_GetRuntimeHost(info, &host);
ICLRRuntimeInfo_Release(info);
}
if (SUCCEEDED(ret))
ret = RuntimeHost_CreateManagedInstance(host, pTypeName, NULL, &obj);
if (SUCCEEDED(ret))
ret = RuntimeHost_GetIUnknownForObject(host, obj, &unk);
if (SUCCEEDED(ret))
{
ret = IUnknown_QueryInterface(unk, riid, ppObject);
IUnknown_Release(unk);
}
return ret;
} }
BOOL WINAPI StrongNameSignatureVerification(LPCWSTR filename, DWORD inFlags, DWORD* pOutFlags) BOOL WINAPI StrongNameSignatureVerification(LPCWSTR filename, DWORD inFlags, DWORD* pOutFlags)

View File

@ -77,6 +77,11 @@ extern void free_parsed_config_file(parsed_config_file *file);
typedef struct _MonoDomain MonoDomain; typedef struct _MonoDomain MonoDomain;
typedef struct _MonoAssembly MonoAssembly; typedef struct _MonoAssembly MonoAssembly;
typedef struct _MonoAssemblyName MonoAssemblyName; typedef struct _MonoAssemblyName MonoAssemblyName;
typedef struct _MonoType MonoType;
typedef struct _MonoImage MonoImage;
typedef struct _MonoClass MonoClass;
typedef struct _MonoObject MonoObject;
typedef struct _MonoMethod MonoMethod;
typedef enum { typedef enum {
MONO_IMAGE_OK, MONO_IMAGE_OK,
@ -92,7 +97,11 @@ typedef struct loaded_mono
HMODULE mono_handle; HMODULE mono_handle;
HMODULE glib_handle; HMODULE glib_handle;
MonoImage* (*mono_assembly_get_image)(MonoAssembly *assembly);
MonoAssembly* (*mono_assembly_open)(const char *filename, MonoImageOpenStatus *status); MonoAssembly* (*mono_assembly_open)(const char *filename, MonoImageOpenStatus *status);
MonoClass* (*mono_class_from_mono_type)(MonoType *type);
MonoClass* (*mono_class_from_name)(MonoImage *image, const char* name_space, const char *name);
MonoMethod* (*mono_class_get_method_from_name)(MonoClass *klass, const char *name, int param_count);
void (*mono_config_parse)(const char *filename); void (*mono_config_parse)(const char *filename);
MonoAssembly* (*mono_domain_assembly_open) (MonoDomain *domain, const char *name); MonoAssembly* (*mono_domain_assembly_open) (MonoDomain *domain, const char *name);
void (*mono_free)(void *); void (*mono_free)(void *);
@ -101,6 +110,12 @@ typedef struct loaded_mono
int (*mono_jit_exec)(MonoDomain *domain, MonoAssembly *assembly, int argc, char *argv[]); int (*mono_jit_exec)(MonoDomain *domain, MonoAssembly *assembly, int argc, char *argv[]);
MonoDomain* (*mono_jit_init)(const char *file); MonoDomain* (*mono_jit_init)(const char *file);
int (*mono_jit_set_trace_options)(const char* options); int (*mono_jit_set_trace_options)(const char* options);
MonoDomain* (*mono_object_get_domain)(MonoObject *obj);
MonoObject* (*mono_object_new)(MonoDomain *domain, MonoClass *klass);
void* (*mono_object_unbox)(MonoObject *obj);
MonoType* (*mono_reflection_type_from_name)(char *name, MonoImage *image);
MonoObject* (*mono_runtime_invoke)(MonoMethod *method, void *obj, void **params, MonoObject **exc);
void (*mono_runtime_object_init)(MonoObject *this_obj);
void (*mono_set_dirs)(const char *assembly_dir, const char *config_dir); void (*mono_set_dirs)(const char *assembly_dir, const char *config_dir);
char* (*mono_stringify_assembly_name)(MonoAssemblyName *aname); char* (*mono_stringify_assembly_name)(MonoAssemblyName *aname);
} loaded_mono; } loaded_mono;
@ -113,6 +128,11 @@ extern HRESULT RuntimeHost_Construct(const CLRRuntimeInfo *runtime_version,
extern HRESULT RuntimeHost_GetInterface(RuntimeHost *This, REFCLSID clsid, REFIID riid, void **ppv); extern HRESULT RuntimeHost_GetInterface(RuntimeHost *This, REFCLSID clsid, REFIID riid, void **ppv);
extern HRESULT RuntimeHost_GetIUnknownForObject(RuntimeHost *This, MonoObject *obj, IUnknown **ppUnk);
extern HRESULT RuntimeHost_CreateManagedInstance(RuntimeHost *This, LPCWSTR name,
MonoDomain *domain, MonoObject **result);
extern HRESULT RuntimeHost_Destroy(RuntimeHost *This); extern HRESULT RuntimeHost_Destroy(RuntimeHost *This);
#endif /* __MSCOREE_PRIVATE__ */ #endif /* __MSCOREE_PRIVATE__ */