Get Username in C – Unicode

You will need to include the library secur32.lib into your project.

GetUserNameExW – Retrieves the name of the user or other security principal associated with the calling thread. You can specify the format of the returned name. (Unicode names)

#include <stdio.h>
#include <windows.h>
#include <lmcons.h>
#define SECURITY_WIN32
#include <security.h>
int main(void) {
	DWORD length = UNLEN + 1;
	WCHAR username[length];
	if(GetUserNameExW(NameSamCompatible, username, &length) == 0)
	    printf("Error trying to retrieve username\n");
	wprintf(L"username: %s\n", username);