如何将字符串转换为LPCTSTR? [英] How to convert string to LPCTSTR?

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

问题描述

如何将字符串转换为LPCTSTR?

How to convert string to LPCTSTR?

推荐答案

ATL提供了一些宏来执行此操作.
ATL provides some macros to do this.
#include <atlbase.h>
USES_CONVERSION;
</atlbase.h>


相关的宏为:
CA2T(从ANSI到TCHAR的常量)
CW2T(从TCHAR到const宽).

Google USES_CONVERSION.另请参见 ATL和MFC字符串转换宏 [ ^ ]

如果使用的是STL字符串,则可能需要typedef std::basic_string<tchar> tstring</tchar>.
最近,我发现自己对Windows API函数的Unicode版本使用了越来越多的显式调用,并对所有字符串都使用了std::wstring. ANSI有点过时了...

希望这会有所帮助,
Pablo.


The relevant macros are:
CA2T (const ANSI to TCHAR)
CW2T (const wide to TCHAR).

Google USES_CONVERSION. See also ATL and MFC String Conversion Macros [^]

If you''re using STL strings, you may want to typedef std::basic_string<tchar> tstring</tchar>.
Lately, I find myself using more and more explicit calls to the Unicode versions of the Windows API functions, and using std::wstring for all my strings. ANSI is a bit dated...

Hope this helps,
Pablo.


您的问题的实际答案是:不,无法将字符串转换为LPCTSTR.第一个是由一大块内存表示的对象,后者是指向该对象的指针.

现在,让我们将您的问题解释为:我有一个字符串,需要一个LPCTSTR,例如,用于将参数传递给函数.我该怎么做?".然后这个问题就可以解决了,原来是主要的组成部分:

1)内存分配

2)字符表示(8位与16位)

让我们从(2)开始. LPTCSTR被定义为指向char字符串或wchar_t字符串的指针,具体取决于您的编译设置(VC ++项目设置中的多字节字符串或Unicode字符串).如果您的源字符串碰巧是另一种格式,则必须使用某种转换机制将宽字符(16位)转换为8位字符,反之亦然.您可以使用MultiByteToWideChar之类的函数或其对应函数来做到这一点.或者,如果您使用的是CString,则该任务可能与编写代码一样简单:

The actual answer to your question is: No, there is no way to convert a string to an LPCTSTR. The first is an object that is represented by a chunk of memory, the latter is a pointer to such an object.

Now, let''s interpret your question as: "I have a string and need an LPCTSTR, e.g. for passing an argument to a function. How can I do that?". Then the problem is solvable and turns out to have to major components:

1) Memory allocation

2) Character representation (8-bit vs. 16-bit)

Let''s start with (2). LPTCSTR is defined as pointer to a char string or wchar_t string, depending on your compilation settings (Multi-byte string or Unicode string in your VC++ project settings). If your source string happens to be in the other format, you have to use some conversion mechanism to translate wide characters (16-bit) to 8-bit characters or vice versa. You can use functions like MultiByteToWideChar or its counterpart to do that. Or, if you are using CString, the task may be as easy as writing:

// assuming we are compiling for Unicode
CString s1;
...
CStringA s2 (s1); // translates s1 to an 8-bit char string



如果您的源字符串恰好具有正确"的字符大小,则无需进行任何转换. CString有一个内置的强制转换函数以指向const char指针",因此您可以编写



If your source string happens to have the "right" character size, you don''t have to convert anything. CString has a built-in cast function to "pointer to const char", so you can write

CString s1;
...
LPCTSTR pS2 = s1;



和s1将为您提供指向其内部缓冲区的指针.

如果使用的是STL :: string,则必须通过调用c_str成员函数来显式地进行强制转换,例如:



and s1 will give you a pointer to its internal buffer.

If you are using STL::string you must do the cast explicitly by calling the c_str member function, for example:

// assuming you are compiling for multi-byte (8-bit) strings
STL::string s1;
...
LPCTSTR pS2 = s1.c_str();



现在要解决问题(1),即缓冲区管理.如果不必转换,因为字符格式确实匹配,则可以简单地使用指向现有字符串缓冲区的指针,如上面两个示例所示.

如果必须进行转换,则需要额外的缓冲区来存储转换结果.同样,您可以使用多种技术来获取此类缓冲区.在第一个示例中,我们使用了另一个CString提供缓冲区(并使用了CString的功能来转换另一个性别"的字符串).您可以使用STL :: string rsp同样出色地完成操作. STL :: wstring.或者,如果要手动完成所有操作,则必须按new malloc分配缓冲区.

我知道,有很多选择和替代方案.如果您让我们知道,使用的是哪种类型的字符串,以及是否要为多字节字符串或Unicode进行编译,以及要对LPCTSTR进行什么操作,我们可以就如何进行操作提供一些具体的建议.



Now to problem (1), buffer management. If you don''t have to convert, because character formats do match, you can simply use a pointer to the existing string buffer, just as shown in the two examples above.

If you do have to convert you need an additional buffer for the conversion result. Again you can use several techniques to acquire such a buffer. In the first example we used another CString to provide the buffer (and used CString''s capability to convert string of the other "gender"). You can do equally well with STL::string rsp. STL::wstring. Or if you want to do it all by hand you have to allocate the buffer by new or malloc.

I know, there are many options and alternatives. If you let us know, which type of string you are using and whether you are compiling for mulit-byte character strings or Unicode and what you want to do with the LPCTSTR we could give some concrete advice on how to proceed.


std::wstring s2ws(const std::string& s)
{
 int len;
 int slength = (int)s.length() + 1;
 len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);
 wchar_t* buf = new wchar_t[len];
 MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);
 std::wstring r(buf);
 delete[] buf;
 return r;
}

std::string s;

#ifdef UNICODE
std::wstring stemp = s2ws(s); // Temporary buffer is required
LPCWSTR result = stemp.c_str();
#else
LPCWSTR result = s.c_str();
#endif>


谢谢


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆