使用 SWIG 从 C++ 回调到 C# [英] Callback from C++ to C# using SWIG
问题描述
我有一个在 C#.Netcore 和 C++ windows 应用程序中运行的应用程序.
我实现了 C# & 之间的互操作性使用 SWIG 的 C++.
I have an application running in C#.Netcore and C++ windows application.
I achieved interoperability between C# & C++ using SWIG.
但我无法实现从 C++ 到 C# 的回调功能.我也尝试通过将函数指针从 C# 传递给 C++.但它也失败了
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
我的意图是通过
通过将 C# 函数指针传递给 C++ 并在需要时调用该函数指针,以便执行 C# 函数.
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++,以便我可以调用 object->VirtualMethod 将调用 C# 函数.
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++ 代码
''' 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++ 实现
'''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# 代码
'''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();
}
'''
当我执行 cpp_File.SetEventObj(myEvent); 对象设置为 Cpp 为空.所以我无法调用虚函数.
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?
另外,如果这不是正确的方法,那么请建议一些方法来实现从 C++ 到 C# 的回调(使用 SWIG) 或任何其他方法将函数指针从 C# 设置为 C++(使用 SWIG).
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++ 的函数指针,并通过调用该函数指针实现对 C# 的回调
So that I can set a function pointer from C# to C++ and achieve callback to C# by calling that function pointer
推荐答案
我得到了上述查询的答案,我将其发布在这里.你也可以参考这个链接
I got an answer for the above query and I am posting it here. You can also refer to this Link
更改 SWIG 接口代码如下后,它对我有用
After changing the SWIG interface code as below, it worked for me
'''接口代码
%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# 代码
'''C# Code
class MyEventHandler : EventHandlerBase
{
public override void handle()
{
System.Console.WriteLine("handling event...");
}
}
'''
这篇关于使用 SWIG 从 C++ 回调到 C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!