如何在C ++中找到LPCTSTR的长度 [英] How to find length of LPCTSTR in C++

查看:108
本文介绍了如何在C ++中找到LPCTSTR的长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我需要在C ++中找到LPCTSTR字符串的长度。我是C ++的新手,有点迷失。有人可以帮帮我吗?



非常感谢。

Hi,

I need to find the length of an LPCTSTR string in C++. I''m totally new to C++ and kind of lost. Can someone please help me out with it?

Thanks a lot.

推荐答案

微软有一套取决于 _UNICODE _MBCS 定义的typedef和宏。您可以通过调整Visual Studio项目中的项目设置来控制这些定义的存在。通常你可以编写一次源代码并使用许多不同的文本编码设置编译它,如果你使用花式类型名称,如 LPCTSTR _TEXT() 宏和函数(对不起 - 宏),如 _tcslen()

Microsoft has a set of typedefs and macros that depend on the _UNICODE and _MBCS define. You can control the presence of these defines by tweaking the project settings in your Visual Studio Project. Normally you can write the source once and the compile it with many different text encoding settings if you use fancy type-names like LPCTSTR, the _TEXT() macro, and functions (sorry - macros) like _tcslen().
LPCTSTR my_ptr = _TEXT("funny thing");
size_t len = _tcslen(my_ptr);



根据您的项目设置,上述代码可能被预处理为:


Depending on your project settings the above code may be preprocessed to:

const char* my_ptr = "funny thing";
size_t len = strlen(my_ptr);






or

const wchar_t* my_ptr = L"funny thing";
size_t len = wcslen(my_ptr);





还有什么......



我推荐的是支持utf-16或utf-16 + utf-8混合而忘记花哨的微软类型和宏如果你不喜欢我不想浪费你的时间。将您的项目设置转为unicode,要求预处理器将您的Windows系统调用预处理为''W''(widechar / utf16)版本,并仅使用utf编码处理字符串/编码(ansi是过去的)。当前的Windows版本拥有NT内核超过十年,而WinNT内部使用utf-16,因此除了broadchar版本的winapi函数之外,支持例如ansi是没有意义的。我通常在我的程序中内部使用utf8(这有助于避免例如字符串中的''L''前缀并节省内存使用)并且仅在系统调用时将字符串转换为utf16 for windows。使用正确的字符串类,您可以以任何(平台相关的)格式存储字符串,将其隐藏在接口下,这样您就可以编写一个字符串类,用于存储字符串的utf16 for windows和utf8用于同一程序中的unix系统,如果字符串使用系统调用是频繁的... utf8可以很容易地将你的应用程序移植到像系统一样的unix,而即使你专门针对windows平台,微软宏也会使事情变得复杂。


or
whatever else...

What I recommend is supporting utf-16 only or utf-16 + utf-8 mixed and forget about fancy microsoft types and macros if you don''t want to waste your time for nothing. Turn your project setting to unicode to ask the preprocessor to preprocess your windows system calls to the ''W'' (widechar/utf16) version and handle strings/encoding yourself with utf encodings only (ansi is the past). Current windows versions have NT kernel for more than a decade and WinNT uses utf-16 internally so supporting for example ansi besides the widechar version of winapi functions makes no sense. What I ususually do is using utf8 internally in my program (this helps avoiding for example the ''L'' prefix in case of strings and saves memory usage) and convert the strings to utf16 for windows only in case of system calls. With proper string classes you can store string in any (platform dependent) format hiding this under the interface, so you can write a string class that stores the string in utf16 for windows and utf8 for unix systems in the same program if string-using system calls are frequent... utf8 makes it easy to port your app to unix like systems while Microsoft macros just complicate things even if you target windows platform exclusively.


LPCTSTR被定义为a const char * const wchar_t * ,具体取决于字符集的项目设置(多字节或Unicode)。还有根据此设置定义的Microsoft特定功能,并以正确的方式处理:_ tcslen

LPCTSTR is defined as either a const char* or const wchar_t*, depending on your project settings of Character Set (multi-byte or Unicode). There is Microsoft specific functions that is also defined depending on this setting and treats in the correct way: _tcslen.
LPCTSTR pToMyString = _T("This is my string");
size_t len = _tcslen (pToMyString);


LPCTSTR是MFC中const char *的别名。你可以得到一个字符串的长度,例如使用oldschool C风格的函数strlen。



LPCTSTR is the alias in MFC for const char*. You can get the length of a string e.g. with the oldschool C-style function strlen.

LPCTSTR s = "foobar";
std::cout << "Length of s is " << strlen(s);





或者因为你显然在使用MFC,你也可以通过LPCTSTR变量实例化一个CString对象,从CString得到长度。





Or since you are apparently working with MFC, you could as well instantiate a CString object by the LPCTSTR variable and get the length from the CString.

LPCTSTR s = "foobar";
CString cs = s;
std::cout << "Length of s is " << cs.GetLength();





编辑:

只需读取LPCTSTR(对Const TCHAR的长指针) STRing)也可以是const wchar_t *,具体取决于_UNICODE常量。




Just read that LPCTSTR (Long Pointer to a Const TCHAR STRing) could also be a const wchar_t* depending on the _UNICODE constant.


这篇关于如何在C ++中找到LPCTSTR的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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