Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make hosting sample always show using UnmanagedCallersOnly #6139

Merged
merged 1 commit into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading