2015/06/05

C++ Get current user name


程式碼:
#include "stdafx.h"
#include <stdio.h>
#include <atlstr.h>
#include <WinBase.h>

#pragma comment(lib, "Advapi32.lib")

int _tmain(int argc, _TCHAR* argv[])
{

 TCHAR name[256] = TEXT("");
 DWORD nameSize = sizeof(name);

 GetUserNameW(name, &nameSize);
 _tprintf(TEXT("%s\n"), name);

 ZeroMemory(name, nameSize);

 system("pause");
 return 0;
}


執行結果:


參考資料:
https://msdn.microsoft.com/zh-tw/library/windows/desktop/ms724432%28v=vs.85%29.aspx