在Windows上使用C ++获取当前用户名 [英] Get current username in C++ on Windows
本文介绍了在Windows上使用C ++获取当前用户名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图创建一个程序,使用C ++在Windows上检索当前用户的用户名。
I am attempting to create a program that retrieves the current user's username on Windows using C++.
我试过:
char *userName = getenv("LOGNAME");
stringstream ss;
string userNameString;
ss << userName;
ss >> userNameString;
cout << "Username: " << userNameString << endl;
除了用户名:,不输出任何内容。
Nothing is outputted except "Username:".
最简单,最好的方法是获取当前用户名?
What is the simplest, best way to get the current username?
推荐答案
使用Win32API GetUserName
函数。示例:
Use the Win32API GetUserName
function. Example:
#include <windows.h>
#include <Lmcons.h>
char username[UNLEN+1];
DWORD username_len = UNLEN+1;
GetUserName(username, &username_len);
这篇关于在Windows上使用C ++获取当前用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文