English 中文(简体)
获得代表的IntPt并援引
原标题:Getting IntPtr of a delegate and invoking

要模拟 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 (), 然后请回代表 。

谢谢

最佳回答

您无法使用 GetDelegate for FunctionPointer () 来获得管理功能的代表。 这是来自 < a href=" http://msdn. microsoft.com/query/ dev10. query? appId=Dev10IDEF1 & amp; l=EN- US- amp;k=k% 28SYSTEM. RTUNIME. INTEROPSERPES. MARSHAL. GETDLEGATIONFORFUPONPONPONTIONTLATIONJCT% 29; k% 28TorgetFramwork Moniker-% 22. NETFRAMEWORK% 2cV2. 022% 29; k% 28DevLang- CSHARP% 29&rd=ret "Nofol=" MSDN : :

You cannot pass an invalid function pointer to GetDelegateForFunctionPointer. In addition, you can only use this method for pure unmanaged function pointers. You cannot use this method with function pointers obtained through C++ or from GetFunctionPointer. You cannot use this method to create a delegate from a function pointer to another managed delegate.

我不知道为什么你不能这样做,但我猜这是因为微软通用语言运行时间使用快速呼叫呼叫协议,但PInvoke不支持“fastCall” FastCall

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签