windows字符串类型
来源:互联网 发布:免费刷钻软件 编辑:程序博客网 时间:2024/05/16 07:11
解释一: LP-长指针 解释二: LPCSTR A 32-bit pointer to a constant character string. 解释三: LPCSTR 就是 静态char * 静态8位Windows字符(ANSI)无终结字符串指针 L表示long指针, 这是为了兼容Windows 3.1等16位操作系统遗留下来的, 在win32中以及其他的32为操作系统中, long指针和near指针及far修饰符都是为了兼容的作用。没有实际意义。 另外还有: MSDN,LPCTSTR:A 32-bit pointer to a constant character string L表示long指针 这是为了兼容Windows 3.1等16位操作系统遗留下来的,在win32中以及其他的32为操作系统中, long指针和near指针及far修饰符都是为了兼容的作用。没有实际意义。 C表示是一个常量 T表示在Win32环境中, 有一个_T宏 这个宏用来表示你的字符是否使用UNICODE, 如果你的程序定义了UNICODE或者其他相关的宏,那么这个字符或者字符串将被作为UNICODE字符串,否则就是标准的ANSI字符串。 STR表示这个变量是一个字符串 LPCTSTR == const TCHAR * CString 和 LPCTSTR 可以说通用。 原因在于CString定义的自动类型转换,没什么奇特的,最简单的C++操作符重载而已。 CString 转LPCTSTR:
C-Const
T-Unicode/ANSI兼容
STR-字符串
LPSTR A 32-bit pointer to a character string.
LPCTSTR A 32-bit pointer to a constant character string that is portable for Unicode and DBCS.
LPTSTR A 32-bit pointer to a character string that is portable for Unicode and DBCS.
LPCTSTR 就是 静态wchar_t * 如果UNICODE已定义则为LPCWSTR,否则为LPCTSTR
P表示这是一个指针
C表示是一个常量
T在Win32环境中, 有一个_T宏, 这个宏用来表示你的字符是否使用UNICODE, 如果你的程序定义了UNICODE或者其他相关的宏, 那么这个字符或者字符串将被作为UNICODE字符串, 否则就是标准的ANSI字符串。
STR表示这个变量是一个字符串。
所以LPCTSTR就表示一个指向常固定地址的可以根据一些宏定义改变语义的字符串。
同样, LPCSTR就只能是一个ANSI字符串, 在程序中我们大部分时间要使用带T的类型定义。
LPCTSTR == const TCHAR *
BOOL A Boolean value.
BSTR A 32-bit character pointer.
BYTE An 8-bit integer that is not signed.
COLORREF A 32-bit value used as a color value.
DWORD A 32-bit unsigned integer or the address of a segment and its associated offset.
LONG A 32-bit signed integer.
LPARAM A 32-bit value passed as a parameter to a window procedure or callback function.
LPCSTR A 32-bit pointer to a constant character string.
LPSTR A 32-bit pointer to a character string.
LPCTSTR A 32-bit pointer to a constant character string that is portable for Unicode and DBCS.
LPTSTR A 32-bit pointer to a character string that is portable for Unicode and DBCS.
LPVOID A 32-bit pointer to an unspecified type.
LRESULT A 32-bit value returned from a window procedure or callback function.
UINT A 16-bit unsigned integer on Windows versions 3.0 and 3.1; a 32-bit unsigned integer on Win32.
WNDPROC A 32-bit pointer to a window procedure.
WORD A 16-bit unsigned integer.
WPARAM A value passed as a parameter to a window procedure or callback function: 16 bits on Windows versions 3.0 and 3.1; 32 bits on Win32.
Data types unique to the Microsoft Foundation Class Library include the following:
POSITION A value used to denote the position of an element in a collection; used by MFC collection classes.
LPCRECT A 32-bit pointer to a constant (nonmodifiable) RECT structure.
P表示这是一个指针
所以LPCTSTR就表示一个指向常固定地址的可以根据一些宏定义改变语义的字符串。
同样, LPCSTR就只能是一个ANSI字符串,在程序中我们大部分时间要使用带T的类型定义。
常量字符串ansi和unicode的区分是由宏_T来决定的。但是用_T("abcd")时, 字符串"abcd"就会根据编译时的是否定一_UNICODE来决定是char* 还是 w_char*。 同样,TCHAR 也是相同目的字符宏。 看看定义就明白了。简单起见,下面只介绍 ansi 的情况,unicode 可以类推。
ansi情况下,LPCTSTR 就是 const char*, 是常量字符串(不能修改的)。
而LPTSTR 就是 char*, 即普通字符串(非常量,可修改的)。
这两种都是基本类型, 而CString 是 C++类, 兼容这两种基本类型是最起码的任务了。
由于const char* 最简单(常量,不涉及内存变更,操作迅速), CString 直接定义了一个类型转换函数
operator LPCTSTR() {......}, 直接返回他所维护的字符串。
当你需要一个const char* 而传入了CString时, C++编译器自动调用 CString重载的操作符 LPCTSTR()来进行隐式的类型转换。
当需要CString , 而传入了 const char* 时(其实 char* 也可以),C++编译器则自动调用CString的构造函数来构造临时的 CString对象。
因此CString 和 LPCTSTR 基本可以通用。
但是 LPTSTR又不同了,他是 char*, 意味着你随时可能修改里面的数据,这就需要内存管理了(如字符串变长,原来的存贮空间就不够了,则需要重新调整分配内存)。
所以 不能随便的将 const char* 强制转换成 char* 使用。
楼主举的例子
LPSTR lpstr = (LPSTR)(LPCTSTR)string;
就是这种不安全的使用方法。
这个地方使用的是强制类型转换,你都强制转换了,C++编译器当然不会拒绝你,但同时他也认为你确实知道自己要做的是什么。因此是不会给出警告的。
强制的任意类型转换是C(++)的一项强大之处,但也是一大弊端。这一问题在 vc6 以后的版本(仅针对vc而言)中得到逐步的改进(你需要更明确的类型转换声明)。
其实在很多地方都可以看到类似
LPSTR lpstr = (LPSTR)(LPCTSTR)string;
地用法,这种情况一般是函数的约束定义不够完善的原因, 比如一个函数接受一个字符串参数的输入,里面对该字符串又没有任何的修改,那么该参数就应该定义成 const char*, 但是很多初学者弄不清const地用法,或者是懒, 总之就是随意写成了 char* 。 这样子传入CString时就需要强制的转换一下。
这种做法是不安全的,也是不被建议的用法,你必须完全明白、确认该字符串没有被修改。
CString 转换到 LPTSTR (char*), 预定的做法是调用CString的GetBuffer函数,使用完毕之后一般都要再调用ReleaseBuffer函数来确认修改 (某些情况下也有不调用ReleaseBuffer的,同样你需要非常明确为什么这么做时才能这样子处理,一般应用环境可以不考虑这种情况)。
同时需要注意的是, 在GetBuffer 和 ReleaseBuffer之间,CString分配了内存交由你来处理,因此不能再调用其他的CString函数。
CString cStr;
const char *lpctStr=(LPCTSTR)cStr;
LPCTSTR转CString:
LPCTSTR lpctStr;
CString cStr=lpctStr;
- windows字符串类型
- Windows字符串类型
- Windows字符串类型 CHAR* WCHAR* LPSTR LPCSTR ......
- Windows字符串类型 CHAR* WCHAR* LPSTR LPCSTR
- Windows字符串类型(解惑贴)
- 字符串类型
- 字符串类型
- 字符串类型
- 字符串类型
- 字符串类型
- C# 引用类型-字符串类型
- 字符串、值类型引用类型
- 字符串类型转int类型
- String:字符串类型
- String:字符串类型
- 字符串转枚举类型
- 1.1.1. 字符串类型
- LPCTSTR类型和字符串
- bitmap思想及其在java中的相关实现
- 类String的 构造函数、拷贝构造函数、析构函数、赋值函数实现
- think about
- 转:IOCP在网络应用中常见错误分析
- 熟悉wince集成开发环境Platform Builder
- windows字符串类型
- 另一种ASP.NET与Ajax 的实现方式--jQuery
- 用UL标签+CSS实现的柱状图
- Sqlite数据库删除大量数据后的擦屁股工作
- flv网页播放器代码
- deb包 内容解析
- VML+Javascript实现的垂直柱状统计图
- 想下载东西好难啊
- 男人二十岁后应该学会的习惯