I've got another question. I'm using a bit of code written by Nishant Sivakumar (http://www.codeproject.com/tips/CSMTPConnection2.asp) to send an email programmatically. The code is relatively straightforward and there are no problems during compile EXCEPT...
Error 3 error C2664: 'ATL::CMimeHeader::SetSender' : cannot convert parameter 1 from 'const char [19]' to 'LPCTSTR'
SetSender is a member of CSMTPConnection2, which was written by Nish and inherits from CSMTPConnection. It expects an LPCTSTR. Here's how I call it:
CMimeMessage msg;
msg.SetSender(
In fact, every call to this class expects an LPCTSTR and won't compile when I call it with a string literal. In his example code (linked above), Nish calls the functions with literals and doesn't mention any problems.
My question is simple and fundamental. What is an LPCTSTR How do I use them
Thanks,
bubbafett

LPCTSTR
David Ballard
I was getting this same compile error:
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,
Jeremiah
ZO6
Uncle Davy
To learn about Unicode and TCHAR programming in Visual C++, you should read chapter 2 of the book "Programming 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.
RDH123
I had the same problem with my programs and I didn't understand anything, because in the previous version of Visual Studio(2003) they worked properly. I did what you say and now it compiles again. I think Visual Studio 2003 had set the option "Not Set" and we didn't realize, and then we started to use VS 2005 which has the option Unicode, and that was the problem.
Thank you Jeremiah, because that small problems take me a lot of time and make me sick.
harendra
Try including atlbase.h instead. And make sure you have enabled use of ATL in Project Properties -> Configuration Properties -> General -> Use of ATL.
kgooding
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,
Jeremiah
jqsimple
Hi,
Thanks for the reply. But i've added atlbase.h an enabled use of ATL to static ATl, but still i'm facing the problem. I think the header might be with problem. Here you with the problem and what header macro has
#define
A2W(lpa) (((_lpa = lpa) == NULL) NULL : (_convert = (lstrlenA(_lpa)+1),ATLA2WHELPER((LPWSTR) alloca(_convert*2), _lpa, _convert, _acp)))d:\New Folder\sample\sample\sample.cpp(17) : error C2065: '_lpa' : undeclared identifier
d:\New Folder\sample\sample\sample.cpp(17) : error C2065: '_convert' : undeclared identifier
d:\New Folder\sample\sample\sample.cpp(17) : error C2065: '_acp' : undeclared identifier
d:\New Folder\sample\sample\sample.cpp(17) : error C3861: '_lpa': identifier not found, even with argument-dependent lookup
d:\New Folder\sample\sample\sample.cpp(17) : error C3861: '_convert': identifier not found, even with argument-dependent lookup
d:\New Folder\sample\sample\sample.cpp(17) : error C3861: '_lpa': identifier not found, even with argument-dependent lookup
d:\New Folder\sample\sample\sample.cpp(17) : error C3861: '_convert': identifier not found, even with argument-dependent lookup
Thanks,
Sweety
majorobvious
Hi,
Further to above, i'm using A2W() function to convert char to wchar. But i'm facing problem with header that A2W is defined in atlconv.h as a macro in Vs .net 2003. and what ever the variable used there is not found. Iz what its reporting. Do i need to add any other headers with this
Kindly ans me as soon as possible as i blocked...
Thanks,
Sweety
Stacey Doerr - MSFT
You need to put USES_CONVERSION before using those macros.
Steve Whitford
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")
Brian
BobP1339
If you need conversion support from and to Unicode, do the following
Place this at the top of the precompiled header file:
#include <atlbase.h>
At the very beginning of every method that requires conversion, place the following macro, like this:
type MyFunction(...)
{
USES_CONVERSION;
//Body
}
If you need conversion from ASCII to Unicode, do the following:
LPCWSTR lpwszUnicode = A2W(lpszAscii);
and from Unicode to ASCII
LPCSTR lpszAscii = W2A(lpwszUnicode);
I hope this helps