程式碼:
#include "stdafx.h" #include <atlstr.h> #include <Windows.h> #define SIZE 256 int _tmain(int argc, _TCHAR* argv[]) { TCHAR name[SIZE] = TEXT(""); DWORD nameSize = sizeof(name); GetUserNameW(name, &nameSize); char * cName = (char*)malloc(SIZE); //TCHAR to Char wcstombs(cName, name, SIZE); _tprintf(TEXT("%s\n"), name); printf("%s\n",cName); ZeroMemory(name, nameSize); ZeroMemory(cName, SIZE); system("pause"); return 0; }
執行結果:
參考資料:
https://msdn.microsoft.com/en-us/library/aa273083%28v=vs.60%29.aspx