获取对象Functionallity出的C ++代码在C# [英] Getting Object Functionallity out of C++ code in C#

查看:85
本文介绍了获取对象Functionallity出的C ++代码在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经有一个函数在C ++调用在旧的lib某些功能写道。这个函数创建一些内存,使通话和破坏内存。为了优化这个我想创建一个对象,会继续分配,直到该对象被破坏的内存。但是我要被调用从C#这个功能,不相信我可以导出类,只是函数或变量

I have have a function in wrote in C++ that calls some functions in a old lib. This function creates some memory makes the calls and destroys the memory. To optimize this I would create an object that would keep the memory allocated until the object was destroyed. However I'm going to be calling this function from C# and don't believe I can export a Class, just functions or variables.

我的想法是代替本;想想DLL作为一个阶级和使用本地变量的dll域内指向内存。然后有一个函数来创建存储,调用工人功能和另一个当与DLL进行破坏内存。

My idea is instead this; Think of the DLL as a class and the use local vars inside the scope of the dll to point to memory. Then have a function to create the memory, call the worker functions and another to destroy the memory when done with the DLL.

这是一个好方法?有没有更好的办法?

Is this a good approach? Is there a better way?

推荐答案

我喜欢写在C ++ / CLI(原托管C ++)托管的包装,因为它使得它更容易明确你要使用C ++侧管理/非托管的互操作性的东西,你的C#没有得到污染带的p / Invoke样式代码。

I prefer to write a managed wrapper in C++/CLI (formerly Managed C++), as it makes it much easier to explicitly do what you want with managed/unmanaged interoperability on the C++ side, and your C# doesn't get polluted with P/Invoke style code.

修改只注意到你的评论的但是我将要调用从C#这个功能,不相信我可以导出类,只是函数或变量。

Edit Just noticed your comment "However I'm going to be calling this function from C# and don't believe I can export a Class, just functions or variables."

这是不完全正确 - C#的可以导入全类从C ++ / CLI代码生成的汇编

That's not entirely true - C# can import full classes from an assembly generated from C++/CLI code.

这篇关于获取对象Functionallity出的C ++代码在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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