Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 411
  • Last Modified:

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> >
0
John500
Asked:
John500
1 Solution
 
JonFish85Commented:
try this maybe:

pstrLocalFile = (LPCTSTR)StringFileName;

hope this helps!
0
 
AxterCommented:
pstrLocalFile = StringFileName.c_str();
0
 
John500Author Commented:
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
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
sindbadCommented:

Try,

pstrLocalFile = (LPCTSTR) StringFileName.c_str();

0
 
nietodCommented:
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.
}
0
 
AssafLavieCommented:
"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*'...
0
 
nietodCommented:
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
0
 
jasonclarkeCommented:
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());
0
 
nietodCommented:
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.
0
 
MattCCommented:
what about CString??
0
 
John500Author Commented:
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();
    ...
}

0
 
John500Author Commented:
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.


   
0
 
nietodCommented:
That is sage because the strig object is not ever altered in the code.  The only time it is "altered" is when it is destroyed at the end of the function and you are done using it at that time.

If you did something like

   string StringFileName;

   StringFileName = lpCmdLine;
   pstrLocalFile = StringFileName.c_str();
   
   StringFileName += " "; // alter string.

   FTPConnect(pstrLocalFile); // ERROR.

that would be a problem.


However, a slightly safer approach is

FTPConnect(StringFileName.c_str());

This never stores the pointer to the c-string.  This is safest because the pointer is only used when the FTPConnect function is used and durring this period of time it is impossible for the string object to change.  So there is almost no chance of making an accidental mistake that way.  You could add code that does change hte strign object, yet this code would still be safe.
0
 
John500Author Commented:
Ok great!
0
 
nietodCommented:
Thanks, although it was really Axter that 1st suggested the solution.  
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now