游戏开发论坛

 找回密码
 立即注册
搜索
查看: 3722|回复: 7

LPCTSTR = Long Pointer to a Const TCHAR STRing

[复制链接]

36

主题

107

帖子

107

积分

注册会员

Rank: 2

积分
107
发表于 2008-2-28 10:12:00 | 显示全部楼层 |阅读模式
Error 3 error C2664: 'ATL::CMimeHeader::SetSender' : cannot convert parameter 1 from 'const char [19]' to 'LPCTSTR'

36

主题

107

帖子

107

积分

注册会员

Rank: 2

积分
107
 楼主| 发表于 2008-2-28 10:13:00 | 显示全部楼层

Re:LPCTSTR = Long Pointer to a Const TCHAR STRing

LPCTSTR = Long Pointer to a Const TCHAR STRing  (Don't worry, a Long Pointer is the same as a pointer. There were two flavors of pointers under 16-bit windows.)

Here's the table:

LPSTR = char*
LPCSTR = const char*
LPWSTR = wchar_t*
LPCWSTR = const wchar_t*
LPTSTR = char* or wchar_t* depending on _UNICODE
LPCTSTR = const char* or const wchar_t* depending on _UNICODE

"myname@address.com" is a const char*.  Try the alternative L"myname@address.com" which is a const wchar_t*.

If this compiles, then get into the habit of using the _T macro which will prepend L for Unicode builds, keeping your code as portable as possile.  e.g. _T("myname@address.com")

36

主题

107

帖子

107

积分

注册会员

Rank: 2

积分
107
 楼主| 发表于 2008-2-28 10:19:00 | 显示全部楼层

Re:LPCTSTR = Long Pointer to a Const TCHAR STRing

Error 1 error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'const char [13]' to 'LPCWSTR' ...


For some strange reason, I remeber seeing code that prefixed string literals with an" L" character, but I never really knew why this prefix was there.  So, I prefixed my strings with the magic "L" character and poof.. the compile error went away.

Can somebody explain what this "L" represents and is there some project setting or #define, #pragma that I can use to avoid having to prefix my string literals with the "L"?

I also tried to prefix my strings with "_T(...)" but I just get the following compile error:

Error 1 error C3861: '_T': identifier not found ...


Does this require a special header file? (At the moment, I only #include <windows.h>).

I'm really new to this windows programming and until today, I never even heard of a LPWSTR or LPCSTR ... etc.  but I do know that prefixing a string with a captial letter "L" is not a C/C++ standard unless it defines a macro of some sorts...(When I tried to "go to definition" of "L", I only got "identifier not found" message box).

Thanks in advance for any enlightenment!

Regards,

36

主题

107

帖子

107

积分

注册会员

Rank: 2

积分
107
 楼主| 发表于 2008-2-28 10:22:00 | 显示全部楼层

Re:LPCTSTR = Long Pointer to a Const TCHAR STRing

Well, it seems I have answered my own question...  Not about the definition of the "L" magic prefix character, but I noticed that in another project, the "Character Set" option (Project Properties -> Configuration Properties -> General -> Character Set) in the project settings was "Not Set".  In this case, I didn't need to use the "L" prefix on string literals when casting to a LPCSTR type.

However, when this option is set to "Use Unicode character set", then string literals needed to be prefixed with the magic "L" in order to compile.

If anybody can clearly explain what this "L" string prefix does, that would be helpful in my future programming experiences.

Thanks in advance,

36

主题

107

帖子

107

积分

注册会员

Rank: 2

积分
107
 楼主| 发表于 2008-2-28 10:24:00 | 显示全部楼层

Re:LPCTSTR = Long Pointer to a Const TCHAR STRing

Well, it seems I have answered my own question...  Not about the definition of the "L" magic prefix character, but I noticed that in another project, the "Character Set" option (Project Properties -> Configuration Properties -> General -> Character Set) in the project settings was "Not Set".  In this case, I didn't need to use the "L" prefix on string literals when casting to a LPCSTR type.

However, when this option is set to "Use Unicode character set", then string literals needed to be prefixed with the magic "L" in order to compile.

If anybody can clearly explain what this "L" string prefix does, that would be helpful in my future programming experiences.

Thanks in advance,

36

主题

107

帖子

107

积分

注册会员

Rank: 2

积分
107
 楼主| 发表于 2008-2-28 10:25:00 | 显示全部楼层

Re:LPCTSTR = Long Pointer to a Const TCHAR STRing

To learn about Unicode and TCHAR programming in Visual C++, you should read chapter 2 of the book &quotrogramming Windows" by Charles Petzold. Or if you don't have that book, check out the MSDN section on International Programming: http://msdn2.microsoft.com/en-us/library/06b9yaeb.aspx (especially the part called "Generic-Text Mappings in Tchar.h"). Notice that _T() is defined as:

#ifdef _UNICODE
#define _T(x)      L ## x
#else /* _UNICODE */
#define _T(x)      x
#endif /* _UNICODE */

ie. if the preprocessor directive _UNICODE is defined, _T("...") expands to the string prefixed by the letter L (L"..."), otherwise it just expands to the original string ("..."). And LPTSTR expands to char * (if _UNICODE is not defined), or to wchar_t * (if _UNICODE is defined).

The "Character Set" option controls whether _UNICODE and UNICODE are #defined or not.


36

主题

107

帖子

107

积分

注册会员

Rank: 2

积分
107
 楼主| 发表于 2008-2-28 10:57:00 | 显示全部楼层

Re:LPCTSTR = Long Pointer to a Const TCHAR STRing

#include   <tchar.h>

36

主题

107

帖子

107

积分

注册会员

Rank: 2

积分
107
 楼主| 发表于 2008-2-28 11:04:00 | 显示全部楼层

Re:LPCTSTR = Long Pointer to a Const TCHAR STRing

建立的环境的UNICODE。
MessageBox 是一个宏,会按UNICODE展开成MessageBoxW
MessageBoxW第二,三个参数是wchar_t。
所以要加一个 "L "
MessageBox( NULL, L "注册窗口时出错! ", L "出错啦 ", 0 );
TEXT( "注册窗口出错 ")
或者用TEXT()宏。TEXT宏会根据UNICODE展开成L或者空
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-12-20 06:05

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表