本文介绍了将WCHAR转换为LPCTSTR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用CSocket,我想使用IP地址建立连接.

With CSocket, I want to make a connect with an IP address.

CSocket client;
client.Create();
client.Connect(IP, 80);

但是IP被定义为WCHAR ip[16];

client.Connect(IP, 80)要求IPLPCTSTR类型

如何从WCHAR转换为LPCTSTR?

推荐答案

如果您是为Unicode字符集构建的(因为任何比2000左右的Windows程序都应该更新),LPCTSTR将是LPCWSTR的typedef wchar_t const *,而wchar_t[]数组将衰减到该值.

If you build for the Unicode character set (as any Windows program more recent than about 2000 should), LPCTSTR will be a typedef for LPCWSTR aka wchar_t const *, and a wchar_t[] array would decay to that.

如果针对多字节字符集进行构建,则必须转换数据.我建议为此使用CW2T()(实际上是一个类,但几乎总是用作临时对象),例如:

If you build for the Multibyte character set, you'll have to convert your data. I suggest using CW2T() for that (it is actually a class, but is almost always used as a temporary object), eg:

client.Connect(CW2T(ip), 80);

这篇关于将WCHAR转换为LPCTSTR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 09:45