성태의 닷넷 이야기
홈 주인
모아 놓은 자료
프로그래밍
질문/답변
사용자 관리
사용자
메뉴
아티클
외부 아티클
유용한 코드
온라인 기능
MathJax 입력기
최근 덧글
[정성태] Working with Rust Libraries from C#...
[정성태] Detecting blocking calls using asyn...
[정성태] 아쉽게도, 커뮤니티는 아니고 개인 블로그입니다. ^^
[정성태] 질문이 잘 이해가 안 됩니다. 우선, 해당 소스코드에서 ILis...
[양승조
] var대신 dinamic으로 선언해서 해결은 했습니다. 맞는 해...
[양승조
] 또 막혔습니다. ㅠㅠ var list = props[i].Ge...
[양승조
] 아. 감사합니다. 어제는 안됐던것 같은데....정신을 차려야겠네...
[정성태] "props[i].GetValue(props[i])" 코드에서 ...
[정성태] 저렇게 조각 코드 말고, 실제로 재현이 되는 예제 프로젝트를 압...
[정성태] Modules 창(Ctrl+Shift+U)을 띄워서, 해당 Op...
글쓰기
제목
이름
암호
전자우편
HTML
홈페이지
유형
제니퍼 .NET
닷넷
COM 개체 관련
스크립트
VC++
VS.NET IDE
Windows
Team Foundation Server
디버깅 기술
오류 유형
개발 환경 구성
웹
기타
Linux
Java
DDK
Math
Phone
Graphics
사물인터넷
부모글 보이기/감추기
내용
<div style='display: inline'> <h1 style='font-family: Malgun Gothic, Consolas; font-size: 20pt; color: #006699; text-align: center; font-weight: bold'>.NET Core를 직접 호스팅해 (runtimeconfig.json 없이) EXE만 배포해 실행</h1> <p> (이번 주제는, self-contained 방식이 아닌 framework-dependent인 경우 적용할 수 있습니다.)<br /> <br /> 닷넷 코어에서 runtimeconfig.json을 없앨 수 있을까요?<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > 닷넷코어 빌드 시 runtimeconfig 파일을 없앨 수 있는지.. ; <a target='tab' href='https://www.sysnet.pe.kr/3/0/5352'>https://www.sysnet.pe.kr/3/0/5352</a> </pre> <br /> 예전에 유사하게 닷넷 프레임워크에 대해서도 이런 처리를 했었는데요,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > .NET EXE 파일을 닷넷 프레임워크 버전에 상관없이 실행할 수 있을까요? - 두 번째 이야기 ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/1746'>https://www.sysnet.pe.kr/2/0/1746</a> </pre> <br /> 마찬가지로 .NET Core를 직접 호스팅하면 runtimeconfig.json을 배포하지 않고도 (정확히는 실행 파일에 포함시켜 배포 후 런타임에 풀어내는 식으로) 실행할 수 있습니다. 이를 위해 호스팅 관련 공식 문서들을 보면,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > Native hosting ; <a target='tab' href='https://github.com/dotnet/runtime/blob/master/docs/design/features/native-hosting.md'>https://github.com/dotnet/runtime/blob/master/docs/design/features/native-hosting.md</a> Sample .NET Core Hosts ; <a target='tab' href='https://github.com/dotnet/samples/tree/master/core/hosting'>https://github.com/dotnet/samples/tree/master/core/hosting</a> Write a custom .NET Core host to control the .NET runtime from your native code ; <a target='tab' href='https://learn.microsoft.com/en-us/dotnet/core/tutorials/netcore-hosting'>https://learn.microsoft.com/en-us/dotnet/core/tutorials/netcore-hosting</a> </pre> <br /> github에 예제가 2가지로 나뉘는데,<br /> <br /> <ul> <li><a target='tab' href='https://github.com/dotnet/samples/tree/master/core/hosting/HostWithCoreClrHost'>HostWithHostFxr</a> - .NET Core 3.0에서 도입된 nethost/hostfxr API를 이용(.NET 3.0 이후의 호스팅 용도로 권장)</li> <li><a target='tab' href='https://github.com/dotnet/samples/tree/master/core/hosting/HostWithHostFxr'>HostWithCoreClrHost</a> - coreclrhost.h에서 제공하는 API를 이용(기존 .NET Core 2.2 이하의 호스팅에서 선호)/li> </ul> <br /> 이 글에서는 HostWithHostFxr 방식으로 실습해 보겠습니다.<br /> <br /> <hr style='width: 50%' /><br /> <br /> .NET Core/5 런타임을 호스팅하기 위한 첫 번째 작업은 hostfxr.dll을 찾는 것입니다. 왜냐하면 그 DLL 내에 런타임 호스팅을 위한 API들이 정의되어 있기 때문입니다. 그리고 그 hostfx.dll을 찾는 작업을 nethost에서 담당하는데 이미 nethost를 다루는 것에 대해서는 다음의 글에서 설명했습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > Visual C++ - .NET Core의 nethost.lib와 정적 링크 ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/12419'>https://www.sysnet.pe.kr/2/0/12419</a> </pre> <br /> 일단 위의 단계까지 마무리 된 걸로 가정해 다음의 소스 코드에서 시작해 보겠습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > struct get_hostfxr_parameters { size_t size; const wchar_t* assembly_path; const wchar_t* dotnet_root; }; extern "C" int __stdcall get_hostfxr_path( wchar_t* buffer, size_t * buffer_size, const struct get_hostfxr_parameters* parameters); #include <iostream> #include <Windows.h> #include <shlwapi.h> #include <assert.h> #include <fstream> #include <string> #pragma comment(lib, "C:\\Program Files\\dotnet\\packs\\Microsoft.NETCore.App.Host.win-x64\\5.0.0\\runtimes\\win-x64\\native\\libnethost.lib") int main() { wchar_t hostfxr_path[MAX_PATH]; { size_t buffer_size = sizeof(hostfxr_path) / sizeof(wchar_t); int rc = get_hostfxr_path(hostfxr_path, &buffer_size, nullptr); // buffer == C:\Program Files\dotnet\host\fxr\5.0.0\hostfxr.dll } return 0; } </pre> <br /> 이후 순서는 문서에 따라 단계별로 하시면 됩니다. ^^<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > Step 1 - Load hostfxr and get exported hosting functions ; <a target='tab' href='https://learn.microsoft.com/en-us/dotnet/core/tutorials/netcore-hosting#step-1---load-hostfxr-and-get-exported-hosting-functions'>https://learn.microsoft.com/en-us/dotnet/core/tutorials/netcore-hosting#step-1---load-hostfxr-and-get-exported-hosting-functions</a> Step 2 - Initialize and start the .NET Core runtime ; <a target='tab' href='https://learn.microsoft.com/en-us/dotnet/core/tutorials/netcore-hosting#step-2---initialize-and-start-the-net-core-runtime'>https://learn.microsoft.com/en-us/dotnet/core/tutorials/netcore-hosting#step-2---initialize-and-start-the-net-core-runtime</a> Step 3 - Load managed assembly and get function pointer to a managed method ; <a target='tab' href='https://learn.microsoft.com/en-us/dotnet/core/tutorials/netcore-hosting#step-3---load-managed-assembly-and-get-function-pointer-to-a-managed-method'>https://learn.microsoft.com/en-us/dotnet/core/tutorials/netcore-hosting#step-3---load-managed-assembly-and-get-function-pointer-to-a-managed-method</a> Step 4 - Run managed code! ; <a target='tab' href='https://learn.microsoft.com/en-us/dotnet/core/tutorials/netcore-hosting#step-4---run-managed-code'>https://learn.microsoft.com/en-us/dotnet/core/tutorials/netcore-hosting#step-4---run-managed-code</a> </pre> <br /> 따라서, hostfxr.dll로부터 호스팅 관련 API들을 가져온 후,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > ...[생략]... #pragma comment(lib, "shlwapi.lib") #define NETHOST_CALLTYPE __stdcall enum hostfxr_delegate_type { hdt_com_activation, hdt_load_in_memory_assembly, hdt_winrt_activation, hdt_com_register, hdt_com_unregister, hdt_load_assembly_and_get_function_pointer }; typedef struct hostfxr_initialize_parameters { size_t size; const wchar_t* host_path; const wchar_t* dotnet_root; } hostfxr_initialize_parameters; typedef void* hostfxr_handle; typedef int32_t(NETHOST_CALLTYPE* hostfxr_initialize_for_runtime_config_fn)( const wchar_t* runtime_config_path, const hostfxr_initialize_parameters* parameters, /*out*/ hostfxr_handle* host_context_handle); typedef int32_t(NETHOST_CALLTYPE* hostfxr_get_runtime_delegate_fn)( const hostfxr_handle host_context_handle, enum hostfxr_delegate_type type, /*out*/ void** delegate); typedef int32_t(NETHOST_CALLTYPE* hostfxr_close_fn)(const hostfxr_handle host_context_handle); static hostfxr_initialize_for_runtime_config_fn init_fptr; static hostfxr_get_runtime_delegate_fn get_delegate_fptr; static hostfxr_close_fn close_fptr; #pragma comment(lib, "C:\\Program Files\\dotnet\\packs\\Microsoft.NETCore.App.Host.win-x64\\5.0.0\\runtimes\\win-x64\\native\\libnethost.lib") static void* load_library(const wchar_t* path) { assert(path != NULL); HMODULE h = LoadLibraryW(path); assert(h != NULL); return (void*)h; } static void* get_export(void* h, const char* name) { assert(h != NULL && name != NULL); void* f = GetProcAddress((HMODULE)h, name); assert(f != NULL); return f; } int main() { { // ...[생략]... void* lib = load_library(hostfxr_path); <span style='color: blue; font-weight: bold'>init_fptr = (hostfxr_initialize_for_runtime_config_fn)get_export(lib, "hostfxr_initialize_for_runtime_config"); get_delegate_fptr = (hostfxr_get_runtime_delegate_fn)get_export(lib, "hostfxr_get_runtime_delegate"); close_fptr = (hostfxr_close_fn)get_export(lib, "hostfxr_close");</span> } return 0; } </pre> <br /> 런타임 호스팅을 시작하고 끝내는 것을 다음과 같이 할 수 있습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > #define NETHOST_SUCCESS 0 static bool is_failure(int rc) { // The CLR hosting API uses the Win32 HRESULT scheme. This means // the high order bit indicates an error and S_FALSE (1) can be returned // and is _not_ a failure. return (rc < NETHOST_SUCCESS); } // ...[생략]... int main() { // ...[생략]... hostfxr_handle cxt = nullptr; int rc = 0; wchar_t <span style='color: blue; font-weight: bold'>config_path</span>[MAX_PATH] = L"...[path_to_runtimeconfig.json]..."; do { rc = <span style='color: blue; font-weight: bold'>init_fptr(config_path, nullptr, &cxt);</span> if (is_failure(rc) || cxt == nullptr) { break; } } while (false); if (cxt != nullptr) { <span style='color: blue; font-weight: bold'>close_fptr(cxt);</span> } } </pre> <br /> 그리고, 바로 저 init_fptr의 첫 번째 인자에 들어가는 것이 "runtimeconfig.json"의 경로입니다. 그럼, 간단하게 저 파일을 런타임 시에 만들어도 상관없는 것입니다. 원칙적으로는 get_hostfxr_path에서 구한 .NET Core 런타임의 버전을 설정하는 것이 맞겠지만 여기서는 소스 코드의 간결함을 위해 그냥 5.0.0으로 고정시켜 진행합니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > // ...[생략]... void write_and_get_config_path(wchar_t* config_path, size_t config_path_size, wchar_t* hostfxr_path) { std::string txt = "{\n" " \"runtimeOptions\": {\n" " \"tfm\": \"net5.0\",\n" " \"framework\" : {\n" " \"name\": \"Microsoft.NETCore.App\",\n" " \"version\" : \"5.0.0\"\n" " }\n" " }\n" "}\n"; HMODULE hModule = ::GetModuleHandle(NULL); ::GetModuleFileName(hModule, config_path, (DWORD)config_path_size); ::PathCchRemoveFileSpec(config_path, config_path_size); ::PathCchAppendEx(config_path, config_path_size, L"SampleHostApp.runtimeconfig.json", 0); std::ofstream ofs; ofs.open(config_path); ofs << txt; ofs.close(); } int main() { // ...[생략]... wchar_t config_path[MAX_PATH]; { size_t config_path_size = sizeof(buffer) / sizeof(wchar_t); write_and_get_config_path(config_path, config_path_size, buffer); } // ...[생략]... } </pre> <br /> 이걸로 일단, runtimeconfig.json은 해결했고 그다음 우리가 만든 .NET DLL에서 시작점을 지정해 함수 포인터를 얻는 작업을 해야 합니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > int main() { // ...[생략]... const wchar_t* dotnet_type = L"SampleHostApp.Program, SampleHostApp"; const wchar_t* dotnet_type_method = L"Main"; const wchar_t* dotnet_delegate_type = L"SampleHostApp.MainDelegate, SampleHostApp"; void* func = nullptr; rc = get_managed_export_fptr( <span style='color: blue; font-weight: bold'>assembly_path</span>, dotnet_type, dotnet_type_method, dotnet_delegate_type, nullptr, &func); if (is_failure(rc) || func == nullptr) { break; } // ...[생략]... } </pre> <br /> 여기서 예제로 사용하는 SampleHostApp.dll은 .NET Core 3.1 프로젝트로 아래의 예제 코드를 빌드한 것입니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > using System; namespace <span style='color: blue; font-weight: bold'>SampleHostApp</span> { class <span style='color: blue; font-weight: bold'>Program</span> { <span style='color: blue; font-weight: bold'>static void Main(string[] args)</span> { Console.WriteLine("[NETCORE] Hello World!"); Console.WriteLine("# of args: " + args?.Length); foreach (string arg in args ?? Array.Empty<string>()) { Console.WriteLine(arg); } } } <span style='color: blue; font-weight: bold'>public delegate void MainDelegate(string[] args);</span> } </pre> <br /> 또한 <a target='tab' href='https://www.sysnet.pe.kr/2/0/1746#inc_res'>지난번과 마찬가지로 이들은 호스팅을 담당하는 C/C++ EXE 내에 리소스로 포함</a>할 것이므로 런타임 시에 다음과 같은 코드로 풀어내야 합니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > // ...[생략]... void write_and_get_assembly_path(wchar_t* assembly_path, size_t asm_path_size) { HMODULE hModule = ::GetModuleHandle(NULL); ::GetModuleFileName(hModule, assembly_path, asm_path_size); ::PathCchRemoveFileSpec(assembly_path, asm_path_size); ::PathCchAppendEx(assembly_path, asm_path_size, L"SampleHostApp.dll", 0); <span style='color: blue; font-weight: bold'>HRSRC hResource = FindResource(hModule, MAKEINTRESOURCE(IDR_DLLFILE1), L"DLLFILE"); HGLOBAL hMemory = LoadResource(hModule, hResource); DWORD dwSize = SizeofResource(hModule, hResource); LPVOID lpAddress = LockResource(hMemory); std::ofstream ofs; ofs.open(assembly_path, std::ios::binary);</span> ofs.write((char*)lpAddress, dwSize); ofs.close(); } int main() { // ...[생략]... wchar_t assembly_path[MAX_PATH]; { size_t asm_path_size = sizeof(assembly_path) / sizeof(wchar_t); write_and_get_assembly_path(assembly_path, asm_path_size); } // ...[생략]... } </pre> <br /> 자, 그럼 이걸로 끝이군요, 함수 포인터도 잘 구했으니 이제 다음과 같은 식으로 호출만 하면 됩니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > typedef void (*main_ptr)(intptr_t); main_ptr mainFunc = (main_ptr)func; mainFunc(0); </pre> <br /> <hr style='width: 50%' /><br /> <br /> 그런데, 여기서 한 가지 문제가 있습니다. 바로 닷넷의 Main 메서드에 전달할 "string [] args" 인자 값을 C/C++에서 딱히 어떻게 구성해야 하는 모르겠습니다. (혹시, 아시는 분은 덧글 부탁드립니다.)<br /> <br /> 그래서 위의 경우처럼 실행하면 Main 메서드의 string [] args에 null이 전달되어 다음과 같은 식의 실행 결과를 얻게 됩니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > E:\DotNetSamples\x64\Debug> <span style='color: blue; font-weight: bold'>netcore_host.exe test</span> [NETCORE] Hello World! # of args: </pre> <br /> 하지만, 이렇게 Main 메서드 만을 부르는 식이라면 더 간단하게 호스팅을 처리하는 방법이 hostfx.dll에 있습니다. 바로 hostfxr_main 함수인데, 이것을 사용하면 별다른 호스팅 코딩 없이 모두 자동화되어 처리가 됩니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > int main() { wchar_t hostfxr_path[MAX_PATH]; { size_t buffer_size = sizeof(hostfxr_path) / sizeof(wchar_t); int rc = get_hostfxr_path(hostfxr_path, &buffer_size, nullptr); if (is_failure(rc)) noreturn_runtime_load_failure(rc); void* lib = load_library(hostfxr_path); hostfxr_main_fptr = (hostfxr_main_fn)get_export(lib, "hostfxr_main"); } wchar_t config_path[MAX_PATH]; { size_t config_path_size = sizeof(config_path) / sizeof(wchar_t); write_and_get_config_path(config_path, config_path_size, hostfxr_path); } wchar_t assembly_path[MAX_PATH]; { size_t asm_path_size = sizeof(assembly_path) / sizeof(wchar_t); write_and_get_assembly_path(assembly_path, asm_path_size); } int nArgs = 0; LPWSTR* szArglist = CommandLineToArgvW(GetCommandLineW(), &nArgs); <span style='color: blue; font-weight: bold'>PCWSTR* argv = new PCWSTR[2 + ((short)nArgs) - 1]; argv[0] = config_path; // 첫 번째 요소에 runtimeconfig.json 파일 경로 argv[1] = assembly_path; // 두 번째 요소에 .NET DLL 경로 for (int i = 0; i < nArgs - 1; i++) { argv[2 + i] = szArglist[i + 1]; // 세 번째 이후는 명령행에 전달된 인자 } hostfxr_main_fptr(2 + (nArgs - 1), argv); // 내부에서 자동으로 호스팅도 시작하고 Main 메서드도 호출</span> delete[] argv; LocalFree(szArglist); if (cxt != nullptr) { close_fptr(cxt); } return 0; } </pre> <br /> <hr style='width: 50%' /><br /> <br /> 참고로, 위의 예에서 get_managed_export_fptr를 이용한 호출 방식은 내부에 반드시 델리게이트 타입이 정의되어 있어야 합니다. 예제의 경우 그래서 MainDelegate 타입을 포함했던 것입니다.<br /> <br /> 마찬가지로 <a target='tab' href='https://learn.microsoft.com/en-us/dotnet/core/tutorials/netcore-hosting'>닷넷 호스팅 문서</a>의 예제에서는,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > HostWithHostFxr ; <a target='tab' href='https://github.com/dotnet/samples/tree/master/core/hosting/HostWithHostFxr'>https://github.com/dotnet/samples/tree/master/core/hosting/HostWithHostFxr</a> </pre> <br /> ComponentEntryPoint라는 델리게이트를 정의해 놓고 그 양식에 맞는 닷넷 메서드를 호출하고 있습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > public delegate int ComponentEntryPoint(IntPtr args, int sizeBytes); </pre> <br /> 재미있는 것은, .NET 5에서의 함수 포인터가 적용되면서 델리게이트를 생략할 수 있게 되었다는 점입니다. 이에 대한 예제는 <a target='tab' href='https://www.sysnet.pe.kr/2/0/12421'>DNNE</a>에서 제공하는 platform.c의 get_callable_managed_function 코드에서 확인할 수 있습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > // ...[생략]... <span style='color: blue; font-weight: bold'>#define UNMANAGEDCALLERSONLY_METHOD ((const char_t*)-1)</span> // ...[생략]... void* get_callable_managed_function( const char_t* dotnet_type, const char_t* dotnet_type_method, <span style='color: blue; font-weight: bold'>const char_t* dotnet_delegate_type</span>) { assert(dotnet_type && dotnet_type_method); // Check if the runtime has already been prepared. if (!get_managed_export_fptr) { prepare_runtime(); assert(get_managed_export_fptr != NULL); } char_t buffer[DNNE_MAX_PATH]; const char_t assembly_filename[] = DNNE_STR(DNNE_TOSTRING(DNNE_ASSEMBLY_NAME)) DNNE_STR(".dll"); const char_t* assembly_path = NULL; int rc = get_current_dir_filepath(DNNE_ARRAY_SIZE(buffer), buffer, DNNE_ARRAY_SIZE(assembly_filename), assembly_filename, &assembly_path); if (is_failure(rc)) noreturn_export_load_failure(rc); // Function pointer to managed function void* func = NULL; rc = get_managed_export_fptr( assembly_path, dotnet_type, dotnet_type_method, <span style='color: blue; font-weight: bold'>dotnet_delegate_type</span>, NULL, &func); if (is_failure(rc)) noreturn_export_load_failure(rc); return func; } void* get_fast_callable_managed_function( const char_t* dotnet_type, const char_t* dotnet_type_method) { return get_callable_managed_function(dotnet_type, dotnet_type_method, <span style='color: blue; font-weight: bold'>UNMANAGEDCALLERSONLY_METHOD</span>); // return get_callable_managed_function(dotnet_type, dotnet_type_method, 0x05); } </pre> <br /> 즉, 닷넷 모듈에 정의한 메서드 중 UnmanagedCallersOnly 특성이 부여된 경우라면 그 메서드를 호출할 때는 델리게이트 타입을 명시할 필요 없이 -1 값을 전달하면 되는 것입니다.<br /> <br /> 이 글의 예제 코드는 github에 올려 두었습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > DotNetSamples/Cpp/netcore_host ; <a target='tab' href='https://github.com/stjeong/DotNetSamples/tree/master/Cpp/netcore_host'>https://github.com/stjeong/DotNetSamples/tree/master/Cpp/netcore_host</a> </pre> <br /> 개선해야 할 점이 있다면, 리눅스를 지원하지 않아 윈도우에서만 실행이 됩니다. (원하시는 분은 PR 넣으셔도 됩니다. ^^)<br /> <br /> <hr style='width: 50%' /><br /> <br /> 만약 nethost에 대한 의존성이 싫다면, 사실 그냥 만들어도 됩니다. 중요한 것은, "C:\Program Files\dotnet\host\fxr\5.0.0\hostfxr.dll" 경로를 찾는 것이기 때문에 윈도우라면 레지스트리 또는 %ProgramFiles%에서 검색하면 (100%는 아니겠지만) 대부분의 경우 해결이 됩니다. 물론, nethost 만큼 다양한 방식으로 검색해 반환하려면 꽤나 코드가 들어갈 텐데요, 원한다면 nethost 관련 소스 코드는 다음의 경로에서 찾을 수 있습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > runtime/src/installer/corehost/cli/nethost/CMakeLists.txt ; <a target='tab' href='https://github.com/dotnet/runtime/blob/master/src/installer/corehost/cli/nethost/CMakeLists.txt'>https://github.com/dotnet/runtime/blob/master/src/installer/corehost/cli/nethost/CMakeLists.txt</a> runtime/src/installer/corehost/cli/nethost/nethost.cpp ; <a target='tab' href='https://github.com/dotnet/runtime/blob/master/src/installer/corehost/cli/nethost/nethost.cpp'>https://github.com/dotnet/runtime/blob/master/src/installer/corehost/cli/nethost/nethost.cpp</a> runtime/src/installer/corehost/cli/fxr_resolver.cpp ; <a target='tab' href='https://github.com/dotnet/runtime/blob/master/src/installer/corehost/cli/fxr_resolver.cpp'>https://github.com/dotnet/runtime/blob/master/src/installer/corehost/cli/fxr_resolver.cpp</a> core-setup/src/corehost/common/pal.windows.cpp ; <a target='tab' href='https://github.com/dotnet/core-setup/blob/master/src/corehost/common/pal.windows.cpp'>https://github.com/dotnet/core-setup/blob/master/src/corehost/common/pal.windows.cpp</a> runtime/src/installer/corehost/cli/apphost/static/hostfxr_resolver.cpp ; <a target='tab' href='https://github.com/dotnet/runtime/blob/master/src/installer/corehost/cli/apphost/static/hostfxr_resolver.cpp'>https://github.com/dotnet/runtime/blob/master/src/installer/corehost/cli/apphost/static/hostfxr_resolver.cpp</a> </pre> <br /> 실제로 위의 코드를 보면, nethost에서 %ProgramFiles% 또는 %ProgramFiles(x86)%의 경로에 "host" + "fxr" 문자열의 하위 경로 2개가 존재하면 그 이하 폴더를 열거하는 식의 코드를 볼 수 있습니다.<br /> <br /> 혹은, nethost를 비롯해 닷넷 관련 모듈은 "<a target='tab' href='https://www.sysnet.pe.kr/2/0/11243#corehost_trace'>COREHOST_TRACE=1</a>" 환경 변수로 인한 로그를 꽤나 자세하게 남기므로 그 로그를 통해 nethost의 동작을 유추하는 것도 가능합니다. (첨부 파일은 제 컴퓨터에서 실행한 예제 코드의 trace 로그입니다.)<br /> <br /> 그나저나, 정리해 놓고 나니 한 가지 아쉬움이 남는군요. ^^ 닷넷 프레임워크의 경우 DLL을 메모리로부터 로드하는 AppDomain.Load 버전을 제공하므로 정말로 한 개의 모듈로 실행하는 것이 가능했습니다. 반면, 닷넷 코어는 runtimeconfig.json과 닷넷 DLL에 대한 경로를 필요로 하므로 사용 전 반드시 파일에 써야 하는 단점이 있습니다.<br /> </p><br /> <br /><hr /><span style='color: Maroon'>[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]</span> </div>
첨부파일
스팸 방지용 인증 번호
1607
(왼쪽의 숫자를 입력해야 합니다.)