rjcoupe
February 25th, 2009, 05:59 AM
I'm trying - at this point - to write a fairly simple app to get the current user's username and display it in a message box. Because I want the final version of the app to do a bit more than this (baby steps, etc.) I've put the username-grabbing section in a function.
My problem lies in a fundamental non-understanding of some aspects of C++.
#define _WIN32_WINNT 0x0500
#include <windows.h>
#include <iostream>
#include <string>
using std::cout;
using std::cin;
using std::endl;
using std::cerr;
using std::string;
char getUserName()
{
char acUserName[100];
string UserName;
DWORD nUserName = sizeof(acUserName);
if (GetUserName(acUserName, &nUserName)) {
cout << "User name is " << acUserName << "." << endl;
UserName = acUserName;
}
else {
cerr << "Failed to lookup user name, error code " <<
GetLastError() << "." << endl;
}
return *acUserName;
}
int main()
{
HWND hWnd = GetConsoleWindow();
ShowWindow( hWnd, SW_HIDE );
char greeting[] = "User seems to be ";
char UserName = getUserName();
MessageBoxA(hWnd, greeting + UserName, "Title goes here.", MB_ICONWARNING);
return 0;
}
The getUserName() function works, though I'm not 100% clear on why (for example - acUserName is declared as a char, yet the return value seems to be a pointer?), but the messagebox currently just displays a single " character.
I'm sure I've made several newbie mistakes here, but if anyone could help me out and set me on my way, I'd be most grateful :)
My problem lies in a fundamental non-understanding of some aspects of C++.
#define _WIN32_WINNT 0x0500
#include <windows.h>
#include <iostream>
#include <string>
using std::cout;
using std::cin;
using std::endl;
using std::cerr;
using std::string;
char getUserName()
{
char acUserName[100];
string UserName;
DWORD nUserName = sizeof(acUserName);
if (GetUserName(acUserName, &nUserName)) {
cout << "User name is " << acUserName << "." << endl;
UserName = acUserName;
}
else {
cerr << "Failed to lookup user name, error code " <<
GetLastError() << "." << endl;
}
return *acUserName;
}
int main()
{
HWND hWnd = GetConsoleWindow();
ShowWindow( hWnd, SW_HIDE );
char greeting[] = "User seems to be ";
char UserName = getUserName();
MessageBoxA(hWnd, greeting + UserName, "Title goes here.", MB_ICONWARNING);
return 0;
}
The getUserName() function works, though I'm not 100% clear on why (for example - acUserName is declared as a char, yet the return value seems to be a pointer?), but the messagebox currently just displays a single " character.
I'm sure I've made several newbie mistakes here, but if anyone could help me out and set me on my way, I'd be most grateful :)