2015/06/08

C++ wcstombs_s TCHAR to Char

程式碼:


#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);
 size_t size;

 //TCHAR to Char
 wcstombs_s(&size, cName, (size_t)SIZE, name, (size_t)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/70x70wa0.aspx