Link to home
Start Free TrialLog in
Avatar of John500
John500Flag for United States of America

asked on

How is it possible to convert a 'string' variable to a 'LPCTSTR' variable?

Greetings:

In my program I need to perform various 'string' operations to create a specific string.  Afterwards, this string needs to be used in a function which takes a 'LPCTSTR' parameter.

How can I convert the 'string' into a variable of type 'LPCTSTR'?

The assingment I'm attempting looks like this:

string StringFileName;
LPCTSTR pstrLocalFile;

...
...
pstrLocalFile = StringFileName;
...

The error I'm getting is:

error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >
Avatar of JonFish85
JonFish85

try this maybe:

pstrLocalFile = (LPCTSTR)StringFileName;

hope this helps!
Avatar of Axter
pstrLocalFile = StringFileName.c_str();
Avatar of John500

ASKER

JonFish85,

I did that, and got this:

error C2440: 'type cast' : cannot convert from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'const char *'
        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

Try,

pstrLocalFile = (LPCTSTR) StringFileName.c_str();

Axter (and also sinbad) is right about how to get it to compiler.

BUT you need to make sure to use it correctly.  the c_str()  member function returns a pointer to a temporarily allocated NUl-terminated string.   This string may be deleted or may be changed the next time a non-constant member function is called on the string object.  This includes the string object's destructor.  that means that the pointer returned is of a temporary nature.  You cannot save that point and use it later (long later).  you must use the pointer (almost) immediately and then not use it.  Once the string has had the opportunity to change, that pointer is not valid.  for example

char *F()
{
   const char *ptr;
   char c;
   string S = "ABC";

   ptr = S.c_str();
   cout << ptr; // okay
   c = *ptr; //  okay;
   S[0] = '1'; // String has been changed, pointer is not    valid.
   cout << ptr; // DANGER ERROR.

   return ptr; // ERROR even if string hadn't been
            // changed. here it will be destroyed
            // so pointer is not valid.
}
"pstrLocalFile = (LPCTSTR) StringFileName.c_str();"

This is useless.
If you're in Unicode it's not going to compile. If your not, you're casting a 'const char*' into a 'const char*'...
Just to clarify, that.  It is the cast that is useless--actually more "dangerious", not the technique of calling c_str().   That part is fine
I think it's the fact that a TCHAR string is being used that is useles...

something like this is needed:

USES_CONVERSION;
string StringFileName;
LPCTSTR pstrLocalFile;
pstrLocalFile = A2CT(StringFileName.c_str());
That would be the safest. But maybe overkill if you don't plan on supporting unicode.   The problem with the original is that it you ever did change your mind and support unicode, the code would still compile and then work wrong.
what about CString??
Avatar of John500

ASKER

nietod,

This will be a fairly simple program of opening an FTP session and transfering a file.  

I'll need to accept a command line parameter and pass that parameter to the LPCTSTR variable.  Since you say the pointer must be used "(almost) immediately", wouldn't you agree that the following sequence will work fine:

#include <afxinet.h>


void FTPConnect(LPCTSTR pstrLocalFile)
{
    LPCTSTR pstrRemoteFile "some directory";
...
PutFile(pstrLocalFile, pstrRemoteFile, ...);
...
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    string StringFileName;

    StringFileName = lpCmdLine;
    pstrLocalFile = FTPFileName.c_str();

    FTPConnect();
    ...
}

Avatar of John500

ASKER

nietod,

My last comment should have shown the FTPConnect function taking a parameter within main():

FTPConnect(pstrLocalFile);

Also, to reiterate, the reason I don't just assign the command line parameter (type LPSTR) to the LPCTSTR variable (which will work) is because I need to first test the command line string and add to it a directory path.  Since I can't easily add these strings, I first manipulate the string and then assign it to the LPCTSTR.


   
ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of John500

ASKER

Ok great!
Thanks, although it was really Axter that 1st suggested the solution.