interop – 固定一个空数组
发布时间:2020-12-16 04:51:35 所属栏目:百科 来源:网络整理
导读:在C/C++LI中,是否可以固定不包含元素的数组? 例如 arraySystem::Byte^ bytes = gcnew arraySystem::Byte(0);pin_ptrSystem::Byte pin = bytes[0]; //-- IndexOutOfRangeException occurs here MSDN提供的建议不包括空数组的情况. http://msdn.microsoft.com
在C/C++LI中,是否可以固定不包含元素的数组?
例如 array<System::Byte>^ bytes = gcnew array<System::Byte>(0); pin_ptr<System::Byte> pin = &bytes[0]; //<-- IndexOutOfRangeException occurs here MSDN提供的建议不包括空数组的情况. 顺便说一句,您可能想知道为什么我想要固定一个空数组.简短的回答是,为了简化代码,我想对空数组和非空数组进行相同处理. 解决方法
不,而不是pin_ptr<>.你可以回到GCHandle来实现同样的目标:
using namespace System::Runtime::InteropServices; ... array<Byte>^ arr = gcnew array<Byte>(0); GCHandle hdl = GCHandle::Alloc(arr,GCHandleType::Pinned); try { unsigned char* ptr = (unsigned char*)(void*)hdl.AddrOfPinnedObject(); // etc.. } finally { hdl.Free(); } 听起来我应该使用List< Byte> ^而不是btw. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |