tags:

views:

2456

answers:

5

Hi

Can any ne Help converting string to LPWSTR

string command=obj.getInstallationPath()+""

Now i wat to pass it as parameter for CreateProcessW(xx,command,x.......)

But cretaeProcessW() accepts only LPWSTR so i need to cast string to LPWSTR

Thanks in Advance

+9  A: 

If you have an ANSI string, then have you considered calling CreateProcessA instead? If there is a specific reason you need to call CreateProcessW then you will need to convert the string. Try the MultiByteToWideChar function.

Greg Hewgill
+1  A: 

You could store it in a CString and call the LPWSTR operator on it:

const char* sz = "tadaaa";
const CString s( sz );
LPCWSTR ws = static_cast<LPCWSTR>( s ); //calling CString::operator (LPCWSTR)()const;

Note that the WSTR cast operator is only defined for unicode builds.

xtofl
please don't use C-style casts in C++ sample code
@mmutsz: you're right: we shouldn't do that. However, the C-style cast syntactically resembles the cast-operator.
xtofl
+3  A: 

The easiest way to convert an ansi string to a wide (unicode) string is to use the string conversion macros.

To use these, put USES_CONVERSION at the top of your function, then you can use macros like A2W() to perform the conversion very easily.

eg.

char* sz = "tadaaa";
CreateProcessW(A2W(sz), ...);

The macros allocate space on the stack, perform the conversion and return the converted string.

gbjbaanb
The documentation you refer to also says you don't need the USES_CONVERSION for ATL7.0 and up.
xtofl
yeah, but I've been using it for so long I add that automatically :)
gbjbaanb
A: 

It looks like this would work:

LPCWSTR string_to_LPCWSTR(const std::string& s)
{
    std::basic_ostringstream<WCHAR> buffer;
    buffer << s;
    return buffer.str().c_str();
}

The idea is to output the std::string into a basic_ostringstream<WCHAR> buffer. Then, calling the str() method on that buffer will give a basic_string<WCHAR> (as opposed to a std::string, which is just std::basic_string<char>). Finally, calling the c_str() method on your basic_string<WCHAR> will give a const WCHAR*, which is another name for LPCWSTR, and will be usable whenever an API requests a LPWSTR, i.e. a WCHAR*.

Domenic
+1  A: 

Also, you might want to consider using TCHAR throughout... If I'm correct, the idea would be something like this:

typedef std::basic_string<TCHAR> tstring

// Make any methods you control return tstring values. Thus, you could write:
tstring command = obj.getInstallationPath();
CreateProcess(x, command.c_str(), ...);

Note that we use CreateProcess instead of CreateProcessW or CreateProcessA. The idea is that if UNICODE is defined, then TCHAR is typedefed to WCHAR and CreateProcess is #defined to be CreateProcessW, which accepts a LPWSTR; but if UNICODE is not defined, then TCHAR becomes char, and CreateProcess becomes CreateProcessA, which accepts a LPSTR. But I might not have the details right here... this stuff seems somewhat needlessly complicated :(.

Domenic