结合函数、绑定、c++ 和托管代码
Combining function, bind, c++ and managed code
我有一个 c++ 函数,它期望将函数对象(AuthenticateNotifyFunc)传递给它:
class lc_Authenticate
{
public:
typedef enum {
kAbort,
kContinue
} lc_AuthenticateStatus;
typedef std::tr1::function<lc_AuthenticateStatus (const string &msg)> AuthenticateNotifyFunc;
bool Authenticate(lc_AuthenticateParams ¶ms,
AuthenticateNotifyFunc notifyFunc);
}
在一个托管的 c++ 项目中,我试图定义一个参数来传递给上面的函数,这样:
public ref class Form1 : public System::Windows::Forms::Form
{
public:
lc_Authenticate::lc_AuthenticateStatus UpdateStatus(const string &msg)
{
<<DO SOMETHING>>
return(lc_Authenticate::kContinue);
}
void test()
{
string appKey, appSecret;
appKey = GetString(this->appKeyTextBox->Text);
appSecret = GetString(this->appSecretTextBox->Text);
lc_Authenticate dbauth;
lc_AuthenticateParams params(appKey, appSecret);
// DOESN'T COMPILE won't let me take address of member function
// or know about _1
lc_Authenticate::AuthenticateNotifyFunc func =
std::tr1::bind(&Form1::UpdateStatus, this, _1);
dbauth.Authenticate(params, func);
}
};
所以我正在尝试实现一种将函数传递给 c++ 方法的通用方法,这样它就不在乎传递的函数是静态函数还是成员函数。而且我不清楚如何从托管代码中执行此操作。
根据设计,不能绑定到托管类的实例方法。 垃圾回收器在压缩堆时移动对象,导致此更改。 您需要使用托管委托。 因此,您无法避免使用本机帮助程序类,该类提供函数所需的稳定回调<>。 你可以从那里回到托管代码 Marshal::GetFunctionPointerForDelegate()。
相关文章:
- 如何在 c++ 非托管代码中反序列化 byte[] 的 json 字符串?
- 如何使用指针直接从托管代码中的非托管代码中读取矢量数据
- 异步接口托管 - > 非托管代码
- 从C++(非托管代码)检索数组到 C 尖锐形式(托管)
- 从本机代码返回到托管代码会损坏返回的对象
- 将C++非托管代码转换为 C#
- 将托管事件处理程序传递给 Linux 中的非托管代码
- 访问在托管代码创建的事件上对WaitforsingLeoBject拒绝
- 使用 P/Invoke 时存储非托管代码的数据
- Windows 服务导入C++非托管代码
- 将字符串数组从托管代码编组到本机代码
- P/从获取"tried to access protected memory error"调用非托管C++代码
- 将数据从非托管代码传递到托管代码
- 为使用C# /托管代码的无注册COM Interop设置DLL
- 从无操纵项目(C/C VS2005)调用托管代码(C#/Visual Basic VS2010)
- 从 VB.NET 调用非托管代码C++ dll
- 当C#中的字节数组传递给结构中的非托管代码时,会发生什么情况
- 在托管代码和非托管代码之间传递非托管结构的安全数组
- 如何调试C++非托管代码中的较低级别文件访问异常/崩溃
- 自定义Direct2D呈现托管代码和非托管代码之间的批封送处理