爲了模擬從C++調用,我想下面的代碼獲得委託的IntPtr的和調用
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。 對PInvoke函數'Test!Test.Program + CppFuncDelegate :: Invoke' 的調用已使堆棧不平衡。這很可能是因爲託管PInvoke 簽名與非託管目標籤名不匹配。檢查 PInvoke簽名的調用約定和參數是否匹配 目標非託管簽名。
誰能告訴我什麼,我做錯了什麼?
注意:請不要告訴我做action.Invoke();這不是這個練習的重點。我想讓IntPtr處理委託並使用GetDelegateForFunctionPointer(),然後調用返回的委託。
謝謝。
您是否檢查過PInvoke簽名的調用約定是否與目標非受控簽名匹配? – dtb
@dtb'CppFuncDelegate'接受一個字符串並返回void,並且我將action的委託聲明爲'Action';所以我認爲他們匹配。有沒有其他方法來檢查PInvoke簽名? –
我不想談論參數,我正在談論調用約定。 – dtb