【问题标题】:Call C# delegate with function pointer in Managed C++在托管 C++ 中使用函数指针调用 C# 委托
【发布时间】:2012-10-06 09:14:22
【问题描述】:

程序.cs

class Program
    {
        public static void He(string v)
        {
                Console.WriteLine(v);
                Console.WriteLine("End?");
            }
        public delegate void he(string v);

        static void Main(string[] args)
        {
            Class1 cls = new Class1();
            IntPtr p = Marshal.GetFunctionPointerForDelegate(new he(He));
            cls.call(p, "String");
        }
    }

TestClassLibrary.h

namespace TestClassLibrary 
{
    typedef void (Hello)(System::String ^ v);

    public ref class Class1
    {
        public:
        void call(IntPtr p,System::String ^v);
    };
}

TestClassLibrary.cpp

namespace TestClassLibrary 
{
    void Class1::call(IntPtr p,System::String ^ v)
    {
        Hello * h = (Hello *)p.ToPointer();
        h(v);
    }
}

我不知道这段代码有什么问题:(

(测试类库被编译成托管DLL。)

【问题讨论】:

  • 您遇到的错误是什么?
  • Reetrancy(?) 我不知道它是什么..?

标签: c# c++ .net windows managed


【解决方案1】:

托管 C++/CLI 具有委托 - 语法几乎与 C# 相同 - 为什么不使用它们,并且不涉及编组(因为无论如何您正在编译为托管 C++/CLI)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-28
    • 1970-01-01
    • 1970-01-01
    • 2012-12-29
    • 2012-01-30
    • 1970-01-01
    相关资源
    最近更新 更多