Click to See Complete Forum and Search --> : C++ Newbie - Hopefully a simple question


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 :)

zerver
February 25th, 2009, 06:24 AM
Hi!

I have marked your problem in bold below. The function returns "char", which is a single character. You need to return a pointer (char *) or use a string class like CString or std::string.

I suggest you return the UserName variable, which is a string already. If you decide to return char * instead, please note that acUserName is a local variable in your function, and therefore cannot be returned unless you make it static.


char getUserName()
{
char acUserName[100];
...
return *acUserName;
}

int main()
{
...
char UserName = getUserName();
...
}

rjcoupe
February 25th, 2009, 06:44 AM
Zerver: thanks for the quick and helpful response :)

I've edited the code with your advice in mind:

string getUserName()
{
...
return UserName;
}


int main()
{
HWND hWnd = GetConsoleWindow();
ShowWindow( hWnd, SW_HIDE );
string greeting = "User seems to be ";
string UserName = getUserName();

MessageBoxA(hWnd, greeting + UserName, "Title goes here.", MB_ICONWARNING);

return 0;
}

The compiler complains (at least, I think this is what it's complaining about) that MessageBoxA wants a 'const CHAR *' for its second argument, rather than a string:

cannot convert `std::basic_string<char, std::char_traits<char>, std::allocator<char> >' to `const CHAR*' for argument `2' to `int MessageBoxA(HWND__*, const CHAR*, const CHAR*, UINT)'

zerver
February 25th, 2009, 08:34 AM
Try this:

MessageBoxA(hWnd, (greeting + UserName).c_str(), "Title goes here.", MB_ICONWARNING);

rjcoupe
February 25th, 2009, 09:28 AM
That works - thank you!

I had a look at an explanation of the c_str() method - definitely sounds like one to remember for exactly this kind of use. Thanks again :)