如何 boost::bind 到传递并返回 std::string 的托管类的成员?

2024-03-06

我正在尝试做一些非常类似的事情this https://stackoverflow.com/questions/163757/how-to-use-boostbind-in-c-cli-to-bind-a-member-of-a-managed-class但我正在努力将字符串传递给回调或从回调传递字符串。

这是我尝试运行的代码的精简版本:

using namespace System;
using namespace System::Runtime::InteropServices;

#pragma unmanaged

// The unmanaged boost function prototype the native library wants to bind to
typedef boost::function<std::string(const std::string&)> MyNativeCallback;

// The unmanaged library I'm trying to wrap
class MyUnmanagedClass
{
    public:
        MyUnmanagedClass() {}

        void RegisterCallback(MyNativeCallback callback);
};

// Taken from here: https://stackoverflow.com/questions/163757/how-to-use-boostbind-in-c-cli-to-bind-a-member-of-a-managed-class
typedef std::string(__stdcall *ChangeCallback)(std::string);
public delegate std::string ChangeHandler(std::string);

#pragma managed

// The managed callback we'll eventually trigger if all this works.
public delegate String^ ManagedHandler(String^);

// A managed wrapper to bridge the gap and provide a managed call in response to the unmanaged callback
public ref class MyManagedClass
{
protected:

    MyUnmanagedClass* m_responder;

public:
    event ManagedHandler^ OnChange;

public:
    MyManagedClass():
        m_responder(new MyUnmanagedClass())
    {
        // Example code I'm trying to adapt from here: https://stackoverflow.com/questions/163757/how-to-use-boostbind-in-c-cli-to-bind-a-member-of-a-managed-class
        ChangeHandler^ handler = gcnew ChangeHandler(this, &MyManagedClass::HandleRequest);
        GCHandle gch = GCHandle::Alloc(handler);
        System::IntPtr ip = Marshal::GetFunctionPointerForDelegate(handler);
        ChangeCallback cbFunc = static_cast<ChangeCallback>(ip.ToPointer());

        MyNativeCallback handlerToBind = boost::bind(cbFunc, _1); // <-- This fails with 'error C2825'
        m_responder->RegisterCallback(handlerToBind);
    }

    std::string HandleRequest(std::string s)
    {
        OnChange(nullptr);
        return nullptr;
    }
};

...这给了我以下错误:

1>C:\boost_1_55_0\boost/bind/bind.hpp(69): error C2825: 'F': must be a class or namespace when followed by '::'
1>          C:\boost_1_55_0\boost/bind/bind_template.hpp(15) : see reference to class template instantiation 'boost::_bi::result_traits<R,F>' being compiled
1>          with
1>          [
1>              R=boost::_bi::unspecified,
1>              F=std::basic_string<char,std::char_traits<char>,std::allocator<char>> (__stdcall *)(std::string)
1>          ]
1>          c:\projects\zephir-git\zephirsoftware-gitlab\lcufirmware\managedzeromqdataforwarding\Responder.h(54) : see reference to class template instantiation 'boost::_bi::bind_t<R,F,L>' being compiled
1>          with
1>          [
1>              R=boost::_bi::unspecified,
1>              F=std::string (__stdcall *)(std::string),
1>              L=boost::_bi::list1<boost::arg<1>>
1>          ]
1>C:\boost_1_55_0\boost/bind/bind.hpp(69): error C2039: 'result_type' : is not a member of '`global namespace''
1>C:\boost_1_55_0\boost/bind/bind.hpp(69): error C2146: syntax error : missing ';' before identifier 'type'
1>C:\boost_1_55_0\boost/bind/bind.hpp(69): error C2208: 'boost::_bi::type' : no members defined using this type
1>C:\boost_1_55_0\boost/bind/bind.hpp(69): fatal error C1903: unable to recover from previous error(s); stopping compilation

我不禁觉得我已经快要得到这个了,但我还缺少一些东西吗?


通过一些实验和一些更详细的检查解决了这个问题关于不同形式的文档bind http://www.boost.org/doc/libs/1_63_0/libs/bind/doc/html/bind.html#bind.faq.Q_forms.

这是按预期工作的结果代码:

using namespace System;
using namespace System::Runtime::InteropServices;

#pragma unmanaged

// The unmanaged boost function prototype the native library wants to bind to
typedef boost::function<std::string(const std::string&)> MyNativeCallback;

// The unmanaged library I'm trying to wrap
class MyUnmanagedClass
{
    public:
        MyUnmanagedClass() {}

        void RegisterCallback(MyNativeCallback callback);
};

typedef std::string(__stdcall *ChangeCallback)(std::string);
public delegate std::string ChangeHandler(std::string);

#pragma managed

// The managed callback we'll eventually trigger if all this works.
public delegate std::string ManagedHandler(const std::string&);

// A managed wrapper to bridge the gap and provide a managed call in response to the unmanaged callback
public ref class MyManagedClass
{
protected:

    MyUnmanagedClass* m_responder;

public:
    event ManagedHandler^ OnChange;

public:
    MyManagedClass():
        m_responder(new MyUnmanagedClass())
    {
        // Example code I'm trying to adapt from here: http://stackoverflow.com/questions/163757/how-to-use-boostbind-in-c-cli-to-bind-a-member-of-a-managed-class
        ChangeHandler^ handler = gcnew ChangeHandler(this, &MyManagedClass::HandleRequest);
        GCHandle gch = GCHandle::Alloc(handler);
        System::IntPtr ip = Marshal::GetFunctionPointerForDelegate(handler);
        ChangeCallback cbFunc = static_cast<ChangeCallback>(ip.ToPointer());

        MyNativeCallback handlerToBind = boost::bind<std::string>(cbFunc, _1);
        m_responder->RegisterCallback(handlerToBind);
    }

    std::string HandleRequest(const std::string& s)
    {
        return OnChange(s);
    }
};
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何 boost::bind 到传递并返回 std::string 的托管类的成员? 的相关文章

  • WPF 中的屏幕分辨率问题?

    我将在 WPF 中使用以下代码检测分辨率 double height System Windows SystemParameters PrimaryScreenHeight double width System Windows Syste
  • 在 C# 中生成 HMAC-SHA1

    我正在尝试使用 C 来使用 REST API API 创建者提供了以下用于 hmac 创建的伪代码 var key1 sha1 body var key2 key1 SECRET KEY var key3 sha1 key2 var sig
  • 带 If 的嵌套 For 循环的时间复杂度

    void f int n for int i 1 i lt n i if i int sqrt n 0 for int k 0 k lt pow i 3 k do something 我的思考过程 执行if语句的次数 sum i 1 to
  • HttpWebRequest vs Webclient(特殊场景)

    我知道这个问题之前已经回答过thread https stackoverflow com questions 1694388 webclient vs httpwebrequest httpwebresponse 但我似乎找不到详细信息 在
  • 无法解析远程名称 - webclient

    我面临这个错误 The remote name could not be resolved russgates85 001 site1 smarterasp net 当我请求使用 Web 客户端读取 html 内容时 出现错误 下面是我的代
  • 2D morton 码编码/解码 64 位

    如何将给定 x y 的莫顿代码 z 顺序 编码 解码为 32 位无符号整数 生成 64 位莫顿代码 反之亦然 我确实有 xy2d 和 d2xy 但仅适用于 16 位宽的坐标 产生 32 位莫顿数 在网上查了很多 但没有找到 请帮忙 如果您可
  • 使用 WF 的多线程应用程序的错误处理模式?

    我正在写一个又长又详细的问题 但只是放弃了它 转而选择一个更简单的问题 但我在这里找不到答案 应用程序简要说明 我有一个 WPF 应用程序 它生成多个线程 每个线程执行自己的 WF 处理线程和 WF 中的错误 允许用户从 GUI 端进行交互
  • C++11 动态线程池

    最近 我一直在尝试寻找一个用于线程并发任务的库 理想情况下 是一个在线程上调用函数的简单接口 任何时候都有 n 个线程 有些线程比其他线程完成得更快 并且到达的时间不同 首先我尝试了 Rx 它在 C 中非常棒 我还研究了 Blocks 和
  • C# 中的常量和只读? [复制]

    这个问题在这里已经有答案了 可能的重复 const 和 readonly 之间有什么区别 https stackoverflow com questions 55984 what is the difference between cons
  • tabcontrol selectedindex 更改事件未被触发 C#

    嘿伙计们 我有一个很小的问题 请参阅下面的代码 this is main load private void Form1 Load object sender EventArgs e tabAddRemoveOperator Selecte
  • 使用 mingw32 在 Windows 上构建 glew 时“DllMainCRTStartup@12”的多个定义

    我关注了这个主题 使用 mingw 使建筑物在 Windows 上闪闪发光 https stackoverflow com questions 6005076 building glew on windows with mingw 6005
  • ASP.NET JQuery AJAX POST 返回数据,但在 401 响应内

    我的应用程序中有一个网页 需要调用我设置的 Web 服务来返回对象列表 这个调用是这样设置的 document ready function var response ajax type POST contentType applicati
  • 0-1背包算法

    以下 0 1 背包问题是否可解 浮动 正值和 浮动 权重 可以是正数或负数 背包的 浮动 容量 gt 0 我平均有 这是一个相对简单的二进制程序 我建议用蛮力进行修剪 如果任何时候你超过了允许的重量 你不需要尝试其他物品的组合 你可以丢弃整
  • 初始化 LPCTSTR /LPCWSTR [重复]

    这个问题在这里已经有答案了 我很难理解并使其正常工作 基本上归结为我无法成功初始化这种类型的变量 它需要有说的内容7 2E25DC9D 0 USB003 有人可以解释 展示这种类型的正确初始化和类似的值吗 我已查看此站点上的所有帮助 将项目
  • C 中带有指针的结构的内存开销[重复]

    这个问题在这里已经有答案了 我意识到当我的结构包含指针时 它们会产生内存开销 这里有一个例子 typedef struct int num1 int num2 myStruct1 typedef struct int p int num2
  • Visual Studio 2017 完全支持 C99 吗?

    Visual Studio 的最新版本改进了对 C99 的支持 最新版本VS2017现在支持所有C99吗 如果没有 C99 还缺少哪些功能 No https learn microsoft com en us cpp visual cpp
  • 在 C++17 中使用 成员的链接错误

    我在 Ubuntu 16 04 上使用 gcc 7 2 并且需要使用 C 17 中的新文件系统库 尽管确实有一个名为experimental filesystem的库 但我无法使用它的任何成员 例如 当我尝试编译此文件时 include
  • C++、三元运算符、std::cout

    如何使用 C 用三元运算符编写以下条件 int condition1 condition2 condition3 int double result int or double std cout lt lt condition1 resul
  • OSError: [WinError 193] %1 不是有效的 Win32 应用程序,同时使用 CTypes 在 python 中读取自定义 DLL

    我正在尝试编写用 python 封装 C 库的代码 我计划使用 CTypes 来完成此操作 并使用 Visual Studio 来编译我的 DLL 我从一个简单的函数开始 在 Visual Studio 内的标头中添加了以下内容 然后将其构
  • 如何使用 C# 以低分辨率形式提供高分辨率图像

    尝试使用 300dpi tif 图像在网络上显示 目前 当用户上传图像时 我正在动态创建缩略图 如果创建的页面引用宽度为 500x500px 的高分辨率图像 我可以使用相同的功能即时转换为 gif jpg 吗 将创建的 jpg 的即将分辨率

随机推荐