Callback from C++ to C# using SWIG

喜你入骨 提交于 2021-02-20 19:22:11

问题


I have an application running in C#.Netcore and C++ windows application.
I achieved interoperability between C# & C++ using SWIG.

But I am not able to achieve Callback functionality from C++ to C#. Also I tried by passing function pointer from C# to 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.

  2. Creating a base class with virtual function in C++ and derive a class in C# which implement the virtual method. The set the object of derived to C++ so that I can call the object->VirtualMethod will invoke C# function.

But both methods failed.

''' C++ Code

    #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

#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

 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); Object setting to Cpp is null. 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).

So that I can set a function pointer from C# to C++ and achieve callback to C# by calling that function pointer


回答1:


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

'''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

class MyEventHandler : EventHandlerBase
    {

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

'''



来源:https://stackoverflow.com/questions/59657232/callback-from-c-to-c-sharp-using-swig

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!