0

I tried https://stackoverflow.com/a/11587467/2738536

#include <windows.h>
#include <Lmcons.h>

char username[UNLEN+1];
GetUserName(username, UNLEN+1);

But I got this error: 'GetUserNameA' : cannot convert parameter 2 from 'int' to 'LPDWORD'

Community
  • 1
  • 1
user10056
  • 63
  • 4
  • 12
  • Read the [docs](http://msdn.microsoft.com/en-us/library/windows/desktop/ms724432(v=vs.85).aspx). It's an input and output parameter. – chris Sep 13 '13 at 02:41

2 Answers2

3

As per the documentation, the length you pass in has to be a pointer to a double-word, because the function changes it based on what's returned.

Hence you should have something like:

TCHAR username[UNLEN+1];       // TCHAR to allow for MBCS and Unicode
DWORD len = UNLEN + 1;         //   if you're in to that sort of thing :-)
GetUserName(username, &len);
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • 1
    I *must* stop holding back on answers until I find the documentation link to include (and a quote from those docs). :-) It slows me down every time. I have to learn to post the answer first and *then* edit to add the links later. :-) – Ken White Sep 13 '13 at 02:45
  • "the length you pass in has to be a pointer to a double-word" Where is it stated that you should write "&len" – user10056 Sep 13 '13 at 02:49
  • @user10056, it stated in the word "pointer" in my text and in the type definition `LPDWORD` (long _pointer_ to double-word) on MSDN. `&len` means the address of (pointer to) `len`. – paxdiablo Sep 13 '13 at 02:51
0

The type LPDWORD is actually a pointer.

You need to do something like the following:

char username[UNLEN + 1];
DWORD name_length = ULEN + 1;
GetUserName(username, &name_length);

DWORD reference

BinderNews
  • 570
  • 3
  • 10