为C+实现回调C#函数+;动态链接库 我正在为C++库编写一个DLL包装器,从C调用。该包装器还应该具有从库中调用并在C#中实现的回调函数。例如,这些函数将std::vector作为输出参数。我不知道怎么做这个。如何通过回调函数将未知大小的缓冲区从C到C传递到C++?p>

为C+实现回调C#函数+;动态链接库 我正在为C++库编写一个DLL包装器,从C调用。该包装器还应该具有从库中调用并在C#中实现的回调函数。例如,这些函数将std::vector作为输出参数。我不知道怎么做这个。如何通过回调函数将未知大小的缓冲区从C到C传递到C++?p>,c#,c++,interop,C#,C++,Interop,让我们举个例子 CallbackFunction FunctionImplementedInCSharp; void FunctionCalledFromLib(const std::vector<unsigned char>& input, std::vector<unsigned char>& output) { // Here FunctionImplementedInCSharp (C# delegate) should somehow

让我们举个例子

CallbackFunction FunctionImplementedInCSharp;

void FunctionCalledFromLib(const std::vector<unsigned char>& input, std::vector<unsigned char>& output)
{
    // Here FunctionImplementedInCSharp (C# delegate) should somehow be called
}

void RegisterFunction(CallbackFunction f)
{
    FunctionImplementedInCSharp = f;
}
CallbackFunction函数在CSharp中实现;
void函数calledFromlib(常量std::vector&input,std::vector&output)
{
//这里应该以某种方式调用函数implementedincharp(C#delegate)
}
void RegisterFunction(CallbackFunction f)
{
函数implementedincharp=f;
}
应该如何定义
CallbackFunction
,函数calledFromlib中的代码是什么


<> P>一个让我哑口无言的事情是:我如何删除C++中C++中创建的缓冲区?

< P>有一些你应该注意的事情。第一个问题是,如果您是从非托管代码调用.NET委托,那么除非您遵循一些非常狭窄的约束,否则您将非常痛苦

理想情况下,您可以在C#中创建一个委托,将其传递到托管代码中,将其封送到函数指针中,任意保留它,然后调用它而不会产生不良影响。NET文档是这样说的

我可以告诉你,这根本不是真的。最终,委托的一部分或其thunk将被垃圾收集,当您从非托管代码调用函数指针时,您将被发送到遗忘。我不在乎微软怎么说,我严格按照他们的处方,看着函数指针变成垃圾,尤其是在服务器端代码中

因此,使用函数指针的最有效方法是:

  • C代码调用非托管代码,传入委托
  • 非托管代码将委托封送到函数指针
  • 非托管代码执行一些工作,可能会调用函数指针
  • 非托管代码删除对函数指针的所有引用
  • 非托管代码返回托管代码
考虑到这一点,假设我们在C#中有以下内容:

然后在托管C++(不):

我最近没有在C++/CLI中这样做-语法不同-我认为它最终看起来是这样的:

// This is declared in a class
static CallIntoUnamangedCode(MyManagedDelegate ^delegate)
{
    pin_ptr<MyManagedDelegate ^> pinnedDelegate = &delegate;
    SOME_CALLBACK_PTR p = Marshal::GetFunctionPointerForDelegate(pinnedDelegate);
    CallDeepIntoUnmanagedCode(p); // This will call p
}
typedef void * (*f_AllocPtr) (size_t nBytes);
typedef void *t_AllocCookie;

extern void RegisterAllocFunction(f_AllocPtr allocPtr, t_AllocCookie cookie);
我们的期望是,当您调用分配内存的API时,它将被矢量化到提供的
f_AllocPtr
中。信不信由你,你可以用C写这个。它很甜:

public IntPtr ManagedAllocMemory(long nBytes)
{
    byte[] data = new byte[nBytes];
    GCHandle dataHandle = GCHandle.Alloc(data, GCHandleType.Pinned);
    unsafe {
        fixed (byte *b = &data[0]) {
            dataPtr = new IntPtr(b);
            RegisterPointerHandleAndArray(dataPtr, dataHandle, data);
            return dataPtr;
        }
    }
}
登记点Handlean Darray将三胞胎塞满,以便安全保管。这样,当调用相应的free时,您可以执行以下操作:

public void ManagedFreeMemory(IntPtr dataPointer)
{
    GCHandle dataHandle;
    byte[] data;
    if (TryUnregister(dataPointer, out dataHandle, out data)) {
        dataHandle.Free();
        // do anything with data?  I dunno...
    }
}
当然,这是愚蠢的,因为分配的内存现在被固定在GC堆中,并将其碎片化到地狱——但关键是这是可行的


但是,我个人也看到了这种失败,除非实际的指针是短期的。这通常意味着包装API,以便在调用完成特定任务的例程时,它注册回调,执行该任务,然后拉出回调

事实证明,原来问题的答案很简单,一旦你知道了,整个回调问题就没有问题了。输入缓冲区参数替换为参数对
unsigned char*input,int-input\u length
,输出缓冲区参数替换为参数对
unsigned char**output,int*output\u length
。C#代表应该是这样的

public delegate int CallbackDelegate(byte[] input, int input_length,
                                     out byte[] output, out int output_length);
C++中的包装>和包装应该是这样的< /P>
void FunctionCalledFromLib(const std::vector<unsigned char>& input, std::vector<unsigned char>& output)
{
    unsigned char *output_aux;
    int output_length;

    FunctionImplementedInCSharp(
        &input[0], input.size(), &ouput_aux, &output_length);

    output.assign(output_aux, output_aux + output_length);

    CoTaskMemFree(output_aux); // IS THIS NECESSARY?
}
void函数calledFromlib(const std::vector&input,std::vector&output)
{
无符号字符*输出;
int输出长度;
函数在CSharp中实现(
&输入[0]、输入.size()、&ouput\U aux和&output\U length);
分配(输出\辅助,输出\辅助+输出\长度);
CoTaskMemFree(output_aux);//这是必需的吗?
}
最后一行是迷你拼图的最后一部分。我必须打电话吗,还是由警察自动为我打电话


关于PrimthWe的漂亮文章,我希望通过使用静态函数来绕过整个问题。VisualStudio 2013至少有一个安全的方法:将C语言的回调传递给C++,并有C++存储,然后从非托管代码异步调用它们。您可以创建一个托管C++/CX类(例如,名为“CallbackManager”)来保存映射中的回调委托引用,并为每个引用键入一个枚举值。然后,非托管代码可以通过委托的关联枚举值从托管C++/CX CallbackManager类检索托管委托引用。这样,您就不必存储原始函数指针,也就不必担心委托被移动或被垃圾收集:它在整个生命周期中都保持在托管堆中

<>在CalbBuffsMeals+H:/P> C++侧
#include <unordered_map>
#include <mutex>

using namespace Platform;

namespace CPPCallbacks
{
    // define callback IDs; this is what unmanaged C++ code will pass to the managed CallbacksManager class to retrieve a delegate instance
    public enum class CXCallbackType
    {
        cbtLogMessage,
        cbtGetValueForSetting
        // TODO: add additional enum values as you add more callbacks
    }

    // defines the delegate signatures for our callbacks; these are visible to the C# side as well
    public delegate void LogMessageDelegate(int level, String^ message);
    public delegate bool GetValueForSettingDelegate(String^ settingName, String^* settingValueOut);
    // TODO: define additional callbacks here as you need them

     // Singleton WinRT class to manage C# callbacks; since this class is marked 'public' it is consumable from C# as well
    public ref class CXCallbacksManager sealed
    {
    private:
        CXCallbacksManager() { }  // this is private to prevent incorrect instantiation

    public:
        // public methods and properties are all consumable by C# as well
        virtual ~CXCallbacksManager() { }

        static property CXCallbacksManager^ Instance
        {
            CXCallbacksManager^ get();
        }

        bool UnregisterCallback(CXCallbackType cbType);
        void UnregisterAllCallbacks();
        Delegate^ GetCallback(CXCallbackType cbType);

        // define callback registration methods
        RegisterLogMessageCallback(LogMessageDelegate^ cb) { RegisterCallback(CXCallbackType::cbtLogMessage, cb); }
        RegisterGetValueForSettingCallback(GetValueForSettingDelegate^ cb) { RegisterCallback(CXCallbackType::GetValueForSetting, cb); }
        // TODO: define additional callback registration methods as you add more callbacks

    private:
        void RegisterCallback(CXCallbackType cbType, Delegate^ rCallbackFunc);

        typedef unordered_map<CXCallbackType, Delegate^> CALLBACK_MAP;
        typedef pair<CXCallbackType, Delegate^> CBType_Delegate_Pair;

        // Note: IntelliSense errors shown for static data is a Visual Studio IntellSense bug; the code below builds fine
        // See http://social.msdn.microsoft.com/Forums/windowsapps/en-US/b5d43215-459a-41d6-a85e-99e3c30a162e/about-static-member-of-ref-class?forum=winappswithnativecode
        static mutex s_singletonMutex;
        static CXCallbacksManager^ s_rInstance;

        mutex m_callbackMapMutex;
        CALLBACK_MAP m_callbacksMap;   // key=CallbackType, value = C# delegate (function) pointer
    };
}  

最后,这里是如何调用来自非托管C++代码的注册C回调:

#include <assert.h>
#include "CXCallbacksManager.h"

using namespace Platform;

namespace CPPCallbacks
{
    // define static class data
    CXCallbacksManager^ CXCallbacksManager::s_rInstance;
    mutex CXCallbacksManager::s_singletonMutex;

    // Returns our singleton instance; this method is thread-safe
    CXCallbacksManager^ CXCallbacksManager::Instance::get()
    {
        s_singletonMutex.lock();

        if (s_rInstance == nullptr)
            s_rInstance = ref new CXCallbacksManager();  // this lives until the application terminates

        s_singletonMutex.unlock();
        return s_rInstance;
    }

    // Register a C# callback; this method is thread-safe
    void CXCallbacksManager::RegisterCallback(const CXCallbackType cbType, Delegate^ rCallbackFunc)
    {
        _ASSERTE(rCallbackFunc);

        m_callbackMapMutex.lock();
        m_callbacksMap.insert(CBType_Delegate_Pair(cbType, rCallbackFunc)); 
        m_callbackMapMutex.unlock();
    }

    // Unregister a C# callback; this method is thread-safe
    // Returns: true on success, false if no callback was registered for callbackType
    bool CXCallbacksManager::UnregisterCallback(const CXCallbackType cbType)
    {
        m_callbackMapMutex.lock();
        const bool bRemoved = (m_callbacksMap.erase(cbType) > 0);
        m_callbackMapMutex.unlock();

        return bRemoved;
    }

    // Unregister all callbacks; this method is thread-safe
    void CXCallbacksManager::UnregisterAllCallbacks()
    {
        // must lock the map before iterating across it
        // Also, we can't change the contents of the map as we iterate across it, so we have to build a vector of all callback types in the map first.
        vector<CXCallbackType> allCallbacksList;
        m_callbackMapMutex.lock();

        for (CALLBACK_MAP::const_iterator it = m_callbacksMap.begin(); it != m_callbacksMap.end(); it++)
            allCallbacksList.push_back(it->first);

        for (unsigned int i = 0; i < allCallbacksList.size(); i++)
        {
            CALLBACK_MAP::const_iterator it = m_callbacksMap.find(allCallbacksList[i]);
            if (it != m_callbacksMap.end())     // sanity check; should always succeed
                UnregisterCallback(it->first);
        }
        m_callbackMapMutex.unlock();
    }

    // Retrieve a registered C# callback; returns NULL if no callback registered for type
    Delegate^ CXCallbacksManager::GetCallback(const CXCallbackType cbType)
    {
        Delegate^ rCallbackFunc = nullptr;
        m_callbackMapMutex.lock();

        CALLBACK_MAP::const_iterator it = m_callbacksMap.find(cbType);
        if (it != m_callbacksMap.end())
            rCallbackFunc = it->second;
        else
            _ASSERTE(false);    // should never happen! This means the caller either forgot to register a callback for this cbType or already unregistered the callback for this cbType.

        m_callbackMapMutex.unlock();
        return rCallbackFunc;
    }
}
#include <assert.h>
#include <atlstr.h>   // for CStringW
#include "CXCallbacksManager.h"

using namespace CPPCallbacks;

// this is an unmanaged C++ function in the same project as our CXCallbacksManager class
void LogMessage(LogLevel level, const wchar_t *pMsg)
{
    _ASSERTE(msg);

    auto rCallback = static_cast<LogMessageDelegate^>(CXCallbacksManager::Instance->GetCallback(CXCallbackType::cbtLogMessage));
    _ASSERTE(rCallback);
    rCallback(level, ref new String(pMsg));   // invokes C# method
}

// this is an unmanaged C++ function in the same project as our CXCallbacksManager class
// Sets settingValue to the value retrieved from C# for pSettingName
// Returns: true if the value existed and was set, false otherwise
bool GetValueForSetting(const wchar_t *pSettingName, CStringW &settingValue)
{
    bool bRetCode = false;

    auto rCallback = static_cast<GetValueForSettingDelegate^>(CXCallbacksManager::Instance->GetCallback(CXCallbackType::cbtGetValueForSetting));
    _ASSERTE(rCallback);
    if (rCallback)    // sanity check; should never be null
    {
        String^ settingValueOut;
        bRetCode = rCallback(ref new String(pSettingName), &settingValueOut);

        // store the retrieved setting value to our unmanaged C++ CStringW output parameter
        settingValue = settingValueOut->Data(); 
    }
    return bRetCode;
}
#包括
#包括//用于CStringW
#包括“CXCallbacksManager.h”
使用名称空间回调;
//这是与我们的CXCALBACKSAMFER类相同的项目中的非托管C++函数
无效日志消息(日志级别,常量wchar\u t*pMsg)
{
_ASSERTE(味精);
auto rCallback=static_cast(cxcallbackmanager::Instance->GetCallback(CXCallbackType::cbtLogMessage));
_资产(rCallback);
rCallback(level,ref new String(pMsg));//调用C#方法
}
//这是与我们的CXCALBACKSAMFER类相同的项目中的非托管C++函数
//将settingValue设置为从C#检索到的pSettingName值
//返回:如果值存在且已设置,则返回true,否则返回false
bool GetValueForSetting(常量wchar\u t*pSettingName、CStringW和settingValue)
{
bool-bRetCode=false;
auto rCallback=static_cast(cxcallbackmanager::Instance->GetCallback(CXCallbackType::cbtGetValueForSetting));
_阿塞
#include <assert.h>
#include "CXCallbacksManager.h"

using namespace Platform;

namespace CPPCallbacks
{
    // define static class data
    CXCallbacksManager^ CXCallbacksManager::s_rInstance;
    mutex CXCallbacksManager::s_singletonMutex;

    // Returns our singleton instance; this method is thread-safe
    CXCallbacksManager^ CXCallbacksManager::Instance::get()
    {
        s_singletonMutex.lock();

        if (s_rInstance == nullptr)
            s_rInstance = ref new CXCallbacksManager();  // this lives until the application terminates

        s_singletonMutex.unlock();
        return s_rInstance;
    }

    // Register a C# callback; this method is thread-safe
    void CXCallbacksManager::RegisterCallback(const CXCallbackType cbType, Delegate^ rCallbackFunc)
    {
        _ASSERTE(rCallbackFunc);

        m_callbackMapMutex.lock();
        m_callbacksMap.insert(CBType_Delegate_Pair(cbType, rCallbackFunc)); 
        m_callbackMapMutex.unlock();
    }

    // Unregister a C# callback; this method is thread-safe
    // Returns: true on success, false if no callback was registered for callbackType
    bool CXCallbacksManager::UnregisterCallback(const CXCallbackType cbType)
    {
        m_callbackMapMutex.lock();
        const bool bRemoved = (m_callbacksMap.erase(cbType) > 0);
        m_callbackMapMutex.unlock();

        return bRemoved;
    }

    // Unregister all callbacks; this method is thread-safe
    void CXCallbacksManager::UnregisterAllCallbacks()
    {
        // must lock the map before iterating across it
        // Also, we can't change the contents of the map as we iterate across it, so we have to build a vector of all callback types in the map first.
        vector<CXCallbackType> allCallbacksList;
        m_callbackMapMutex.lock();

        for (CALLBACK_MAP::const_iterator it = m_callbacksMap.begin(); it != m_callbacksMap.end(); it++)
            allCallbacksList.push_back(it->first);

        for (unsigned int i = 0; i < allCallbacksList.size(); i++)
        {
            CALLBACK_MAP::const_iterator it = m_callbacksMap.find(allCallbacksList[i]);
            if (it != m_callbacksMap.end())     // sanity check; should always succeed
                UnregisterCallback(it->first);
        }
        m_callbackMapMutex.unlock();
    }

    // Retrieve a registered C# callback; returns NULL if no callback registered for type
    Delegate^ CXCallbacksManager::GetCallback(const CXCallbackType cbType)
    {
        Delegate^ rCallbackFunc = nullptr;
        m_callbackMapMutex.lock();

        CALLBACK_MAP::const_iterator it = m_callbacksMap.find(cbType);
        if (it != m_callbacksMap.end())
            rCallbackFunc = it->second;
        else
            _ASSERTE(false);    // should never happen! This means the caller either forgot to register a callback for this cbType or already unregistered the callback for this cbType.

        m_callbackMapMutex.unlock();
        return rCallbackFunc;
    }
}
using CPPCallbacks;

namespace SomeAppName
{
    internal static class Callbacks
    {
        // invoked during app startup to register callbacks for unmanaged C++ code to invoke asynchronously
        internal static void RegisterCallbacks()
        {
            CPPCallbacks.CXCallbacksManager.Instance.RegisterLogMessageCallback(new LogMessageDelegate(LogMessageDelegateImpl));
            CPPCallbacks.CXCallbacksManager.Instance.RegisterGetValueForSettingCallback(new GetValueForSettingDelegate(GetValueForSettingDelegateImpl));
            // TODO: register additional callbacks as you add them
        }

        //-----------------------------------------------------------------
        // Callback delegate implementation methods are below; these are invoked by C++
        // Although these example implementations are in a static class, you could also pass delegate instances created 
        // from inside a non-static class, which would maintain their state just like any other instance method (i.e., they have a 'this' object).
        //-----------------------------------------------------------------

        private static void LogMessageDelegateImpl(int level, string message)
        {
            // This next line is shown for example purposes, but at this point you can do whatever you want because 
            // you are running in a normal C# delegate context.
            Logger.WriteLine(level, message);
        }

        private static bool GetValueForSettingDelegateImpl(String settingName, out String settingValueOut)
        {
            // This next line is shown for example purposes, but at this point you can do whatever you want because 
            // you are running in a normal C# delegate context.
            return Utils.RetrieveEncryptedSetting(settingName, out settingValueOut);   
        }
    };
}
#include <assert.h>
#include <atlstr.h>   // for CStringW
#include "CXCallbacksManager.h"

using namespace CPPCallbacks;

// this is an unmanaged C++ function in the same project as our CXCallbacksManager class
void LogMessage(LogLevel level, const wchar_t *pMsg)
{
    _ASSERTE(msg);

    auto rCallback = static_cast<LogMessageDelegate^>(CXCallbacksManager::Instance->GetCallback(CXCallbackType::cbtLogMessage));
    _ASSERTE(rCallback);
    rCallback(level, ref new String(pMsg));   // invokes C# method
}

// this is an unmanaged C++ function in the same project as our CXCallbacksManager class
// Sets settingValue to the value retrieved from C# for pSettingName
// Returns: true if the value existed and was set, false otherwise
bool GetValueForSetting(const wchar_t *pSettingName, CStringW &settingValue)
{
    bool bRetCode = false;

    auto rCallback = static_cast<GetValueForSettingDelegate^>(CXCallbacksManager::Instance->GetCallback(CXCallbackType::cbtGetValueForSetting));
    _ASSERTE(rCallback);
    if (rCallback)    // sanity check; should never be null
    {
        String^ settingValueOut;
        bRetCode = rCallback(ref new String(pSettingName), &settingValueOut);

        // store the retrieved setting value to our unmanaged C++ CStringW output parameter
        settingValue = settingValueOut->Data(); 
    }
    return bRetCode;
}
public ImageSurface(byte[] pngData)
    : base(ConstructImageSurfaceFromPngData(pngData), true)
{
    offset = 0;
}

private static int offset;

private static IntPtr ConstructImageSurfaceFromPngData(byte[] pngData)
{
    NativeMethods.cairo_read_func_t func = delegate(IntPtr closure, IntPtr out_data, int length)
    {
        Marshal.Copy(pngData, offset, out_data, length);
        offset += length;
        return Status.Success;
    };
    return NativeMethods.cairo_image_surface_create_from_png_stream(func, IntPtr.Zero);
}