gpt4 book ai didi

delphi - 提供缓冲区来接收 Out LPWSTR 值

转载 作者:行者123 更新时间:2023-12-02 23:04:49 25 4
gpt4 key购买 nike

我很少直接使用Windows API,但我遇到了一种不可避免的情况,我不知道如何处理它。

MS API 文档引用了 API 调用的参数,例如

out Something LPWSTR

所以我显然必须提供一个缓冲区来接收它,但是我应该如何知道要提供多大的缓冲区?我想我可以尝试在 Something 上调用 StrLen,但这似乎意味着我需要调用 API 两次,一次是为了获取长度,第二次是为了实际检索结果,并且长度可能在此期间发生了变化。

OTOH,我可以预先分配缓冲区(例如作为本地 WideChar 数组),但这似乎有点不切实际,因为返回的字符串可以是任意长度(?)。

那么,我的q是如何正确接收一个Out LPWSTR的值呢?另外,作为补充,我如何找到它的编码(或者 Win API 是否始终使用相同的编码,如果是的话,是哪一个?)

我正在询问 Delphi 的后 Unicode 版本。

最佳答案

在等待您对评论的回复时:请考虑一下 GetTempPathW() 函数。是documented如下

DWORD GetTempPathW(
DWORD nBufferLength,
LPWSTR lpBuffer
);

nBufferLength

The size of the string buffer identified by lpBuffer, in TCHARs.

lpBuffer

A pointer to a string buffer that receives the null-terminated string specifying the temporary file path.

Return value

If the function succeeds, the return value is the length, in TCHARs, of the string copied to lpBuffer, not including the terminating null character. If the return value is greater than nBufferLength, the return value is the length, in TCHARs, of the buffer required to hold the path.

所以,是的,如果您想分配正确长度的缓冲区,您需要首先使用 nBufferLength = 0 调用它,或者足够小。然后使用返回值分配缓冲区并再次调用该函数。

关于delphi - 提供缓冲区来接收 Out LPWSTR 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58628671/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com