Skip to content

Commit

Permalink
Make hosting sample always show using UnmanagedCallersOnly (#6139)
Browse files Browse the repository at this point in the history
  • Loading branch information
elinor-fung authored Sep 5, 2023
1 parent 6ed5ce7 commit 3a75e78
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
5 changes: 4 additions & 1 deletion core/hosting/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -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.
Expand Down
7 changes: 3 additions & 4 deletions core/hosting/src/DotNetLib/Lib.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
24 changes: 11 additions & 13 deletions core/hosting/src/NativeHost/nativehost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,22 +128,27 @@ int main(int argc, char *argv[])
// </SnippetCallManaged>
}

#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,
Expand All @@ -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;
Expand Down

0 comments on commit 3a75e78

Please sign in to comment.