要模拟 C+++ 的呼叫, 我尝试以下代码
private delegate void CppFuncDelegate(string message);
private static void Main(string[] args)
{
Console.WriteLine("+++ BEGIN TEST +++");
Action<string> action = str => Console.WriteLine("Received:" + str);
IntPtr delegatePtr = action.Method.MethodHandle.GetFunctionPointer();
CppFuncDelegate cppFuncDelegate = (CppFuncDelegate)
Marshal.GetDelegateForFunctionPointer(delegatePtr,
typeof(CppFuncDelegate));
cppFuncDelegate.Invoke("Hello");
}
但我得到
PInvokeStackImbalance was detected. A call to PInvoke function Test!Test.Program+CppFuncDelegate::Invoke has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.
谁能告诉我 我做错了什么?
NOTE :请不要告诉我要采取行动。 Invoke (); 这与这项练习无关。 我想让 IntPtr 处理代表事务, 并使用 GetDelegate ForFunctionPointer (), 然后请回代表 。
谢谢