35
loading...
This website collects cookies to deliver better user experience
public static int Hello(IntPtr arg, int argLength)
{
if (argLength < System.Runtime.InteropServices.Marshal.SizeOf(typeof(LibArgs)))
{
return 1;
}
LibArgs libArgs = Marshal.PtrToStructure<LibArgs>(arg);
Console.WriteLine($"Hello, world! from {nameof(Lib)} [count: {s_CallCount++}]");
PrintLibArgs(libArgs);
return 0;
}
LibArgs libArgs = Marshal.PtrToStructure<LibArgs>(arg);
[StructLayout(LayoutKind.Sequential)]
public struct LibArgs
{
public IntPtr Message;
public int Number;
}
[StructLayout(LayoutKind.Sequential)]
Marshal.PtrToStringUni(libArgs.Message)
bool load_hostfxr()
{
// Pre-allocate a large buffer for the path to hostfxr
char_t buffer[MAX_PATH];
size_t buffer_size = sizeof(buffer) / sizeof(char_t);
int rc = get_hostfxr_path(buffer, &buffer_size, nullptr);
if (rc != 0)
return false;
// Load hostfxr and get desired exports
void *lib = load_library(buffer);
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");
return (init_fptr && get_delegate_fptr && close_fptr);
}
const string_t config_path = root_path + STR("DotNetLib.runtimeconfig.json");
load_assembly_and_get_function_pointer_fn load_assembly_and_get_function_pointer = nullptr;
load_assembly_and_get_function_pointer = get_dotnet_load_assembly(config_path.c_str());
assert(load_assembly_and_get_function_pointer != nullptr && "Failure: get_dotnet_load_assembly()");
const string_t dotnetlib_path = root_path + STR("DotNetLib.dll");
const char_t *dotnet_type = STR("DotNetLib.Lib, DotNetLib");
const char_t *dotnet_type_method = STR("Hello");
// <SnippetLoadAndGet>
// Function pointer to managed delegate
component_entry_point_fn hello = nullptr;
int rc = load_assembly_and_get_function_pointer(
dotnetlib_path.c_str(),
dotnet_type,
dotnet_type_method,
nullptr /*delegate_type_name*/,
nullptr,
(void**)&hello);
// </SnippetLoadAndGet>
assert(rc == 0 && hello != nullptr && "Failure: load_assembly_and_get_function_pointer()");
for (int i = 0; i < 3; ++i)
{
// <SnippetCallManaged>
lib_args args
{
STR("from host!"),
i
};
hello(&args, sizeof(args));
// </SnippetCallManaged>
}