How to convert std::string to LPCSTR?
str.c_str()
gives you a const char *
, which is an LPCSTR
(Long Pointer to Constant STRing) -- means that it's a pointer to a 0
terminated string of characters. W
means wide string (composed of wchar_t
instead of char
).
std::string to LPCTSTR
Your problem here is the fact that LPCTSTR
is resolved to wchar_t*
or char*
based on whether your build supports unicode (unicode flag set or not).
To explicitly call the char*
version, call CreateDirectoryA()
.
Converting string to LPCTSTR
Eventually, I managed with the pitfall using conversion-function s2ws() and doing few operations. I place my soultion here for people who will have similar troubles with converting string. In my first post i wrote that i needed to convert string to LPCTSTR and finally it turned out that argument in my function is not, LPCTSTR but LPCWSTR that is const wchar_t*.
So, soulution:
string = "COM3";
wstring stemp;
LPCWSTR result_port;
stemp = s2ws(port_nr);
result_port = stemp.c_str(); // now passing result_port to my function i am getting success
declaration of s2ws:
wstring s2ws(const std::string& s)
{
int len;
int slength = (int)s.length() + 1;
len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);
wchar_t* buf = new wchar_t[len];
MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);
std::wstring r(buf);
delete[] buf;
return r;
}
cannot convert from 'std::string' to 'LPSTR'
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.
Important note: If you pass the resulting pointer s
from above to a function which tries to modify the data it is pointing to this will result in undefined behaviour. The only way to properly deal with it is to duplicate the string into a non-const buffer (e.g. via strdup
)
How to convert std::wstring to LPCTSTR in C++?
Simply use the c_str
function of std::w/string
.
See here:
http://www.cplusplus.com/reference/string/string/c_str/
std::wstring somePath(L"....\\bin\\javaw.exe");
if (!CreateProcess(somePath.c_str(),
cmdline, // Command line.
NULL, // Process handle not inheritable.
NULL, // Thread handle not inheritable.
0, // Set handle inheritance to FALSE.
CREATE_NO_WINDOW, // ON VISTA/WIN7, THIS CREATES NO WINDOW
NULL, // Use parent's environment block.
NULL, // Use parent's starting directory.
&si, // Pointer to STARTUPINFO structure.
&pi)) // Pointer to PROCESS_INFORMATION structure.
{
printf("CreateProcess failed\n");
return 0;
}
How can I convert an std::filesystem::path to LPCSTR for use in one of the LoadLibrary() variants?
Actually on Windows you should be able to use LoadLibraryW(path1.c_str())
as on Windows the returned type of std::filesystem::path::c_str() should be a const wchar_t*
so it's a good fit for the LoadLibraryW
expected LPCWSTR
.
As for the error of C2228
my guess is, you tried path1.string.c_str()
as given by your comment, wich should have been path1.string().c_str()
. That would give you a LPCSTR
compatible string for LoadLibaryA
, but if there is a chance of Non-ASCII in your path I would suggest using the explicit LoadLibaryW
version.
In any way: When interfacing WinAPI with std::filesystem::path
you should use the explicit A/W-Version to make your code safe independent of the state of _UNICODE
, and I allways suggest the *W
versions.
std::ostringstream to LPCSTR?
Like this:
std::string str = oss.str();
LPCSTR cstr = str.c_str();
Note that you cstr
only remains valid until the next modification of str
. So you cannot, for instance, return cstr
from a function because str
is a local variable that has left scope.
Instead of returning LPCSTR
from this function, return std::string
. That avoids dealing with lifetime issues if you returned LPCSTR
. If you returned LPCSTR
you would have to allocate memory and make sure you deallocated it. Exactly the sort of thing you don't want to be doing in C++ code. So, return std::string
and call c_str()
on that object at the point where you call the Windows API function.
Related Topics
C++ Vector Size. Why -1 Is Greater Than Zero
Behavior of Post Increment in Cout
Arrays VS Vectors: Introductory Similarities and Differences
Is the 'Override' Keyword Just a Check For a Overridden Virtual Method
Can Any One Provide Me a Sample of Singleton in C++
C++: Constructor Initializer For Arrays
Non-Blocking Console Input C++
C++ Template Partial Specialization Member Function
Why Is My Integer Math With Std::Pow Giving the Wrong Answer
Boost_1_60_0 .Zip Installation in Windows