Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

GetUserNameEx() problem

By: a guest on Feb 13th, 2012  |  syntax: C++  |  size: 1.15 KB  |  views: 192  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  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. }