将普通的c#对象传递给c++并调用该对象的方法
Pass plain C# object to C++ and call a method on that object
我想把一个c#类的对象传递给c++,并在原生c++代码中想调用该传递对象的一些方法。
Public class MyClass{
{
bool IsThisValid()
{
IsValid(this);//Check the Native passing it this object
}
[DllImport("mylibraryinCpp.dll", EntryPoint="IsValid", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall) ]
static internal extern bool IsValid(object);
}//end of class
现在我想让c++代码获取这个对象并对其执行一些操作。在本地c++代码中,我将签名设置为
//Native C++ code mylibraryinCpp.dll
extern "C" __declspec(dllexport) BOOL __stdcall IsValid (VARIANT theObject)
{
((mscorlib::_object*)(thObject.pdispVal))->get_ToString(bstrStringVal);//AccessViolationException occurs here.
}
上面的代码是工作的大多数时间,但有时它是给AccessViolationException,其他内存可能是腐败的消息。我是新的,所以不确定这是正确的方式来传递对象从c#到c++ ?谢谢你的建议
对象可能被垃圾回收器移动。通常,您只需将函数指针传递给本机代码——它可以通过JIT从。net中的委托生成——这绝对有效。这就是所谓的反向P/调用
相关文章:
- 什么时候调用组成单元对象的析构函数
- 对RValue对象调用的LValue ref限定成员函数
- CMake-按正确顺序将项目与C运行时对象文件链接
- 空基优化子对象的地址
- 将对象数组的引用传递给函数
- 你能重载对象变量名本身返回的内容吗
- C++使用整数的压缩数组初始化对象
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 将对象移动到std::shared_ptr
- 代理对象的常量正确性
- 提升 ASIO 无法识别计时器对象
- 将Ref对象作为类成员
- 将包含C样式数组的对象初始化为成员变量(C++)
- 如何返回一个类的两个对象相加的结果
- 使用std::函数映射对象方法
- 是否需要删除包含对象的"pair"?
- 如何在自删除后将对象设置为nullptr
- 迭代时从向量和内存中删除对象
- 构造对象的歧义
- 使用"std::unordereded_map"映射到"std::list"对象