LPCWSTR
LPCWSTR
An LPCWSTR is a 32-bit pointer to a constant string of 16-bit Unicode Charactor, which may be null-terminated.
This type is declared as follows:
typedef const wchar_t* LPCWSTR;
該類型是如下聲明的:
typedef const wchar_t* LPCWSTR;
因為在VS2005以後,編碼方式默認為Unicode,部分函數在使用時默認調用Unicode方式(函數名+W,exp:MessageBox+W=MessageBoxW),而非ANSI方式(函數名+A,exp:MessageBox+A=MessageBoxA)。
請看winuser.h中的聲明如下:
WINUSERAPI
int
WINAPI
MessageBoxA(
__in_opt HWND hWnd,
__in_opt LPCSTR lpText,
__in_opt LPCSTR lpCaption,
__in UINT uType);
WINUSERAPI
int
WINAPI
MessageBoxW(
__in_opt HWND hWnd,
__in_opt LPCWSTR lpText,
__in_opt LPCWSTR lpCaption,
__in UINT uType);
#ifdef UNICODE
#define MessageBox MessageBoxW
#else
#define MessageBox MessageBoxA
#endif
上述聲明的意思是,在unicode編碼下MessageBox被編譯為MessageBoxW,否則就編譯為MessageBoxA。而兩者的區別則看函數聲明中參數2、3就可以明白了。
如果遇到參數錯誤(cannot convert parameter * from 'const char [**]' to 'LPCWSTR'),可以考慮察看聲明,如果有ANSI方式的只要在函數後面加個A就可以了,或者在定義參數時把char*改為WCHAR*。