获取delphi中的指针缓冲区的大小 [英] Get size of a pointer buffer in delphi

查看:85
本文介绍了获取delphi中的指针缓冲区的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取一个 WriteFile 缓冲区的大小,以了解必须在缓冲区中写入多少数据.缓冲区数据类型为Pointer Buffer:Pointer ,我尝试使用 SizeOf(Buffer) SizeOf(@Buffer) SizeOf 不返回'WriteFile'接收到的缓冲区的大小,它只是返回'Pointer'数据类型(8)的大小.

I want to get the size of a WriteFile Buffer to know how much data I must write in a buffer. the buffer datatype is Pointer Buffer:Pointer, i try to use SizeOf(Buffer) or SizeOf(@Buffer) but SizeOf does not return size of buffer received by 'WriteFile' , it simply return size of 'Pointer' data type (8).

我该怎么办?

{对不起我的英语,请问}

{Excuse me for my bad English}

推荐答案

如果所有缓冲区都是指向该缓冲区的指针,则无法检索该缓冲区的大小.您必须独立于指针来跟踪缓冲区大小.

You cannot retrieve the size of a buffer if all you have is a pointer to it. You must keep track of the buffer size independently of the pointer.

执行此操作的一种常见方法是将大小保留在与指针一起存储的单独变量中.将大小传递给需要它的任何函数.

A common way to do this is to hold the size in a separate variable that you store along with the pointer. Pass the size on to any function that needs it.

执行此操作的另一种方法是使用动态数组.编译器和运行时会自动跟踪动态数组的长度,可以使用 Length 进行查询.您可以通过简单的强制转换获得指向缓冲区的指针:

Another way to do this is to use a dynamic array. The compiler and runtime keep track of the length of dynamic arrays automatically, and this can be queried using Length. You can get a pointer to the buffer with a simple cast:

var
  Buffer: TBytes; // dynamic array of byte
....
Buffer := ...; // initialize
WriteBuffer(Pointer(Buffer), Length(Buffer));

这篇关于获取delphi中的指针缓冲区的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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