69

Is there any method? My computer is AMD64.

::std::string str;
BOOL loadU(const wchar_t* lpszPathName, int flag = 0);

When I used:

loadU(&str);

the VS2005 compiler says:

Error 7 error C2664:: cannot convert parameter 1 from 'std::string *__w64 ' to 'const wchar_t *'

How can I do it?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
user25749
  • 4,825
  • 14
  • 61
  • 83

5 Answers5

112

First convert it to std::wstring:

std::wstring widestr = std::wstring(str.begin(), str.end());

Then get the C string:

const wchar_t* widecstr = widestr.c_str();

This only works for ASCII strings, but it will not work if the underlying string is UTF-8 encoded. Using a conversion routine like MultiByteToWideChar() ensures that this scenario is handled properly.

Yoav Feuerstein
  • 1,925
  • 2
  • 22
  • 53
marijne
  • 2,992
  • 5
  • 22
  • 21
  • 4
    This should work fine for ASCII strings, but it will not work if the underlying string is UTF-8 encoded. Using a conversion routine like MultiByteToWideChar() ensures that this scenario is handled properly. – Matt Dillard Oct 29 '08 at 13:50
  • You should use MultiByteToWideChar with correct codepage. To be precise, two calls to it are needed: the first to get the required length of the target wchar_t string and the second to convert the char string to the wchar_t string. – Johann Gerell Oct 30 '08 at 08:56
  • It will only work if wchar_t is Unicode and char is Latin-1 or ASSCII. The first 256 character values of Unicode exactly match Latin-1; the first 128 characters of Latin-1 exactly match ASCII. – MSalters Oct 30 '08 at 10:07
  • This was the quick fix I needed for my situation. Thanks! – chaosTechnician May 31 '11 at 18:54
  • 1
    cannot convert ‘const char*’ to ‘wchar_t*’ – Hani Goc Dec 08 '15 at 13:46
  • 1
    this gives me an error: `cannot convert from 'const wchar_t *' to 'wchar_t *'` I had to use: `const wchar_t* widecstr = widestr.c_str();` – Wakan Tanka Feb 15 '17 at 13:16
  • @MattDillard If this answer is wrong, you could post a correct one ... – Bersan Jul 13 '22 at 13:46
  • 1
    @Bersan - I did, 14 years ago... :-) – Matt Dillard Jul 14 '22 at 16:54
48

If you have a std::wstring object, you can call c_str() on it to get a wchar_t*:

std::wstring name( L"Steve Nash" );
const wchar_t* szName = name.c_str();

Since you are operating on a narrow string, however, you would first need to widen it. There are various options here; one is to use Windows' built-in MultiByteToWideChar routine. That will give you an LPWSTR, which is equivalent to wchar_t*.

arkon
  • 2,209
  • 3
  • 27
  • 36
Matt Dillard
  • 14,677
  • 7
  • 51
  • 61
10

You can use the ATL text conversion macros to convert a narrow (char) string to a wide (wchar_t) one. For example, to convert a std::string:

#include <atlconv.h>
...
std::string str = "Hello, world!";
CA2W pszWide(str.c_str());
loadU(pszWide);

You can also specify a code page, so if your std::string contains UTF-8 chars you can use:

CA2W pszWide(str.c_str(), CP_UTF8);

Very useful but Windows only.

Rob
  • 76,700
  • 56
  • 158
  • 197
  • You probably want CA2CW. You might also want to add a basic explanation for the macro naming convention (eg: Const Ascii -> Const Wchar). – Nick Oct 29 '08 at 15:48
  • There is no CA2CW conversion function. There is a CA2CT version but this simply calls CA2W. See for a full list. – Rob Oct 30 '08 at 08:58
  • Incredibly useful. In my case, only `#include ` worked, while `#include ` didn't. Dunno why. – ceztko Jul 16 '11 at 07:17
  • I would also recommend `CA2WEX` instead, which bypasses potential stack overruns. – Mooing Duck Mar 12 '15 at 20:47
5

If you are on Linux/Unix have a look at mbstowcs() and wcstombs() defined in GNU C (from ISO C 90).

  • mbs stand for "Multi Bytes String" and is basically the usual zero terminated C string.

  • wcs stand for Wide Char String and is an array of wchar_t.

For more background details on wide chars have a look at glibc documentation here.

kriss
  • 23,497
  • 17
  • 97
  • 116
  • 1
    I believe those are in C standard; either way, they're in Windows too: https://msdn.microsoft.com/en-us/library/k1f9b8cy.aspx – Mooing Duck Mar 12 '15 at 20:48
0

Need to pass a wchar_t string to a function and first be able to create the string from a literal string concantenated with an integer variable.

The original string looks like this, where 4 is the physical drive number, but I want that to be changeable to match whatever drive number I want to pass to the function

auto TargetDrive = L"\\\\.\\PhysicalDrive4";

The following works

int a = 4;


std::string stddrivestring = "\\\\.\\PhysicalDrive" + to_string(a);

std::wstring widedrivestring = std::wstring(stddrivestring.begin(), stddrivestring.end());

const wchar_t* TargetDrive = widedrivestring.c_str();
Grantg
  • 11
  • 3