I am developing an application. One of the methods needs to capture the computer name and user logged on the machine, then display both to the user. I need it to run on both Win
If you can use Boost, you can do this to easily get the host name:
#include <boost/asio/ip/host_name.hpp>
// ... whatever ...
const auto host_name = boost::asio::ip::host_name();
Windows
You can try to use GetComputerName
and GetUserName
, here is a example:
#define INFO_BUFFER_SIZE 32767
TCHAR infoBuf[INFO_BUFFER_SIZE];
DWORD bufCharCount = INFO_BUFFER_SIZE;
// Get and display the name of the computer.
if( !GetComputerName( infoBuf, &bufCharCount ) )
printError( TEXT("GetComputerName") );
_tprintf( TEXT("\nComputer name: %s"), infoBuf );
// Get and display the user name.
if( !GetUserName( infoBuf, &bufCharCount ) )
printError( TEXT("GetUserName") );
_tprintf( TEXT("\nUser name: %s"), infoBuf );
see: GetComputerName and GetUserName
Linux
Use gethostname
to get computer name(see gethostname), and getlogin_r
to get login username. You can look more information at man page of getlogin_r.
Simple usage as follows:
#include <unistd.h>
#include <limits.h>
char hostname[HOST_NAME_MAX];
char username[LOGIN_NAME_MAX];
gethostname(hostname, HOST_NAME_MAX);
getlogin_r(username, LOGIN_NAME_MAX);
On POSIX systems you can use the gethostname and getlogin functions, both declared in unistd.h
.
/*
This is a C program (I've seen the C++ tag too late). Converting
it to a pretty C++ program is left as an exercise to the reader.
*/
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int
main()
{
char hostname[HOST_NAME_MAX];
char username[LOGIN_NAME_MAX];
int result;
result = gethostname(hostname, HOST_NAME_MAX);
if (result)
{
perror("gethostname");
return EXIT_FAILURE;
}
result = getlogin_r(username, LOGIN_NAME_MAX);
if (result)
{
perror("getlogin_r");
return EXIT_FAILURE;
}
result = printf("Hello %s, you are logged in to %s.\n",
username, hostname);
if (result < 0)
{
perror("printf");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
Possible output:
Hello 5gon12eder, you are logged in to example.com.
This seems safer than relying on environment variables which are not always present.
I'm withdrawing that last statement because
getlogin
actually discourages its usage in favour of getenv("LOGIN")
andgetlogin_r
call in the above program fails with ENOTTY
when I run the program from within Emacs instead of an interactive terminal while getenv("USER")
would have worked in both situations.Use gethostname()
to get computer name, support both windows and linux.
in Linux you can also use the following using Posix library to retrieve the real user that owns the process: getuid() returns the real user ID of the calling process. see getuid man page
#include <pwd.h>
string userName = "unknownUser";
// Structure to store user info
struct passwd p;
// Get user ID of the application
uid_t uid = getuid();
// Buffer that contains password additional information
char pwdBuffer[ourPwdBufferSize];
// Temporary structure for reentrant function
struct passwd* tempPwdPtr;
if ((getpwuid_r(uid, &p, pwdBuffer, sizeof(pwdBuffer),
&tempPwdPtr)) == 0) {
userName = p.pw_name;
}
Regarding Denis's answer, note that getenv("HOSTNAME")
for Linux may not always work because the environment variables may not be exported to the program.
Multi-platform C++ code example to fetch just the computer name (this is what worked for my Win7 and CentOS machines):
char *temp = 0;
std::string computerName;
#if defined(WIN32) || defined(_WIN32) || defined(_WIN64)
temp = getenv("COMPUTERNAME");
if (temp != 0) {
computerName = temp;
temp = 0;
}
#else
temp = getenv("HOSTNAME");
if (temp != 0) {
computerName = temp;
temp = 0;
} else {
temp = new char[512];
if (gethostname(temp, 512) == 0) { // success = 0, failure = -1
computerName = temp;
}
delete []temp;
temp = 0;
}
#endif