【问题标题】:'GetUserNameA' : cannot convert parameter 2 from 'int' to 'LPDWORD'“GetUserNameA”:无法将参数 2 从“int”转换为“LPDWORD”
【发布时间】:2013-09-17 14:47:35
【问题描述】:

我试过https://stackoverflow.com/a/11587467/2738536

#include <windows.h>
#include <Lmcons.h>

char username[UNLEN+1];
GetUserName(username, UNLEN+1);

但我收到此错误:“GetUserNameA”:无法将参数 2 从“int”转换为“LPDWORD”

【问题讨论】:

  • 阅读docs。这是一个输入和输出参数。

标签: c++


【解决方案1】:

根据documentation,您传入的长度必须是一个指向双字的指针,因为函数会根据返回的内容对其进行更改。

因此你应该有类似的东西:

TCHAR username[UNLEN+1];       // TCHAR to allow for MBCS and Unicode
DWORD len = UNLEN + 1;         //   if you're in to that sort of thing :-)
GetUserName(username, &len);

【讨论】:

  • 我必须停止回答问题,直到找到要包含的文档链接(以及这些文档的引用)。 :-) 每次都让我慢下来。我必须学会先发布答案,然后然后编辑以稍后添加链接。 :-)
  • "你传入的长度必须是一个指向双字的指针" 哪里说应该写"&len"
  • @user10056,它在我的文本中的“指针”一词和 MSDN 上的类型定义 LPDWORD(长 指针 到双字)中说明。 &amp;len 表示(指向)len 的地址。
【解决方案2】:

LPDWORD 类型实际上是一个指针。

您需要执行以下操作:

char username[UNLEN + 1];
DWORD name_length = ULEN + 1;
GetUserName(username, &name_length);

DWORD reference

【讨论】:

  • "LPDWORD 类型实际上是一个指针。"哪里写的?
  • typedef unsigned long DWORD, *PDWORD, *LPDWORD; LPWORD 前面的 * 使它成为一个指针。
猜你喜欢
  • 1970-01-01
  • 2015-06-12
  • 2018-11-25
  • 1970-01-01
  • 2014-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-09
相关资源
最近更新 更多