GetUserNameEx Function

Hi I'm using Dev-C++ and trying to get the "GetUserNameEx" function to work. "GetUsername" works fine but this one leads to a bunch of errors when using it.

Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#define SECURITY_WIN32
#include <Security.h>
#include <iostream>

using namespace std;
int main()
{
   TCHAR Username[UNLEN+1];
   DWORD nULen = UNLEN;

   GetUserNameEx(NameDisplay, Username, &nULen); 
   cout << Username;
   system("pause");
}


when I try to compile I get "In file included from C:/Dev-Cpp/include/Security.h:38,"

lots of "ULONG_PTR/ULONG/USHORT/PVOID does not name a type"

and then lots of "expected `,' or `;' before '*' token"

and then more "does not name a type" and undeclared variables

Does anyone know the problem? thanks
(also the preview button on this site doesn't seem to work and the length says 0, so I'm not sure if this will post right)
How about defining NameDisplay.

Doc says include Secext.h.
https://msdn.microsoft.com/en-us/library/windows/desktop/ms724435%28v=vs.85%29.aspx
I thought Secext.h is included in Security.h
NameDisplay is from EXTENDED_NAME_FORMAT
Did you try #include <windows.h>
Yeah, for "GetUserName" I had
1
2
#include <windows.h>
#include <Lmcons.h> 
but for this one adding them doesn't seem to help
Being lazy?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#ifndef _UNICODE
#define _UNICODE
#define UNICODE
#endif

#define WIN32_LEAN_AND_MEAN
#include <Windows.h>

#define SECURITY_WIN32
#include <Security.h>

#include <iostream>

int main()
{
	EXTENDED_NAME_FORMAT eNameDisplay = NameFullyQualifiedDN;
	const DWORD Len = 1024;
	TCHAR szUsername[Len + 1];
	DWORD dwLen = Len;

	if (GetUserNameEx(eNameDisplay, szUsername, &dwLen))
	{
		std::wcout << "Username=" << szUsername << std::endl;
	}
}
Well now I'm getting "undefined reference to `GetUserNameExW'" for some reason
You need to link with Secur32.lib.

I don't know if your Dev-C++ understands this: #pragma comment(lib, "Secur32.lib")
Thank you!!! It works finally, I did have to add the library another way but I figured out how to do it.
Topic archived. No new replies allowed.