473,394 Members | 1,960 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 46459
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

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

Similar topics

2
by: hplloyd | last post by:
Hi I want to pick up the name of the logged in windows user for my ASP.NET application. When I use System.Environment.UserName i get ASPNET which is ofcourse not what I want when I try...
3
by: William Stacey [MVP] | last post by:
Is this a bug in fx2? I expect they did not carry forward all the StartInfo properties to api used with Username/Password. When you remove UserName/password, the window does not show as expected....
14
by: Brent Burkart | last post by:
I am trying to capture the Windows Authenticated username, but I want to be able to capture the login name that exists in IIS, not Windows. In order to enter my company's intranet through the...
5
by: nikou_70 | last post by:
I have a problem with ("auth_user") in asp,I try to use windows username and password in asp page for limitation user access to pages, but this server variable returns empty string, can you help me...
0
by: Paul | last post by:
Hi, I'm trying to kick off the iiscnfg.vbs from a webservice to export a website's config to an xml file (And eventually populate other servers with the config). I initially tried this using the...
8
by: Philip Wagenaar | last post by:
I need to send printjobs to a printqueue under diffrent usernames. The printsoftware on the queue is not very 'secure' so I can create a user on 1 system and send a printjob under it's name to the...
11
by: Kirk | last post by:
The following C# web service works fine until you uncomment the lines setting UserName and Password. Then the process starts as the specified user, but hangs in a suspended state. In fact, any...
0
by: Kirk | last post by:
The following C# web service works fine until you uncomment the lines setting UserName and Password. Then, Process.Start throws an Access is Denied Exception. This is with .NET 2.0, of course...
0
by: None | last post by:
Hi, I have written a c# code to find the given username and password is correct or not. (i.e the given username and password will be checked against the active directory) But i don't know how...
8
by: tiijnar | last post by:
Hi, To get windows username Im using the following code. public class GetWindowUsername { public String getUser() { String userName = System.getProperty("user.name");...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.