C ++ / CLI类包装器c库 - 回调 [英] C++/CLI class wrapper for c library - callbacks

查看:147
本文介绍了C ++ / CLI类包装器c库 - 回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用C ++ / CLI封装了一个C库。 C库设计用于非托管C ++类。这意味着库函数接受一个C ++对象指针,然后在回调中提供该指针。这使得回调代码可以将请求重定向到调用C ++对象中的一个适当的事件函数。

I am wrapping a C library using C++/CLI. The C library was designed to be used from an unmanaged C++ class. This means that the library functions accept a C++ object pointer and then provide that pointer back in callbacks. This enables the callback code to redirect requests to an appropriate event function in the calling C++ object.

实际的功能相当复杂,所以我把问题空间简化为一些基本项目:

The actual functions are quite involved, so I have simplified the problem space to just a few basic items:

// C library function signature
void CLibFunc(CLIBCALLBACK *callback, void *caller);

// C callback signature
// Second parameter is meant to point to the calling C++ object
typedef int (__stdcall CLIBCALLBACK) (int param1, void *caller);

// C callback implementation
int CallBackImpl(int param1, void* caller)
{
    // Need to call the ManagedCaller's EventFunction from here
    // ???
}

// C++/CLI caller class
public ref class ManagedCaller
{
    public:
        void CallerFunction(void)
        {
            // Call the C library function
            // Need to pass some kind of this class pointer that refers to this object
            CLibFunc(CallBackImpl, ????);
        }

        void EventFunction(param1)
        {
        }
}

现在,C库函数需要从托管C ++类中调用。在C ++ / CLI下,垃圾收集器在内存中移动对象,因此传递一个简单的固定指针到类不再工作。我可以通过固定对象来解决问题,但是不推荐,因为它导致内存碎片。似乎另一个选择是使用auto_gcroot指针,但我是管理C ++的一个新的,我不知道如何使这项工作。

Now the C library functions need to be called from a managed C++ class. Under C++/CLI, the garbage collector moves objects around in memory, so passing a simple fixed pointer to the class does not work anymore. I can solve the problem by pinning the object, but that is not recommended because it leads to memory fragmentation. It seems that another option would be to use auto_gcroot pointers, but I am fairly new to managed C++ an I am not sure how to make this work.

有谁知道如何使这项工作?什么样的指针应该传递给C函数?回调实现应该如何重定向到调用对象的事件函数?

Does anyone know how to make this work? What kind of pointer should be passed to the C function? How should the callback implementation redirect to the calling object's event function?

推荐答案

这是一篇有关使用C ++类提供本机回调的博客文章: http://blogs.microsoft.co.il/blogs/alon/archive/2007/05/29 /Native-Callback.aspx

Here is an blog post on providing native callbacks using C++ classes: http://blogs.microsoft.co.il/blogs/alon/archive/2007/05/29/Native-Callback.aspx

我不熟悉从C调用C ++的成员函数,但我做了一个接口(抽象基类)类另一个C ++类的回调(类似于文章)。这是一个基本的例子,我提供一个桥梁:

I'm not familiar with calling C++ member functions from C, but I have done an interface (abstract base) class to another C++ class for callbacks (similar to the article). Here is a basic example of what I am providing a bridge for:

// Interface (abstract base) class providing the callback
class IProvider {
public:
    virtual ~IProvider() {}
    virtual void Callback() = 0;
};

// User class of the callback
class CUser {   
    IProvider * m_pProvider;
public:
    CUser(IProvider * pProvider) {
        m_pProvider = pProvider;
    }
    void DoSomething() {
        m_pProvider->Callback();
    }
};

// Implementation of the interface class
class CHelloWorldProvider : public IProvider {
    void Callback() {
        printf("Hello World!");
    }
};

// Usage of the callback provider in a pure native setting
void PureNativeUsage() {
    CHelloWorldProvider oProvider;
    CUser oUser(&oProvider);
    oUser.DoSomething();
}

现在为了使这个可用于提供程序的托管实现,创建一系列提供桥梁的类。

Now in order to make this available for managed implementations of the provider, we have to create a series of classes that provide the bridge.

// Where gcroot is defined
#include <vcclr.h>

// Managed provider interface class 
public interface class IManagedProvider {
    void Callback();
};

// Native bridge class that can be passed to the user
class CProviderBridge : public IProvider {
    // Give the managed class full access
    friend ref class ManagedProviderBase;

    // Store a reference to the managed object for callback redirects
    gcroot<IManagedProvider ^> m_rManaged;

public:
    void Callback(){
        m_rManaged->Callback();
    }
};

// Managed provider base class, this provides a managed base class for extending
public ref class ManagedProviderBase abstract : public IManagedProvider {
    // Pointer to the native bridge object
    CProviderBridge * m_pNative;

protected:
    ManagedProviderBase() {
        // Create the native bridge object and set the managed reference
        m_pNative = new CProviderBridge();
        m_pNative->m_rManaged = this;
    }

public:
    ~ManagedProviderBase() {
        delete m_pNative;
    }

    // Returns a pointer to the native provider object
    IProvider * GetProvider() {
        return m_pNative;
    }

    // Makes the deriving class implement the function
    virtual void Callback() = 0;
};

// Pure managed provider implementation (this could also be declared in another library and/or in C#/VB.net)
public ref class ManagedHelloWorldProvider : public ManagedProviderBase {
public:
    virtual void Callback() override {
        Console::Write("Hello World");
    }
};

// Usage of the managed provider from the native user
void MixedUsage() {
    ManagedHelloWorldProvider ^ rManagedProvider = gcnew ManagedHelloWorldProvider;
    CUser oUser(rManagedProvider->GetProvider());
    oUser.DoSomething();
}






添加代码以显示我使用的托管接口类示例。


Added code to show w/o the managed interface class example I use.

这是我的示例的修改版本, c $ c> CLibFunc 。这是假设C函数如何执行回调是准确的。

Here is a modified version of my example that can be used given your CLibFunc above. This is assuming how the C function performs the callback is accurate.

这也许可以减少一点,取决于你的回调类是如何涉及和你有多少自由扩展你需要。

Also this might be able to be slimmed down a bit depending on how involved your callback classes are and how much freedom for extension you need.

// Where gcroot is defined
#include <vcclr.h>

// C callback signature
// Second parameter is meant to point to the calling C++ object
typedef int (__stdcall CLIBCALLBACK) (int param1, void *caller);

// C library function
void CLibFunc(CLIBCALLBACK *callback, void *caller) {
    // Do some work
    (*callback)(1234, caller);
    // Do more work
}

// Managed caller interface class 
public interface class IManagedCaller {
    void EventFunction(int param1);
};

// C++ native bridge struct
struct CCallerBridge {
    // Give the managed class full access
    friend ref class ManagedCaller;

    // Store a reference to the managed object for callback redirects
    gcroot<IManagedCaller ^> m_rManaged;

public:
    // Cast the caller to the native bridge and call managed event function
    // Note: This must be __stdcall to prevent function call stack corruption
    static int __stdcall CallBackImpl(int param1, void * caller) {
        CCallerBridge * pCaller = (CCallerBridge *) caller;
        pCaller->m_rManaged->EventFunction(param1);
        return 0;
    }
};

// C++/CLI caller class
public ref class ManagedCaller : public IManagedCaller {
    // Pointer to the native bridge object
    CCallerBridge * m_pNative;

public:
    ManagedCaller() {
        // Create the native bridge object and set the managed reference
        m_pNative = new CCallerBridge();
        m_pNative->m_rManaged = this;
    }
    ~ManagedCaller() {
        delete m_pNative;
    }

    // Calls the C library function
    void CallerFunction() {
        CLibFunc(CCallerBridge::CallBackImpl, m_pNative);
    }

    // Managed callback function
    virtual void EventFunction(int param1) {
        Console::WriteLine(param1);
    }
};

// Usage
int main(array<System::String ^> ^args) {
    ManagedCaller ^ oCaller = gcnew ManagedCaller();
    oCaller->CallerFunction();
    return 0;
}

这篇关于C ++ / CLI类包装器c库 - 回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆