问题描述
我有一个静态C库,其中有非静态回调函数。注册此回调的客户端程序从摄像机获取视频数据。
I have a static C library where I have non static call back function. The Client program that register this callback gets Video data from camera .
现在,我正在C ++ / CLI中为此编写Wrapper( DLL )。该包装器Dll将用在C#应用程序中。
Now I am writing Wrapper(DLL) for this in C++/CLI.This Wrapper Dll will be used in C# application.
如何在C ++ / CLI中实现回调,以便C#代码可以注册该回调并从中获取视频数据。 / p>
How to Implement the callback in C++/CLI so that C# code can register it and gets the video data from it.
推荐答案
在C ++ / CLI中,您可以具有静态函数(带有本机C签名,可以用作C库的回调) ),调用托管代表:
In C++/CLI, you can have static functions (with native C signature, which can work as a callback from a C library), calling managed delegates:
// MyDispatcherClass.h
#pragma once
public delegate void MyDelegateType();
public ref class MyDispatcherClass
{
public:
static MyDelegateType^ MyDelegate;
};
static void MyCallback(/*...*/)
{
if (MyDispatcherClass::MyDelegate != nullptr)
MyDispatcherClass::MyDelegate(/* do some type mapping here if needed */);
}
// MyDispatcherClass.cpp:
#include "stdafx.h"
#include "MyDispatcherClass.h"
因此在C库中注册 MyCallback
,将C#委托注册为 MyDispatcherClass :: MyDelegate
完成。
So register MyCallback
at your C library, register your C# delegate to MyDispatcherClass::MyDelegate
and you are done.
这篇关于在C ++ / CLI中包装C回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!