Here is supposedly the code to get the name of the current user:
char acUserName[100];
DWORD nUserName = sizeof(acUserName);
if (GetUserName(acUserName, %26amp;nUserName))
{
cout %26lt;%26lt; "User name is " %26lt;%26lt; acUserName %26lt;%26lt; "." %26lt;%26lt; endl;
}
else
{
cerr %26lt;%26lt; "Failed to lookup user name, error code " %26lt;%26lt;
GetLastError() %26lt;%26lt; "." %26lt;%26lt; endl;
}
But when I compile it, I get this error:
error C2664: 'GetUserNameW' : cannot convert parameter 1 from 'char [100]' to 'LPWSTR'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Once this error is fixed, I would then like to use it to place into the string of a file path. For example: "C:\Username\Documents and Settings"
C++: get the name of the user, then insert it into a string?
I agree with Einstein:
This compiles and runs fine:
#include %26lt;iostream.h%26gt;
#include %26lt;windows.h%26gt;
int main()
{
char acUserName[100];
DWORD nUserName = sizeof(acUserName);
if (GetUserName(acUserName, %26amp;nUserName)) {
cout %26lt;%26lt; "User name is " %26lt;%26lt; acUserName %26lt;%26lt; "." %26lt;%26lt; endl;
}
else {
cerr %26lt;%26lt; "Failed to lookup user name, error code " %26lt;%26lt;
GetLastError() %26lt;%26lt; "." %26lt;%26lt; endl;
}
return 0;
}
The only difference can be the includes.
Reply:Check out http://www.pscode.com for great source code samples - I don't do C++ (I'm a VB programmer) but I use the site all the time.
Reply:You have a problem with your #include directives. Apparently you are mixing the old iostream headers with the Standard C++ Library headers. This causes the compiler error C2664.
Mixing the old iostream headers with the Standard C++ Library headers is not recommended.
----
I don't know if the below work-around will fix the problem...
Try changing:
cout %26lt;%26lt; "User name is" %26lt;%26lt; acUserName %26lt;%26lt; "." %26lt;%26lt; end1;
To
std::cout %26lt;%26lt; "User name is" %26lt;%26lt; acUserName %26lt;%26lt; "." %26lt;%26lt; end1;
Reply:string name; //declares a string
cin%26gt;%26gt;name; //inputs a name
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment