Hello! How correct is this function to get a username in terms of allocating and cleaning memory? Do I need to clean the memory and can there be any problems with this code? You must use pure winapi, so std :: string is not an option.

bool getCU(LPTSTR* usrBuf) { DWORD usrLen = (sizeof(usrBuf)/sizeof(*usrBuf))+1; if (GetUserName((LPTSTR)usrBuf, &usrLen)) return true; else return false; } LPTSTR User[UNLEN+1]; if (getCU(User)) MessageBox(0, (LPCTSTR)User, L"", 0); 

I understand correctly that I am passing a function to getCU a pointer to an LPTSTR array which is not stored in the function itself? Is this the right approach?

  • 3
    "so std :: string is not an option" - why? Is this an assignment or your conviction? - mega

1 answer 1

The function is completely meaningless.

  1. There is no point in sizeof(usrBuf)/sizeof(*usrBuf) to the pointer. This technique only works with arrays, not pointers.

  2. Why is LPTSTR* usrBuf ? Pointer to pointer? What does the pointer to the pointer?

  3. The transmitted pointer to the LPTSTR* usrBuf pointer LPTSTR* usrBuf force-converted to the LPTSTR type? It's pointless.

  4. LPTSTR User[UNLEN+1] is an array of pointers. What do you mean? Why did you need an array of pointers? And what's the point of passing this array of pointers to a MessageBox , forcing it to LPTSTR ?

  5. If you write your code in terms of T types, “changeers” (LP T STR), then stick to this rule everywhere. Those. not L"" , but _T("") .

In general, the code is meaningless. In some original form, he obviously spawned a sheet of error messages from the compiler. These messages were "stifled" by the arrangement of explicit type conversions. The code from this has become even more meaningless.


 bool getCU(LPTSTR usrBuf, DWORD usrLen) { return GetUserName(usrBuf, &usrLen); } ... TCHAR User[UNLEN+1]; if (getCU(User, sizeof User / sizeof *User)) MessageBox(0, User, _T(""), 0); 

But what is the whole idea? Why write a certain getCU wrapper getCU that actually does nothing but call GetUserName ?


 typedef std::basic_string<TCHAR> tstring; tstring getCU() { TCHAR usrBuf[UNLEN+1]; DWORD usrLen = sizeof usrBuf / sizeof *usrBuf; if (!GetUserName(usrBuf, &usrLen)) usrBuf[0] = 0; return tstring(usrBuf); } ... tstring User = getCU(); if (!User.empty()) MessageBox(0, User.c_str(), _T(""), 0); 
  • 2 days ago I started learning C ++. Why is LPTSTR User [UNLEN + 1] an array of pointers? Tell me how to do it right? - RemezW
  • @RemezW: Because LPTSTR is in itself an indicator type. Accordingly, LPTSTR User[UNLEN+1] is an array of pointers. - AnT
  • Thanks for clarifying! And about the memory - in this case, cleaning is not needed, right? The idea is that this function should be called many times in the program, and I didn’t want to write extra code each time (I mean declaration of buffers), but I understand that there will be no difference) - RemezW
  • @RemezW: What kind of "cleaning" are we talking about? There is no dynamic memory allocation in the code. This means that you do not need to release dynamic memory. - AnT
  • Another such question, is it possible to return the user name as a result of a function? Initially, I tried to do just that. @AnT - RemezW