gpt4 book ai didi

c++ - GetUserNameEx() 返回的缓冲区大小

转载 作者:太空狗 更新时间:2023-10-29 21:05:41 25 4
gpt4 key购买 nike

我有一个关于调用 GetUserNameEx() 时奇怪的返回大小的问题。也许有人已经遇到过这个问题。

我调用了 GetUserNameEx() 两次:第一次获取所需的缓冲区大小,第二次实际填充缓冲区。

documentation您可以阅读有关大小参数的信息:

  1. >

    If lpNameBuffer is too small, the function fails and GetLastError returns ERROR_MORE_DATA. This parameter receives the required buffer size, in Unicode characters (whether or not Unicode is being used), including the terminating null character.

    我保存此值 (required_size) 以检查成功返回的值。

  2. >

    On input, this variable specifies the size of the lpNameBuffer buffer, in TCHARs. If the function is successful, the variable receives the number of TCHARs copied to the buffer, not including the terminating null character.

    这个是returned_size

奇怪的是,如果我在项目属性中将字符编码设置为未设置,则required_size为22,returned_size为11。

当我将字符编码设置为使用 Unicode 字符集时,required_size 为 11,returned_size 也为 11。

也许我没有得到什么,但我希望 required_size 不会改变。有人已经看到了吗?

谢谢。

UPD:这里是 code example .

最佳答案

我复制。好吧,这是一个错误。它永远不会出错,因为它要求的缓冲区大小是所需大小的两倍。您将在第二次调用时获得复制到缓冲区的实际字符数,因此您确实知道字符串的实际大小。我不会尝试修复它,它在将字符串从 Unicode 转换为 mbcs 之前实际使用缓冲区的可能性很小。尽管我看不到任何证据。

关于c++ - GetUserNameEx() 返回的缓冲区大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9260151/

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