简体   繁体   English

查找指向本机字节数组c#的不安全字节*的长度

[英]find length of unsafe byte* pointing to native byte array c#

How can I find the length of byte* in c#? 我如何在C#中找到byte *的长度?

It's pointing to a native byte array in an unmanaged c++ library. 它指向非托管c ++库中的本机字节数组。 I need to convert it to ac# byte[], but in order to do so, I need the length. 我需要将其转换为ac#byte [],但要这样做,需要长度。 .Length doesn't work. 长度无效。

byte* ETC = //Stuff from unmanaged c++ DLL;
int ETCLength = ????

You cannot know the length of something just from a pointer; 不能仅通过指针知道某事物的长度; the pointer is just the start. 指针仅仅是开始。 Usually, a pointer and a length are passed together . 通常,一个指针和一个长度被传递到一起 In the future, this may be improved by Span<T> - or maybe it won't! 将来, Span<T>可能会对此进行改进-也许不会! Time will tell. 时间会证明一切。

You need to already know the length. 您需要已经知道长度。 This could be via an API, or it could be via documentation. 这可以通过API,也可以通过文档。 There may be a pattern to the data that implies the end ( nul terminators, for example, or the length being encoded in the first few bytes), but: that approach is how most buffer attacks start. 数据中可能有一个模式表示结尾(例如, nul终止符,或者在前几个字节中编码了长度),但是:这种方法是大多数缓冲区攻击的开始方式。 You should always know the length if you're talking about pointers. 如果您在谈论指针,则应该始终知道长度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM