.Net Core CLR提供两种Host API访问 托管代码的形式,按照微软官方的说法,一种是通过CoreClr.DLL来直接调用托管生成的DLL程序集,另外一种是通过CoreClr里面的C导出函数GetCLRRuntimeHost获取到IID_ICLRRuntimeHost4然后访问托管代码。
其实这两种形式可以合二为一,第一种更简单,更方便的控制托管代码。第二种更灵活些,在一些老旧的主机上会用到这些代码,实际上第一种形式是扩充了第二种访问形式,进行了一个整体封装,原理上其实还是一样的。
实际上这两种形式的实现都可以,作为自定义CLR Host进行定制自己一些业务上或者需求上的作业。
假如说,你要自己定制Core CLR Host ,name这两种方式是最好的选择,也是微提供表层API的实现形式
第二种形式实现: 在上一篇博客中,地址:https://www.cnblogs.com/tangyanzhi1111/p/10524451.html
第一种形式如下实现:
#include "stdafx.h" dataSize, double* data, report_callback_ptr callbackFunction);
void BuildTpaList(const char* directory, const char* extension, std::string& tpaList); typedef void (STDMETHODCALLTYPE MainMethodFp)(LPWSTR* args); typedef char*(*doWork_ptr)(char* abc);
int main(int argc, char* argv[])
{
HMODULE coreClr = LoadLibraryExA((LPCSTR)("coreclr.dll"), NULL, ); coreclr_initialize_ptr initializeCoreClr = (coreclr_initialize_ptr)GetProcAddress(coreClr, "coreclr_initialize");
coreclr_create_delegate_ptr createManagedDelegate = (coreclr_create_delegate_ptr)GetProcAddress(coreClr, "coreclr_create_delegate");
coreclr_shutdown_ptr shutdownCoreClr = (coreclr_shutdown_ptr)GetProcAddress(coreClr, "coreclr_shutdown");
coreclr_execute_assembly_ptr executeAssembly = (coreclr_execute_assembly_ptr)GetProcAddress(coreClr, "coreclr_execute_assembly");
std::string tpaList; BuildTpaList(runtimePath, ".dll", tpaList);
const char* propertyKeys[] = {
"TRUSTED_PLATFORM_ASSEMBLIES" // Trusted assemblies
}; const char* propertyValues[] = {
tpaList.c_str()
}; void* hostHandle;
unsigned int domainId; int hr = initializeCoreClr(
runtimePath, // App base path
"SampleHost", // AppDomain friendly name
sizeof(propertyKeys) / sizeof(char*), // Property count
propertyKeys, // Property names
propertyValues, // Property values
&hostHandle, // Host handle
&domainId); // AppDomain ID doWork_ptr managedDelegate;
hr = createManagedDelegate(
hostHandle,
domainId,
"Main, Version=1.0.0.0",
"Main.Program",
"Main",
(void**)&managedDelegate); ((MainMethodFp*)managedDelegate)(NULL);
double data[];
data[] = ;
data[] = 0.25;
data[] = 0.5;
data[] = 0.75; // Invoke the managed delegate and write the returned string to the console
//char* ret = managedDelegate("Test job", 5, sizeof(data) / sizeof(double), data, ReportProgressCallback); //printf("Managed code returned: %s\n", ret); //CoTaskMemFree(ret); hr = shutdownCoreClr(hostHandle, domainId); if (!FreeLibrary(coreClr))
{
printf("Failed to free coreclr.dll\n");
}
return ;
} void BuildTpaList(const char* directory, const char* extension, std::string& tpaList)
{ std::string searchPath(directory);
searchPath.append("\\");
searchPath.append("*");
searchPath.append(extension); WIN32_FIND_DATAA findData;
HANDLE fileHandle = FindFirstFileA(searchPath.c_str(), &findData); if (fileHandle != INVALID_HANDLE_VALUE)
{
do
{
tpaList.append(directory);
tpaList.append("\\");
tpaList.append(findData.cFileName);
tpaList.append(";"); } while (FindNextFileA(fileHandle, &findData));
FindClose(fileHandle);
}
} int ReportProgressCallback(int progress)
{
printf("Received status from managed code: %d\n", progress);
return -progress;
} Java/.Net讨论群:676817308