tags:

views:

109

answers:

6

As I clould not pass LPCSTR from one function to another (Data get changed) I tried passing it as a string.

But later I need to again convert it back to LPSTR. While trying the conversion I am getting the above error:

cannot convert from 'std::string' to 'LPSTR'

How can I resolve this?

A: 

Are you running somestringvariablename.c_str()? That should work.

Amigable Clark Kant
+1  A: 

There is a function on std::string c_str() . However I doubt that you could not use a std::string in your case.

rerun
If the OP is calling a Win32 API, he can't use `std::string` directly.
Pedro d'Aquino
I read it as two functions he wrote. Since you can't pass a std::string into any windows api function with out conversion anyway.
rerun
A: 

An LPSTR can be substituted with by using a TCHAR (i.e. found in tchar.h). So if you have a std::string, you can use the method std::string::c_str().

C Johnson
A: 

If the function, you are calling does not write to string, but only reads it, then you can simply use string::c_str method. If it is going to write something, then you probably should ensure that your string has enough space by calling string::reserve().

ironic
A: 

That's just because you should use std::string::c_str() method.

But this involves const_cast in given case because const char * returned by c_str() can not be assigned to a non-constant LPSTR.

std::string str = "something";
LPSTR s = const_cast<char *>(str.c_str());

But you must be sure that lifetime of str will be longer that that of LPTSTR variable.

Another mention, if code compiles as Unicode-conformant, then types LPTSTR and std::string are incompatible. You should use std::wstring instead.

Keynslug
@Keynslug, Eventually Solved my problem. But why is this( const_cast:) horrible
Subhen
This won't work. `c_str()` returns a `const char*` which can be casted to `LPCSTR` but not to `LPSTR`. If you `const_cast` `c_str()` to make it work it is **undefined behavior**.
ereOn
The last paragraph is wrong too, LPSTR is *always* char*, even in a Unicode build. You're talking about LPTSTR.
Hans Passant
@ereOn Mentioned about `const_cast` while writing answer but removed mention somewhy. But why `const_cast<char *>(str.c_str())` is undefined behavior? Is that right because we cannot guarantee that lifetime of `str` will be longer that that of `LPTSTR` variable?
Keynslug
@Hans Passant, thanks, i've edited the answer.
Keynslug
You should fix the const_cast<> as well. Flip your answer to CW if you don't like the downvotes.
Hans Passant
@Keynslug: `c_str()` gives something `const`. Removing the `const` qualifier supposes that you make an assumption about the underlying implementation of `c_str()` and know for sure that it is not `const`. Even if this was true for your compiler, modifying the returned value won't modify the `std::string` itself, so what's the point ?
ereOn
A: 

If you need an LPSTR, that means the string will/may be modified. std::string::c_str() returns a const pointer, and you can't just const_cast it away and hope all is good in the world, because it isn't. The string may be changed in all sorts of nasty ways, and your original std::string will be oblivious to all of them.

Try this instead:

// myFunction takes an LPSTR
std::string cppString = "something";
LPSTR cString = strdup( cppString.c_str() );
try {
   myFunction( cString );
   cppString = cString;
} catch(...) {
   free( cString );
}

Wrap the string in a smart pointer and get rid of the try...catch for bonus points (don't forget the custom deleter).

Pedro d'Aquino