Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/131.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何获取当前登录用户win32 api的用户名?_C++_Windows_Winapi - Fatal编程技术网

C++ 如何获取当前登录用户win32 api的用户名?

C++ 如何获取当前登录用户win32 api的用户名?,c++,windows,winapi,C++,Windows,Winapi,我在7年前的其他线程中看到,这就是获取登录用户名的方式: #include <iostream> #include <string> #include <windows.h> #include <Lmcons.h> using namespace std; int main() { char username[UNLEN + 1]; DWORD username_len = UNLEN + 1; GetUserName(usernam

我在7年前的其他线程中看到,这就是获取登录用户名的方式:

#include <iostream>
#include <string>
#include <windows.h>
#include <Lmcons.h>
using namespace std;

int
main()
{
  char username[UNLEN + 1];
  DWORD username_len = UNLEN + 1;
  GetUserName(username, &username_len);
  std::cout << username << "\n" << &username_len;
}
#包括
#包括
#包括
#包括
使用名称空间std;
int
main()
{
字符用户名[UNLEN+1];
DWORD username_len=UNLEN+1;
GetUserName(用户名和用户名);
std::不能使用Unicode版本的函数。为此,请使用
wchar\u t
GetUserNameW

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

int
main()
{
        wchar_t username[UNLEN + 1];
        DWORD username_len = UNLEN + 1;
        GetUserNameW(username, &username_len);
        std::wcout << username << '\n';
}
#包括
#包括
#包括
int
main()
{
wchar_t用户名[UNLEN+1];
DWORD username_len=UNLEN+1;
GetUserNameW(用户名和用户名);

std::wcout你需要一个很好的windows.h编程入门。你需要
TCHAR
而不是
char
iircChange
char
到“wchar\u t”或使用
GetUserNameA
@bolov我认为在新的程序中不建议使用
TCHAR
。这是一篇老帖子上的答案,以及@bolov关于
TCHAR
至少能给你指出正确的方向吗?我的意思是,你的选择是
TCHAR
GetUsername
char
GetUsernameA
wchar\t
GetUsernameW
。好吧,这取决于
\uunicode
是否定义了其他配对,但是如果你nt若要使用任何
\u UNICODE
定义,您需要上述3种定义中的一种。根据我的记忆,是否需要解释?向下箭头上的工具提示显示:“此答案无效”.因此,猜测一下,这个答案根本没有用处。@IInspectable它可能对您没有用处,但它解决了OP的问题,这是任何答案存在的唯一原因。它解决了OP的问题,直到OP在名为的用户帐户下运行代码“@IInspectable我希望看到一些知名人士的参考资料来支持你对
TCHAR
的反对意见。在那之前,这似乎是一种关于事情应该如何发展的观点。