none
LPCTSTR

    Question

  • Hello all,

    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(
    "myname@address.com");

    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

     

    Tuesday, November 15, 2005 10:05 PM

Answers

  • 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.

    Sunday, January 15, 2006 3:07 PM

All replies

  • 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

    Tuesday, November 15, 2005 10:16 PM
  • 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

    Saturday, January 14, 2006 10:05 PM
  • 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

    Saturday, January 14, 2006 10:56 PM
  • 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.

    Sunday, January 15, 2006 3:07 PM
  • 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

    Monday, January 16, 2006 11:12 PM
  • 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

    Wednesday, February 22, 2006 9:19 AM
  • Try including atlbase.h instead. And make sure you have enabled use of ATL in Project Properties -> Configuration Properties -> General -> Use of ATL.

    Wednesday, February 22, 2006 12:49 PM
  • 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

    Wednesday, February 22, 2006 11:48 PM
  • You need to put USES_CONVERSION before using those macros.

     Sweety wrote:

    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

    Thursday, February 23, 2006 12:10 AM
  • 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.

    Monday, April 17, 2006 7:18 AM
  • You need USES_CONVERSION; in frot of the macros somewehre in you code block.

    Monday, April 17, 2006 7:20 AM
  • Thanks,this was excellent.

    Monday, September 22, 2008 9:59 AM
  • i have this function.....that wll be caled inrender.cpp as follow  =>  LoadModel(&gridbox, TEXT("GridBox.x"));

    there is a error => error C2664: 'D3DXCreateTextureFromFileA' : cannot convert parameter 2 from 'ATL::CA2WEX<>' to 'LPCSTR'

     

    void LoadModel(MODEL* Model, LPCTSTR File)
    {
        LPD3DXBUFFER bufMaterial;

        D3DXLoadMeshFromX(File,
                          D3DXMESH_SYSTEMMEM,
                          d3ddev,
                          NULL,
                          &bufMaterial,
                          NULL,
                          &Model->numMaterials,
                          &Model->Mesh);

        D3DXMATERIAL* tempMaterials = (D3DXMATERIAL*)bufMaterial->GetBufferPointer();

        Model->Material = new D3DMATERIAL9[Model->numMaterials];
        Model->Texture = new LPDIRECT3DTEXTURE9[Model->numMaterials];

        for(DWORD index = 0; index < Model->numMaterials; index++)
        {
            Model->Material[index] = tempMaterials[index].MatD3D;
            Model->Material[index].Ambient = Model->Material[index].Diffuse;

            USES_CONVERSION;
            if(FAILED(D3DXCreateTextureFromFile(d3ddev,
                                                CA2W(tempMaterials[index].pTextureFilename),
                                                &Model->Texture[index])))
                Model->Texture[index] = NULL;
        }

    can anyone help me wat is CA2W ...caz error show me this line got problem,,thanks m online to see the result...

    regards Omid

    Saturday, April 24, 2010 3:08 PM
  • Thanks a lot, your words here are so helpful.
    Sunday, April 17, 2011 2:13 PM
  • This will be helpful to you, it explains well here. 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
    Sunday, April 17, 2011 2:28 PM
  • I simply found that, you should use "CA2T" rather than "CA2W" here. Here are some characters from msdn. SourceType/DestinationType Description A ANSI character string. W Unicode character string. T Generic character string (equivalent to W when _UNICODE is defined, equivalent to A otherwise). OLE OLE character string (equivalent to W).
    Sunday, April 17, 2011 3:12 PM