.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
第一种形式如下实现:
1 #include "stdafx.h" 2 3 dataSize, double* data, report_callback_ptr callbackFunction); 4 void BuildTpaList(const char* directory, const char* extension, std::string& tpaList); 5 6 typedef void (STDMETHODCALLTYPE MainMethodFp)(LPWSTR* args); 7 8 typedef char*(*doWork_ptr)(char* abc); 9 int main(int argc, char* argv[]) 10 { 11 HMODULE coreClr = LoadLibraryExA((LPCSTR)("coreclr.dll"), NULL, 0); 12 13 coreclr_initialize_ptr initializeCoreClr = (coreclr_initialize_ptr)GetProcAddress(coreClr, "coreclr_initialize"); 14 coreclr_create_delegate_ptr createManagedDelegate = (coreclr_create_delegate_ptr)GetProcAddress(coreClr, "coreclr_create_delegate"); 15 coreclr_shutdown_ptr shutdownCoreClr = (coreclr_shutdown_ptr)GetProcAddress(coreClr, "coreclr_shutdown"); 16 coreclr_execute_assembly_ptr executeAssembly = (coreclr_execute_assembly_ptr)GetProcAddress(coreClr, "coreclr_execute_assembly"); 17 std::string tpaList; 18 19 BuildTpaList(runtimePath, ".dll", tpaList); 20 const char* propertyKeys[] = { 21 "TRUSTED_PLATFORM_ASSEMBLIES" // Trusted assemblies 22 }; 23 24 const char* propertyValues[] = { 25 tpaList.c_str() 26 }; 27 28 void* hostHandle; 29 unsigned int domainId; 30 31 int hr = initializeCoreClr( 32 runtimePath, // App base path 33 "SampleHost", // AppDomain friendly name 34 sizeof(propertyKeys) / sizeof(char*), // Property count 35 propertyKeys, // Property names 36 propertyValues, // Property values 37 &hostHandle, // Host handle 38 &domainId); // AppDomain ID 39 40 doWork_ptr managedDelegate; 41 hr = createManagedDelegate( 42 hostHandle, 43 domainId, 44 "Main, Version=1.0.0.0", 45 "Main.Program", 46 "Main", 47 (void**)&managedDelegate); 48 49 ((MainMethodFp*)managedDelegate)(NULL); 50 double data[4]; 51 data[0] = 0; 52 data[1] = 0.25; 53 data[2] = 0.5; 54 data[3] = 0.75; 55 56 // Invoke the managed delegate and write the returned string to the console 57 //char* ret = managedDelegate("Test job", 5, sizeof(data) / sizeof(double), data, ReportProgressCallback); 58 59 //printf("Managed code returned: %s\n", ret); 60 61 //CoTaskMemFree(ret); 62 63 hr = shutdownCoreClr(hostHandle, domainId); 64 65 if (!FreeLibrary(coreClr)) 66 { 67 printf("Failed to free coreclr.dll\n"); 68 } 69 return 0; 70 } 71 72 73 void BuildTpaList(const char* directory, const char* extension, std::string& tpaList) 74 { 75 76 std::string searchPath(directory); 77 searchPath.append("\\"); 78 searchPath.append("*"); 79 searchPath.append(extension); 80 81 WIN32_FIND_DATAA findData; 82 HANDLE fileHandle = FindFirstFileA(searchPath.c_str(), &findData); 83 84 if (fileHandle != INVALID_HANDLE_VALUE) 85 { 86 do 87 { 88 tpaList.append(directory); 89 tpaList.append("\\"); 90 tpaList.append(findData.cFileName); 91 tpaList.append(";"); 92 93 } while (FindNextFileA(fileHandle, &findData)); 94 FindClose(fileHandle); 95 } 96 } 97 98 99 100 int ReportProgressCallback(int progress) 101 { 102 printf("Received status from managed code: %d\n", progress); 103 return -progress; 104 } Java/.Net讨论群:676817308
原文地址:https://www.cnblogs.com/tangyanzhi1111/p/10571966.html
时间: 2024-10-10 08:07:27