未管理到托管返回值 [英] Unmanaged to managed return value

查看:72
本文介绍了未管理到托管返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下托管C ++函数(VC ++ 2005(C ++ / CLI)

System :: Array ^ ManagedCppClass :: GetData()

{

BYTE * pData;

int len;

m_pureNativeCPPObj-> GetData(pData,len); //从<获取数据缓冲区br />
非托管类。


数组< byte> ^ Arr = gcnew数组< byte>(len);

System :: Runtime :: InteropServices :: Marshal :: Copy((I ntPtr)pAScan,Arr,0,

len);

返回Arr;

}


我觉得这个功能有效。

但有没有更好的方法(也许没有副本)?

-------

谢谢

Sharon

解决方案

>但是有没有更好的方法(也许没有复制)?


如果你想要返回一个托管数组,你不需要复制。


但是如果你知道你将返回一个字节数组,为什么不让

返回类型数组< byteinstead of System :: Array?

Mattias


-

Mattias Sj?gren [C#MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com

请回复到新闻组。


>" Mattias Sj ?? gren"写道:


但是如果你知道你将返回一个字节数组,为什么不使

返回类型数组< byteinstead of系统::阵列?



它会改变函数的代码吗,如果函数将如何返回数组< byteinstead of System :: Array?


-

谢谢

Sharon



" Sharon" < sh ***** @ newsgroups.nospamwrote in message

新闻:C2 ************************* ********* @ microsof t.com ...


>我有以下托管C ++函数(VC ++ 2005(C ++ / CLI))


System :: Array ^ ManagedCppClass :: GetData()

{

BYTE * pData;

int len;

m_pureNativeCPPObj-> GetData(pData,len); //从

非托管类中获取数据缓冲区。



GetData的原型是什么?看起来你应该提供

缓冲区,但pData没有被初始化。


>

数组< byte> ^ Arr = gcnew数组< byte>(len);

System :: Runtime :: InteropServices :: Marshal ::复制((I ntPtr)pAScan,Arr,0,

len);

返回Arr;

}


我认为这个功能有效。

但是有没有更好的方法(也许没有做副本)?


-------

谢谢

Sharon


I have the following managed C++ function (VC++ 2005 (C++/CLI)

System::Array^ ManagedCppClass::GetData()
{
BYTE* pData;
int len;
m_pureNativeCPPObj->GetData(pData, len); // Get the data buffer from
unmanaged class.

array<byte>^ Arr = gcnew array<byte>(len);
System::Runtime::InteropServices::Marshal::Copy((I ntPtr)pAScan, Arr, 0,
len);
return Arr;
}

I think this function works.
But is there a better way to that (maybe without doing the copy) ?
-------
Thanks
Sharon

解决方案

>But is there a better way to that (maybe without doing the copy) ?

No you need to make a copy if you want return a managed array.

But if you know that you''ll return a byte array, why not make the
return type array<byteinstead of System::Array?
Mattias

--
Mattias Sj?gren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.


>"Mattias Sj??gren" wrote:

But if you know that you''ll return a byte array, why not make the
return type array<byteinstead of System::Array?

Will it change the code of the function, and how, if the function will
return array<byteinstead of System::Array ?

--
Thanks
Sharon



"Sharon" <Sh*****@newsgroups.nospamwrote in message
news:C2**********************************@microsof t.com...

>I have the following managed C++ function (VC++ 2005 (C++/CLI)

System::Array^ ManagedCppClass::GetData()
{
BYTE* pData;
int len;
m_pureNativeCPPObj->GetData(pData, len); // Get the data buffer from
unmanaged class.

What is the prototype for GetData? That looks like you should supply the
buffer, but pData wasn''t initialized.

>
array<byte>^ Arr = gcnew array<byte>(len);
System::Runtime::InteropServices::Marshal::Copy((I ntPtr)pAScan, Arr, 0,
len);
return Arr;
}

I think this function works.
But is there a better way to that (maybe without doing the copy) ?
-------
Thanks
Sharon


这篇关于未管理到托管返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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