我玩了这个dotnet hosting example的代码,遇到了一些问题.
这就是我如何得到函数指针:
component_entry_point_fn function = nullptr;
int rc = functionPointer(
dllPath.c_str(),
dotnetType.c_str(),
methodName.c_str(),
nullptr,
nullptr,
(void**)&function);
然后我给它打了电话:
struct test {
const char* str = "from host!";
int i = 1;
} testArgs;
function(&testArgs, sizeof(testArgs));
而且它运行得很好.输出:
Hello, world! from Lib [count: 1]
-- message: from host!
-- number: 1
Hello again, world! from Lib [count: 2]
-- message: from host!
-- number: 1
但当我try 将参数作为元组传递时:
std::tuple<FunctionArgs...> argsTuple = std::make_tuple(args...);
function(&argsTuple, sizeof(argsTuple));
我知道了
Hello, world! from Lib [count: 1]
Fatal error. System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
at System.SpanHelpers.IndexOfNullByte(Byte*)
at System.Runtime.InteropServices.Marshal.PtrToStringUTF8(IntPtr)
at Scripts.Lib.PrintLibArgs(LibArgs)
at Scripts.Lib.HelloAgain(IntPtr, Int32)
C#端的代码与上面链接中的示例相同. Another important fact is that everything works without errors on Arm MacOS. The error appears on Linux x86_64. The compiler is the same everywhere: clang.
struct 和元组的sizeof是相同的—16字节. 已try 添加对齐方式:
alignas(16) std::tuple<FunctionArgs...>
argsTuple = std::make_tuple(args...);