C#库的C ++/CLI包装器 [英] C++/CLI Wrapper for C# Library

查看:80
本文介绍了C#库的C ++/CLI包装器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我是C ++/CLI的新手.我正在使用C ++/CLI为托管对象创建包装器
图书馆.我几乎成功创建了它.

这就是它的工作方式.

本机C ++-> (通话)-> C ++/CLI包装器-> (通话)-> C#库并返回结果.


包装标头是纯C ++文件.我正在使用静态链接在本地库中加载包装器.

标题看起来像这样...

Hi,

I am newbiew in C++/CLI. I am using C++/CLI to create a wrapper for a managed
library. I almost successfull in creating it.

This is how it works.

Native C++ -> (Calls) -> C++/CLI Wrapper -> (Calls) -> C# library and returns the result back.


The wrapper header is a pure C++ file. I am loading Wrapper in Native library using static linking.

The header looks like this...

class NATIVEDLL_API Wrapper
{
public:
   // constructor
   Wrapper(LPCTSTR uid);
   virtual ~Wrapper();

   int GetInfo(MyStruct* struct_out);
};

struct MyStruct
{
	int length;
	MyEnum enum;
}


我正在使用此MyStruct(它是一个结构)作为out参数.它对我有用,函数返回后结构被填充.

但是,当我们尝试连续调用此方法约1000次时,它将在该函数内失败,而没有任何异常.

发生这种情况是因为指针已被GC清除了吗?您能否建议一种更好的方法来编写此方法.


谢谢
Basanth


I am using this MyStruct (it''s a struct) as a out parameter. It works for me, struct gets filled after the function returns.

But when we try to call this method continuously for about 1000 times it fails inside this function without any exception.

Is this happens because of the pointer is getting cleaned by GC? Can you suggest a better way to write this method.


Thanks
Basanth

推荐答案

//calling API
String^ uid = gcnew String( "...." );
// Marshal the string to pointer before passing to native API
Wrapper* pWrapper = new Wrapper( static_cast<TCHAR*>(Marshal::StringToHGlobalUni(uid).ToPointer());

MyStruct* pStruct = new MyStruct();
Wrapper->GetInfo( pStruct );

// After usage delete the poinetrs
delete pStruct;
pStruct = 0;

delete pWrapper;
pWrapper = 0;



这可能对您有帮助



This may help you


这篇关于C#库的C ++/CLI包装器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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