15

Can anyone help in converting string to LPWSTR

string command=obj.getInstallationPath()+"<some string appended>"  

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

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

Thanks in Advance

CraftedGaming
  • 499
  • 7
  • 21
Cute
  • 13,643
  • 36
  • 96
  • 112

5 Answers5

12

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
  • 951,095
  • 183
  • 1,149
  • 1,285
6

Another way:

mbstowcs_s

use with string.c_str(), you can find example here or here

OR

USES_CONVERSION_EX;

std::string text = "text";
LPWSTR lp = A2W_EX(text.c_str(), text.length());

OR

{
    std::string str = "String to LPWSTR";
    BSTR b = _com_util::ConvertStringToBSTR(str.c_str());
    LPWSTR lp = b;
    Use lp before SysFreeString...
    SysFreeString(b);
}
Community
  • 1
  • 1
Cobaia
  • 1,503
  • 3
  • 22
  • 41
5

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
  • 51,617
  • 12
  • 104
  • 148
3

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
  • 110,262
  • 41
  • 219
  • 271
  • Agreed, I always use tchar for this kind of thing. – derpface Aug 28 '12 at 23:54
  • 1
    Only use TCHAR for converting ancient windows software. The only ones having good reason to keep using it are microsoft documentation writers, when they have both an A and a W function. – Deduplicator Aug 15 '14 at 11:32
1

Here is another option. I've been using this function to do the conversion.

//C++ string to WINDOWS UNICODE string
std::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;
}

And wrap your string like this. s2ws( volume.c_str())

Joel
  • 1,309
  • 2
  • 10
  • 20
  • If that has an exception, it will create a memory leak. – Jonathan Jan 25 '13 at 23:59
  • @Jonathan The memory leak can be fixed by using `std::vector buf(len);` instead of the `new` line. – M.M Aug 15 '14 at 11:29
  • also there is a problem that if `s.size() > INT_MAX - 1` you'll get bogus results at best ; to make this function robust it should check for that and throw an exception or take some other well-defined action – M.M Aug 15 '14 at 11:30