1

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?

D. Absolut
  • 13
  • 3
  • You can simply use Unicode version of Win API functions as described by @tenfour in any case if you still need to convert (for some other reason) You can check [this](https://stackoverflow.com/questions/49697488/how-to-convert-utf8-char-array-to-windows-1252-char-array/49697722#49697722) in case of Windows you can use [MultiByteToWideChar](https://learn.microsoft.com/en-us/windows/win32/api/stringapiset/nf-stringapiset-multibytetowidechar) – Victor Gubin Oct 14 '20 at 19:25
  • 1
    Step 1: Find out what narrow character encoding the source uses. Step 2: Find out which wide character encoding you want to produce. Step 3: Implement a conversion function between those two character encodings. – eerorika Oct 14 '20 at 19:29

1 Answers1

2

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.

tenfour
  • 36,141
  • 15
  • 83
  • 142
  • 3
    Keep in mind that the value GetFullPathNameW returns indicates whether or not the buffer was big enough to hold the result. (The path can be much longer than MAX_PATH.) – Eljay Oct 14 '20 at 19:39