从C#中的C ++代码中获取对象功能 [英] Getting Object Functionality out of C++ code in C#
问题描述
我有一个用C ++编写的函数,该函数在旧的lib中调用某些函数.该函数创建一些使调用发生的内存并销毁该内存.为了优化这一点,我将创建一个对象,该对象将保持分配的内存,直到该对象被销毁为止.但是我将要从C#调用此函数,并且不相信我可以导出Class,而只能导出函数或变量.
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范围内使用本地var指向内存.然后,有了一个函数来创建内存,调用辅助函数,并在使用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(以前称为Managed 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#调用此函数,并且不相信我可以导出Class,而只能导出函数或变量."
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.
这篇关于从C#中的C ++代码中获取对象功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!