Advertisement
Guest User

GetUserNameEx() problem

a guest
Feb 13th, 2012
432
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.15 KB | None | 0 0
  1. // Description:
  2. //   Function gets user name.
  3. void CollectUserName() {
  4.     // Name format, we need the longest, most descriptive one.
  5.     EXTENDED_NAME_FORMAT name_format = NameSamCompatible;
  6.  
  7.     // Local memory pointer to store user name.
  8.     TCHAR *user_name = NULL;
  9.  
  10.     // This will hold user_name size in TCHARs on input and required size
  11.     // of user_name on output.
  12.     ULONG user_name_size = 1;
  13.  
  14.     ULONG required_size;
  15.    
  16.     // Will give GetUserNameEx() minimal buffer. Not sure if it will accept
  17.     // 0-length buffer (this case is not documented).
  18.     user_name = new (nothrow) TCHAR[1];
  19.  
  20.     HRESULT result = GetUserNameEx(name_format, user_name, &user_name_size);
  21.  
  22.     required_size = user_name_size;
  23.  
  24.     delete [] user_name;
  25.  
  26.     user_name = NULL;
  27.  
  28.     // Allocating block of memory of the required size.
  29.     user_name = new (nothrow) TCHAR[user_name_size];
  30.  
  31.     result = GetUserNameEx(name_format, user_name, &user_name_size);
  32.  
  33.     cout << required_size << user_name_size;
  34.     // Output: 22 11 - for Character Encoding set to "Not Set"
  35.     //         11 11 - for Character Encoding set to "Use Unicode Chracter Set"
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement