469,280 Members | 1,823 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,280 developers. It's quick & easy.

get username as string (windows c++)

134 100+
how could i get the username as a string? this gets it as "char", but i need it as a string.
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <windows.h>
  3. using namespace std;
  4. int main()
  5. {
  6.     char acUserName[100];
  7.     DWORD nUserName = sizeof(acUserName);
  8.     if (GetUserName(acUserName, &nUserName)) {
  9.         cout << "User name is " << acUserName << "." << endl;
  10.         cin.get();
  11.     }
  12.     return 0;
  13. }
  14.  
thanks
May 13 '07 #1
7 43464
ilikepython
844 Expert 512MB
how could i get the username as a string? this gets it as "char", but i need it as a string.
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <windows.h>
  3. using namespace std;
  4. int main()
  5. {
  6.     char acUserName[100];
  7.     DWORD nUserName = sizeof(acUserName);
  8.     if (GetUserName(acUserName, &nUserName)) {
  9.         cout << "User name is " << acUserName << "." << endl;
  10.         cin.get();
  11.     }
  12.     return 0;
  13. }
  14.  
thanks
A char array is the same as a string except it has a '\0' terminator. If you want to construct a char array to a string, try this:
Expand|Select|Wrap|Line Numbers
  1. string userName(acUserName);
  2.  
Why would you want it as a string?
May 13 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
Maybe like this:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <windows.h>
  3. using namespace std;
  4. #include <string>
  5. int main()
  6. {
  7.     char acUserName[100];
  8.     string UserName;
  9.     DWORD nUserName = sizeof(acUserName);
  10.     if (GetUserName(acUserName, &nUserName)) {
  11.         UserName = acUserName;
  12.         cout << "User name is " << UserName << "." << endl;
  13.         cin.get();
  14.     }
  15.     return 0;
  16. }
  17.  
May 13 '07 #3
lumpybanana247
134 100+
i changed my code a little bit and i now got the problem

cannot convert 'std::basic_string<char, std::char_traits<char>, std::allocator<char> >' to 'char*' for argument '1' to 'bool EmptyDirectory(char*)'

Expand|Select|Wrap|Line Numbers
  1. ON_COMMAND_BY           (delhistory){
  2.     char acUserName[100];
  3.        string UserName;
  4.        DWORD nUserName = sizeof(acUserName);
  5.        if (GetUserName(acUserName, &nUserName))
  6.        UserName = acUserName;     
  7.        list.add("History");if( EmptyDirectory( "C:\\Documents and Settings\\"+UserName+"\\Local Settings\\History\\History.IE5\\" ) != 0 ){delhistory.disable();}else{}}
  8.  

[i want it as a string so i can add it as part of '"C:\\Docu...."+UserName+"...."

thanks,
nate
May 13 '07 #4
ilikepython
844 Expert 512MB
i changed my code a little bit and i now got the problem

cannot convert 'std::basic_string<char, std::char_traits<char>, std::allocator<char> >' to 'char*' for argument '1' to 'bool EmptyDirectory(char*)'

Expand|Select|Wrap|Line Numbers
  1. ON_COMMAND_BY           (delhistory){
  2.     char acUserName[100];
  3.        string UserName;
  4.        DWORD nUserName = sizeof(acUserName);
  5.        if (GetUserName(acUserName, &nUserName))
  6.        UserName = acUserName;     
  7.        list.add("History");if( EmptyDirectory( "C:\\Documents and Settings\\"+UserName+"\\Local Settings\\History\\History.IE5\\" ) != 0 ){delhistory.disable();}else{}}
  8.  

[i want it as a string so i can add it as part of '"C:\\Docu...."+UserName+"...."

thanks,
nate
That means that the EmptyDirectory() function takes a pointer to a character or a char array. Maybe you don't want a string after all.
You can try two things:
Make everything char arrays and use strcpy():
Expand|Select|Wrap|Line Numbers
  1. char path[200];
  2. strcpy(path, "C:\\Documents and Settings\\");
  3. strcat(path, acUserName);
  4. strcat(path, "\\Local Settings etc....");
  5. if (EmptyDirectory(path) != 0){
  6. //do stuff
  7. }
  8.  
or you can add .c_str() at the end of the whole path:
Expand|Select|Wrap|Line Numbers
  1. if( EmptyDirectory( "C:\\Documents and Settings\\"+UserName+"\\Local Settings\\History\\History.IE5\\".c_str()) != 0 )
  2. //do stuff
  3.  
Try it and see what happens.
May 13 '07 #5
lumpybanana247
134 100+
IGNORE!!

i tried
Expand|Select|Wrap|Line Numbers
  1.   ON_COMMAND_BY           (delhistory)  {list.add("History");
  2.   char acUserName[100];
  3.        string UserName;
  4.        DWORD nUserName = sizeof(acUserName);
  5.        if (GetUserName(acUserName, &nUserName))
  6.        UserName = acUserName;     
  7.                                         if( EmptyDirectory( "C:\\Documents and Settings\\"+UserName+"\\Local Settings\\History\\History.IE5\\".c_str() ) != 0 )
  8.                                         {delhistory.disable();}else{}}
  9.  

and i got the error:
request for member 'c_str' in '"\\Local Settings\\History\\History.IE5\\"', which is of non-class type 'const char [37]'


ill try the str copy way and post another message
May 13 '07 #6
lumpybanana247
134 100+
whoopee!!

Expand|Select|Wrap|Line Numbers
  1.   ON_COMMAND_BY           (delhistory)  {list.add("History");
  2.   char acUserName[100];
  3.        string UserName;
  4.        DWORD nUserName = sizeof(acUserName);
  5.        if (GetUserName(acUserName, &nUserName))
  6.        UserName = acUserName;     
  7.  
  8. char path[200];
  9. strcpy(path, "C:\\Documents and Settings\\");
  10. strcat(path, acUserName);
  11. strcat(path, "\\Local Settings\\History\\");
  12. if (EmptyDirectory(path) != 0){
  13.  
  14.                                         delhistory.disable();}else{}}
  15.  
worked!

thank you very much
May 13 '07 #7
ilikepython
844 Expert 512MB
whoopee!!

Expand|Select|Wrap|Line Numbers
  1.   ON_COMMAND_BY           (delhistory)  {list.add("History");
  2.   char acUserName[100];
  3.        string UserName;
  4.        DWORD nUserName = sizeof(acUserName);
  5.        if (GetUserName(acUserName, &nUserName))
  6.        UserName = acUserName;     
  7.  
  8. char path[200];
  9. strcpy(path, "C:\\Documents and Settings\\");
  10. strcat(path, acUserName);
  11. strcat(path, "\\Local Settings\\History\\");
  12. if (EmptyDirectory(path) != 0){
  13.  
  14.                                         delhistory.disable();}else{}}
  15.  
worked!

thank you very much
You're welcome.
May 13 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

14 posts views Thread by Brent Burkart | last post: by
11 posts views Thread by Kirk | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.