"GetUserNameA":无法将参数 2 从"int"转换为"LPDWORD"

'GetUserNameA' : cannot convert parameter 2 from 'int' to 'LPDWORD'

本文关键字:int 转换 LPDWORD 参数 GetUserNameA      更新时间:2023-10-16

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

#include <windows.h>
#include <Lmcons.h>
char username[UNLEN+1];
GetUserName(username, UNLEN+1);

但是我收到此错误:"获取用户名A":无法将参数2从"int"转换为"LPDWORD"

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

因此,您应该拥有以下内容:

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);

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

您需要执行以下操作:

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

双字节参考