2020-07-09 | C | UNLOCK

VS2019 LPWSTR/LPCWSTR指针与字符串标识符

LPWSTR/LPCWSTR指针与字符串标识符

https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-dtyp/50e9ef83-d6fd-4e22-a34a-2c6b4e3c24f3
C中的宽字符基于wchar_t数据型态
typedef unsigned short wchar_t ;

字符串标识符L

字符串标识符L使用UNICODE编码

字符串标识符_T/_TEXT/TEXT

_T()/_TEXT()/TEXT在VS2019中属tchar.h库函数
根据是否

1
2
#define UNICODE
#define _UNICODE

决定字符串的编码

LPWSTR

L长,P指针,W宽字符,STR字符串。
typedef wchar_t LPWSTR, PWSTR;
LPWSTR所指字符串需要动态wchar_t型

LPCWSTR

L长,P指针,C const,W宽字符,STR字符串。
typedef const wchar_t LPCWSTR;
LPWSTR所指字符串需要静态wchar_t型(const wchar_t
)

LPWSTR定义/初始化

定义LPWSTR
LPWSTR lpwstr;
初始化LPWSTR
LPWSTR lpwstr = NULL;

LPCWSTR定义/初始化

定义LPCWSTR
LPCWSTR lpcwstr;
初始化LPCWSTR
LPCWSTR lpcwstr = NULL;

实参传递LPWSTR形参

动态实参可直接带入传递(LPWSTR形参)

  • TCHAR实参传递LPWSTR形参
    TCHAR t_char[] = L"value";
  • WCHAR实参传递LPWSTR形参
    WCHAR w_char[] = L"value";
  • wchar_t实参传递LPWSTR形参
    wchar_t w_char_t[] = L"value";

标准wchar_t赋值

wchar_t lpwstr[] = L"cmd";//可以传至LPWSTR形参
wchar_t lpwstr[64] = { L"cmd" };//可以传至LPWSTR形参
const wchar_t* const_wchar_t = L"cmd";//LPCWSTR const wchar_t 实参无法传递LPWSTR形参
const wchar_t o1[] = { L"cmd" };//LPCWSTR const wchar_t
实参无法传递LPWSTR形参

指针指向TCHAR字符串

1
2
3
4
5
6
7
8
TCHAR tchar[] = _T("cmd");//#include "tchar.h"
LPWSTR lpWstr = tchar;

TCHAR tchar[] = TEXT("cmd");
LPWSTR lpWstr = tchar;

TCHAR tchar[] = _TEXT("cmd");
LPWSTR lpWstr = tchar;

指针指向WCHAR字符串

1
2
3
4
5
6
7
8
WCHAR wchar[] = _T("cmd");//#include "tchar.h"
LPWSTR lpWstr = wchar;

WCHAR wchar[]= TEXT("cmd");
LPWSTR lpWstr = wchar;

WCHAR wchar[] = _TEXT("cmd");
LPWSTR lpWstr = wchar;

指针指向wchar_t字符串(原生)

1
2
3
4
5
6
7
8
wchar_t wchart[] = _T("cmd");//#include "tchar.h"
LPWSTR lpWstrs = wchart;

wchar_t wchart[] = TEXT("cmd");
LPWSTR lpWstrs = wchart;

wchar_t wchart[] = _TEXT("cmd");
LPWSTR lpWstrs = wchart;

TCHAR编码转换 LPWSTR

1
2
3
4
5
const char* c = "cmd";
//char c[] = "cmd";
TCHAR wchar[64];
LPWSTR lpwstr = wchar;
MultiByteToWideChar(0, 0, c, -1, lpwstr, 10);

WCHAR编码转换 LPWSTR

1
2
3
4
5
const char* c = "cmd";
//char c[] = "cmd";
WCHAR wchar[64];
LPWSTR lpwstr = wchar;
MultiByteToWideChar(0, 0, c, -1, lpwstr, 10);

##wchar_t编码转换兼容LPWSTR

1
2
3
4
5
const char* c = "cmd";
//char c[] = "cmd";
wchar_t wchar[64];
LPWSTR lpwstr = wchar;
MultiByteToWideChar(0, 0, c, -1, lpwstr, 10);

C C++

评论加载中