-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Description
Background and motivation
Currently, coreclr_initialize
is not supported to be called more than once. On the other hand, there is not any method to retrieve an already initialized host, either.
As a result, if one ever want to execute any .NET Core code from native, he must be the first one to contact coreclr, or he is completely out of luck.
Situation: one is writing a plugin which is to be loaded by a native host. The native host requires the plugin to have a native entry point and is completely out of his control. At the same time, there are also other plugins which are also completely out of his control.
Then he want to write part of his code in C#, thus necessitate the hosting of .NET Core. However, he is not the first one to be clever enough to use .NET Core, and there is already someone loading coreclr into the process.
It is not hard to retrieve an already loaded coreclr module (by GetModuleHandleW, for example), but even if one has access to the coreclr module, he cannot run any managed code if coreclr_initialize
is already called by others and the returned host handle is not shared to him.
He is okay with the limitations like only one version of coreclr can exist, the host can only be initialized once, etc. He is also willing to accept some caveats like the host initialized by others might not have properties set to what exactly he wants. All he want is just having a chance to even try to load a managed assembly and execute some code.
But he is still completely out of luck in this case. Any coreclr method to load managed code requires a host handle to start with, but he has no way to get such a handle unless he is the lucky one making the first ever call to coreclr_initialize
among the whole process.
Request: A way for native users other than the first caller of coreclr_initialize
to have a chance to try to load and execute any managed code, provided that they are already aware of any limitation and caveats, like only one coreclr version can exist and the host properties cannot be changed.
API Proposal
//
// Retrieving an already created host handle, provided that there is any.
//
// Parameters:
// [out] hostHandle - Handle of an already created host
//
// Returns:
// HRESULT indicating status of the operation. S_OK if there is a suitable handle.
//
extern int coreclr_get_active_host(void** hostHandle);
API Usage
HRESULT hr;
void* hostHandle;
hr = coreclr_get_active_host(&hostHandle);
if (SUCCEEDED(hr)) {
// coreclr_initialize is already called, try to load my code into this host.
} else {
// try to do host initialization myself.
}
Risks
It is not always possible to load an assembly to a host created by others, the .NET version may differ, some properties may cause conflict, etc.
Nonetheless, without such an API it will be plain impossible to ever have a try.
Metadata
Metadata
Assignees
Labels
Type
Projects
Status