Convert const char* to LPCWSTR

Flaviu_ 1,031 Reputation points
2020-10-11T14:07:25.347+00:00

If I have:

void SomeFunc(const char* name, int size)  

how can I convert const char* name to TCHAR (having size) in order to use it on OpenFileMapping as third parameter ? And all this without using CString. Can you help me ?

Developer technologies | C++
{count} votes

3 answers

Sort by: Most helpful
  1. Castorix31 90,686 Reputation points
    2020-10-11T14:11:38.78+00:00

    You can use MultiByteToWideChar

    0 comments No comments

  2. RLWA32 49,636 Reputation points
    2020-10-11T14:14:26.373+00:00

    TCHAR is either char or wchar_t depending on whether you are building for UNICODE or not. So maybe your are building for UNICODE but coding for a non-unicode character set.

    To convert char to wchar_t (UTF-16LE) -

    You can call the C library function mbstowcs-s-mbstowcs-s-l

    Another option is to call the Windows API function nf-stringapiset-multibytetowidechar

    0 comments No comments

  3. David Lowndes 4,726 Reputation points
    2020-10-11T18:38:35.393+00:00

    In addition to what others have correctly told you - if you're starting out with an ANSI string, your application is never going to be able to handle characters that are outside your code page. This may not be an issue for you, but if you might encounter file names with foreign characters, you should really address the source of the problem such that you can support all file names that Windows can - which means changing the source char * to be TCHAR * too.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.