简体   繁体   English

使用 SWIG 从 C++ 回调到 C#

[英]Callback from C++ to C# using SWIG

I have an application running in C#.Netcore and C++ windows application.我有一个在 C#.Netcore 和 C++ windows 应用程序中运行的应用程序。
I achieved interoperability between C# & C++ using SWIG.我使用 SWIG 实现了 C# 和 C++ 之间的互操作性。

But I am not able to achieve Callback functionality from C++ to C#.但是我无法实现从 C++ 到 C# 的回调功能。 Also I tried by passing function pointer from C# to C++.我也尝试通过将函数指针从 C# 传递给 C++。 But it also failed但它也失败了

My Intention is to achieve callback by我的意图是通过以下方式实现回调

  1. By passing a C# function pointer to C++ and call that function pointer when needed so that C# function will be executed.通过将 C# 函数指针传递给 C++ 并在需要时调用该函数指针,以便执行 C# 函数。

  2. Creating a base class with virtual function in C++ and derive a class in C# which implement the virtual method.在 C++ 中创建一个带有虚函数的基类,并在 C# 中派生一个实现虚方法的类。 The set the object of derived to C++ so that I can call the object->VirtualMethod will invoke C# function.将派生对象设置为 C++,以便我可以调用对象->VirtualMethod将调用 C# 函数。

But both methods failed.但是这两种方法都失败了。

''' C++ Code ''' C++ 代码

    #pragma once
    #include <string>
    #include "TestDataClass.h"

    using namespace std;

    class EventHandlerBase
    {
    public:

        virtual void handle() = 0;

    };

    class TestClass
    {
    public:
        TestClass();
        ~TestClass();

        TestDataClass oData;
        TestDataClass* pData;

        int times2(int arg, string data);
        void SetData(TestDataClass data);
        void SetPointerData(TestDataClass* data);
        TestDataClass* GetPointerData();
        TestDataClass GetData();
        void Print();



        EventHandlerBase* EventObject;
        void SetEventObj(EventHandlerBase* data);


    };

''' '''

''' C++ Implementation ''' C++ 实现

#include "TestClass.h"
#include <iostream>

TestClass::TestClass()
{

}

TestClass::~TestClass()
{
}

int TestClass::times2(int arg, string data)
{
    return arg * 2;
}

void TestClass::SetData(TestDataClass data)
{
    this->oData = data;
}

void TestClass::SetPointerData(TestDataClass* data)
{
    this->pData = data;
}


void TestClass::Print()
{
    cout << this->oData.iData << endl;
    cout << this->oData.sData << endl;

    cout << this->pData->iData << endl;
    cout << this->pData->sData << endl;
    this->EventObject->handle();
}

TestDataClass* TestClass::GetPointerData()
{
    return this->pData;
}

TestDataClass TestClass::GetData()
{
    return this->oData;
}

void TestClass::SetEventObj(EventHandlerBase* data)
{
    if (data)
    {
        this->EventObject = data;
    }
    else
    {
        cout << "Event object is null" << endl;
    }
}

''' '''

''' Interface code '''接口代码

%module CppTestApp

 %include <windows.i>
 %include <std_string.i>

 // generate directors for all classes that have virtual methods
%feature("director") EventHandlerBase; 

%{
    #include "TestClass.h"
    #include "TestDataClass.h"
%}


%include "TestClass.h"
%include "TestDataClass.h"

''' '''

''' C# Code ''' C# 代码

 class MyEventHandler : EventHandlerBase
    {
        public MyEventHandler() : base(System.IntPtr.Zero, false) { }
        public override void handle()
        {
            System.Console.WriteLine("handling event...");
        }
    }

 static void Main(string[] args)
        {
            TestClass cpp_File = new TestClass();

            MyEventHandler myEvent = new MyEventHandler();

            TestDataClass data = new TestDataClass() { iData = 10, sData = "Hello I am Object" };
            TestDataClass pData = new TestDataClass() { iData = 25, sData = "Hello I am Pointer" };

            Console.WriteLine(cpp_File.times2(1, "Haii").ToString());

            cpp_File.SetData(data);
            cpp_File.SetPointerData(pData);

            Console.WriteLine($"{cpp_File.GetData().iData}  ,  {cpp_File.GetData().sData}");
            Console.WriteLine($"{cpp_File.GetPointerData().iData}  ,  {cpp_File.GetPointerData().sData}");

            cpp_File.SetEventObj(myEvent);

            cpp_File.Print();


            Console.WriteLine("-----------------------------");
            Console.ReadLine();
        }

''' '''

When I execute cpp_File.SetEventObj(myEvent);当我执行cpp_File.SetEventObj(myEvent); Object setting to Cpp is null. Cpp 的对象设置为空。 So I am not able call the virtual function.所以我无法调用虚函数。

Can you help me in this?你能帮我吗?

Also if this is not the proper way then please advice some method to achieve Callback from C++ to c# (using SWIG) or any other method to set a function pointer from C# to C++ (using SWIG) .此外,如果这不是正确的方法,那么请建议一些方法来实现从 C++ 到 c# 的回调(使用 SWIG)或任何其他方法将函数指针从 C# 设置为 C++(使用 SWIG)

So that I can set a function pointer from C# to C++ and achieve callback to C# by calling that function pointer这样我就可以设置一个从 C# 到 C++ 的函数指针,并通过调用该函数指针来实现对 C# 的回调

I got an answer for the above query and I am posting it here.我得到了上述查询的答案,并将其发布在这里。 You can also refer to this Link你也可以参考这个链接

After changing the SWIG interface code as below, it worked for me如下更改 SWIG 接口代码后,它对我有用

''' Interface Code '''接口代码

%module (directors="1") CppTestApp


%{
    #include "TestClass.h"

    #include "TestDataClass.h"
%}


 %include <windows.i>
 %include <std_string.i>

%feature("director") EventHandlerBase;

%include "TestClass.h"
%include "TestDataClass.h"

''' '''

''' C# Code ''' C# 代码

class MyEventHandler : EventHandlerBase
    {

        public override void handle()
        {
            System.Console.WriteLine("handling event...");
        }
    }

''' '''

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM