I'm new at C++ coding, and I have this code:
std::string user_name = "raso";
// User Name
TCHAR username[UNLEN + 1];
DWORD username_len = UNLEN + 1;
GetUserName((TCHAR*)username, &username_len);
if (user_name == username)
{
std::cout << "You found the user name!";
}
It gives me an error. It basically finds the user name of the PC and compares it with the string user_name
, but user_name
is a std::string
and username
is a TCHAR*
type, right? So, how can I compare both in an if
statement?
Don't mix-and-match wide and narrow strings.
If your target name is std::string
, use an A
version of API: GetUserNameA
Also, your type casting (TCHAR*)username
was not needed.
std::string user_name = "raso";
// User Name
char username[UNLEN + 1];
DWORD username_len = UNLEN;
GetUserNameA(username, &username_len);
if (user_name == username)
{
std::cout << "You found the user name!";
}