Below is a list of C/C++ character types defined by Windows and commonly used in the Windows API. Their actual types and syntax for specifying string literals are shown.
Actual Type | Literal | ||
---|---|---|---|
CHAR | char | 'a' | |
LPSTR | char* | ||
LPCSTR | const char* | "hello" | |
WCHAR | wchar_t | L'a' | |
LPWSTR | wchar_t* | ||
LPCWSTR | const wchar_t* | L"hello" | |
ANSI | Unicode | ||
TCHAR | char | wchar_t | TEXT('a') |
LPTSTR | char* | wchar_t* | |
LPCTSTR | const char* | const wchar_t* | TEXT("hello") |
Visual Studio's default setting is Unicode. See Project Properties > General > Project Defaults > Character Set.
When Unicode is set (default), both UNICODE and _UNICODE are defined. _T() is the same as TEXT(). For example tchar.h uses _UNICODE to define _T() while winnt.h uses UNICODE to define TEXT().
Windows usually prepares 3 functions of the same API: ANSI, Unicode, and either. ANSI version has "A" appended at the end of the function name while Unicode has "W" appended.
BOOL DeleteFileA(LPCSTR lpFileName);
BOOL DeleteFileW(LPCWSTR lpFileName);
BOOL DeleteFile (LPCTSTR lpFileName);
DeleteFileA( "hello.txt" );
DeleteFileW( L"hello.txt" );
DeleteFile ( TEXT("hello.txt") );
The auto-detect version uses UNICODE to swap to the correct function:
#ifdef UNICODE
#define DeleteFile DeleteFileW
#else
#define DeleteFile DeleteFileA
#endif