How to convert char to wchar_t*?

821 views Asked by At

As the title states I have a simple char that retrieves a full path name of a file I am looking for and I need to convert it to const wchar_t. How can I achieve this? Here is an example of the code:

int main()
{
    char filename[] = "poc.png";
    char fullFilename[MAX_PATH];
    GetFullPathName(filename, MAX_PATH, fullFilename, nullptr);
    const wchar_t *path = fullFilename;
}

As you can see I am trying to get the filename to convert but I couldn't find a way to do so. What would be the most simple solution to this?

1

There are 1 answers

1
tenfour On BEST ANSWER

Your code doesn't show any need to convert between char and wchar_t. Most likely you don't actually need to convert the character types. If you want to use the wchar_t-friendly GetFullPathNameW, then just use wchar_t instead of char.

int main()
{
    wchar_t fullFilename[MAX_PATH];
    GetFullPathNameW(L"poc.png", MAX_PATH, fullFilename, nullptr);
    const wchar_t *path = fullFilename;
    return 0;
}

If you really do need to convert between wchar_t-based C-style strings and char-based C-style strings, then you can use the APIs MultiByteToWideChar and WideCharToMultiByte.