含义
- 其实就是编码问题
- A:
- 代表ANSI
- W:
- 代表宽字节(wide),即 Unicode
- A:
- 一般win的库函数如果后面不加 ’A‘ 或 'W' 时,直接给它传字符串类型值时会报错:
- 以我常用的 ShellExecute() 为例:
- 会报错:
- "const char *" 类型的实参与 "LPCWSTR" 类型的形参不兼容
- 不存在从 “std::string” 到 “LPCWSTR” 的适当转换函数
- LPCWSTR 是个啥?让我们来看看它的定义:
- typedef const wchar_t* LPCWSTR;
- 即指向unicode编码字符串的32位指针,并且所指向字符串是wchar,而非char。
- 解决方法:
- 把函数改为 ShellExecuteA() 或 ShellExecuteW()
- 或者在传字符串时使用宏定义转换类型
- 对于常量字符串:
- _T() (推荐使用)自适应转换,当程序定义为unicode时,相当于L,当程序定义为ansi时,则被忽略(头文件:#include <tchar.h>)
- L ;例如原本传值为 fun("open"),使用后为fun(L"open");注意这个L并没有括号。
- CA2W() 从宽字符集(Unicode)转为多字符集(ANSI)(头文件:#include <atlstr.h>)
- CW2A() 从多字符集(ANSI)转化为宽字符集(Unicode)(头文件:#include <atlstr.h>)
- 对于string / char[] / const char[] :
- 使用以下函数
LPCWSTR stringToLPCWSTR(const std::string& orig)
{
size_t origsize = orig.length() + 1;
const size_t newsize = 100;
size_t convertedChars = 0;
wchar_t *wcstring = (wchar_t *)malloc(sizeof(wchar_t)*(orig.length()-1));
mbstowcs_s(&convertedChars, wcstring, origsize, orig.c_str(), _TRUNCATE);
return wcstring;
}
- -
- -
- 那么如果我是 char[] 或者 const char[] 呢?那当然是转string再使用上面的函数啦。
- 示例:
- -
ShellExecuteA(NULL, "open", "https://blog.coolight.cool/", NULL, NULL, SW_SHOWNORMAL);
string str = "https://blog.coolight.cool/";
ShellExecute(NULL, _T("open"), stringToLPCWSTR(str), NULL, NULL, SW_SHOWNORMAL);
const char link_char[] = "https://blog.coolight.cool/";
ShellExecute(NULL, _T("open"), stringToLPCWSTR(string(link_char)), NULL, NULL, SW_SHOWNORMAL);
ShellExecute(NULL, L"open", CA2W("https://blog.coolight.cool/"), NULL, NULL, SW_SHOWNORMAL);
常见编码格式
见另一篇文章:常见编码格式 / 字节序(大端 / 小端)/ 带BOM
http://casinvietnam.com/# casino tr?c tuy?n vi?t nam
casino tr?c tuy?n casino tr?c tuy?n vi?t nam casino tr?c tuy?n
casino tr?c tuy?n vi?t nam web c? b?c online uy tin choi casino tr?c tuy?n tren di?n tho?i
https://casinvietnam.com/# casino tr?c tuy?n vi?t nam
choi casino tr?c tuy?n tren di?n tho?i casino tr?c tuy?n choi casino tr?c tuy?n tren di?n tho?i
http://casinvietnam.com/# casino online uy tin
http://casinvietnam.shop/# casino tr?c tuy?n vi?t nam
casino tr?c tuy?n web c? b?c online uy tin choi casino tr?c tuy?n tren di?n tho?i
https://casinvietnam.shop/# web c? b?c online uy tin
game c? b?c online uy tin danh bai tr?c tuy?n choi casino tr?c tuy?n tren di?n tho?i
http://casinvietnam.shop/# game c? b?c online uy tin
casino tr?c tuy?n vi?t nam casino online uy tin casino tr?c tuy?n
http://casinvietnam.com/# casino tr?c tuy?n
casino tr?c tuy?n casino tr?c tuy?n casino tr?c tuy?n vi?t nam
http://casinvietnam.com/# casino tr?c tuy?n uy tin
casino tr?c tuy?n vi?t nam game c? b?c online uy tin game c? b?c online uy tin
https://casinvietnam.com/# casino online uy tin
ed medication online: best ed pills online – cheap ed meds
order ed meds online: cheap ed drugs – buy ed pills online
prescription online canada: non prescription online pharmacy – cheap prescription drugs online
legal online pharmacy coupon code canadian online pharmacy no prescription cheapest pharmacy for prescriptions without insurance
http://onlinepharmacyworld.shop/# best no prescription pharmacy
online pharmacy discount code: canadian pharmacy coupon code – canadian pharmacy discount code
cheapest prescription pharmacy: no prescription needed pharmacy – mail order pharmacy no prescription
ed meds cheap erectile dysfunction pills for sale erectile dysfunction online prescription
cheap pharmacy no prescription: canada drugs coupon code – online pharmacy no prescription needed
buy ed meds cheap ed drugs best online ed meds
http://medicationnoprescription.pro/# canadian pharmacy without prescription
can you buy prescription drugs in canada: canadian pharmacy online no prescription needed – no prescription pharmacy online
http://edpill.top/# affordable ed medication
ed doctor online: buy ed medication online – where can i get ed pills
canadian pharmacy coupon code canada pharmacy not requiring prescription no prescription pharmacy paypal
online canadian pharmacy coupon: cheap pharmacy no prescription – canadian pharmacy without prescription
http://edpill.top/# online erectile dysfunction medication
online pharmacy no prescription needed: pharmacy without prescription – pharmacy without prescription
online erectile dysfunction pills: ed medications online – cheapest online ed treatment
buy prescription drugs without a prescription: buy drugs online no prescription – online medication without prescription
cheap ed pills: cheapest ed medication – cheapest online ed meds
pharmacy without prescription online pharmacy non prescription drugs cheapest pharmacy to fill prescriptions without insurance
buy medication online with prescription: prescription meds from canada – canada pharmacy without prescription
boner pills online: cheap ed medicine – low cost ed meds