From 3a75e7848201dc3ca56f7d7015686065775e1464 Mon Sep 17 00:00:00 2001 From: Elinor Fung Date: Tue, 5 Sep 2023 13:09:12 -0700 Subject: [PATCH] Make hosting sample always show using UnmanagedCallersOnly (#6139) --- core/hosting/readme.md | 5 ++++- core/hosting/src/DotNetLib/Lib.cs | 7 +++---- core/hosting/src/NativeHost/nativehost.cpp | 24 ++++++++++------------ 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/core/hosting/readme.md b/core/hosting/readme.md index 3b2dfea23fa..e89d4f43860 100644 --- a/core/hosting/readme.md +++ b/core/hosting/readme.md @@ -19,7 +19,7 @@ Additional comments are contained in source and project files. ## Prerequisites -* [.NET Core 3.1 SDK](https://dotnet.microsoft.com/download) or a later version +* [.NET Core 6.0 SDK](https://dotnet.microsoft.com/download) or a later version * C++ compiler * Windows: `cl.exe` @@ -57,6 +57,9 @@ Hello, world! from Lib [count: 3] Hello, world! from CustomEntryPoint in Lib -- message: from host! -- number: -1 +Hello, world! from CustomEntryPointUnmanagedCallersOnly in Lib +-- message: from host! +-- number: -1 ``` Note: The way the sample is built is relatively complicated. The goal is that it's possible to build and run the sample with simple `dotnet run` with minimal requirements on pre-installed tools. Typically, real-world projects that have both managed and native components will use different build systems for each; for example, msbuild/dotnet for managed and CMake for native. diff --git a/core/hosting/src/DotNetLib/Lib.cs b/core/hosting/src/DotNetLib/Lib.cs index 60d68d1e747..4159063b543 100644 --- a/core/hosting/src/DotNetLib/Lib.cs +++ b/core/hosting/src/DotNetLib/Lib.cs @@ -34,13 +34,12 @@ public static void CustomEntryPoint(LibArgs libArgs) PrintLibArgs(libArgs); } -#if NET5_0 [UnmanagedCallersOnly] - public static void CustomEntryPointUnmanaged(LibArgs libArgs) + public static void CustomEntryPointUnmanagedCallersOnly(LibArgs libArgs) { - CustomEntryPoint(libArgs); + Console.WriteLine($"Hello, world! from {nameof(CustomEntryPointUnmanagedCallersOnly)} in {nameof(Lib)}"); + PrintLibArgs(libArgs); } -#endif private static void PrintLibArgs(LibArgs libArgs) { diff --git a/core/hosting/src/NativeHost/nativehost.cpp b/core/hosting/src/NativeHost/nativehost.cpp index 6dffe1a4b82..7e5d67d3ae3 100644 --- a/core/hosting/src/NativeHost/nativehost.cpp +++ b/core/hosting/src/NativeHost/nativehost.cpp @@ -128,22 +128,27 @@ int main(int argc, char *argv[]) // } -#ifdef NET5_0 // Function pointer to managed delegate with non-default signature typedef void (CORECLR_DELEGATE_CALLTYPE *custom_entry_point_fn)(lib_args args); custom_entry_point_fn custom = nullptr; + lib_args args + { + STR("from host!"), + -1 + }; + + // UnmanagedCallersOnly rc = load_assembly_and_get_function_pointer( dotnetlib_path.c_str(), dotnet_type, - STR("CustomEntryPointUnmanaged") /*method_name*/, + STR("CustomEntryPointUnmanagedCallersOnly") /*method_name*/, UNMANAGEDCALLERSONLY_METHOD, nullptr, (void**)&custom); assert(rc == 0 && custom != nullptr && "Failure: load_assembly_and_get_function_pointer()"); -#else - // Function pointer to managed delegate with non-default signature - typedef void (CORECLR_DELEGATE_CALLTYPE *custom_entry_point_fn)(lib_args args); - custom_entry_point_fn custom = nullptr; + custom(args); + + // Custom delegate type rc = load_assembly_and_get_function_pointer( dotnetlib_path.c_str(), dotnet_type, @@ -152,13 +157,6 @@ int main(int argc, char *argv[]) nullptr, (void**)&custom); assert(rc == 0 && custom != nullptr && "Failure: load_assembly_and_get_function_pointer()"); -#endif - - lib_args args - { - STR("from host!"), - -1 - }; custom(args); return EXIT_SUCCESS;