Cant convert parameter from char [#] to LPWSTR

When I compile this code in Visual C ++, I got the following error. May help me solve this problem.

DWORD nBufferLength = MAX_PATH;
char szCurrentDirectory[MAX_PATH + 1];
GetCurrentDirectory(nBufferLength, szCurrentDirectory); 
szCurrentDirectory[MAX_PATH +1 ] = '\0';

Error message:

Error   5   error C2664: 'GetCurrentDirectoryW' : cannot convert parameter 2 from 'char [261]' to 'LPWSTR'  c:\car.cpp
+3
source share
2 answers

Your program is configured to compile as unicode. This is why GetCurrentDirectory is the GetCurrentDirectoryW that expects LPWSTR( wchar_t*).

GetCurrentDirectoryW expects wchar_tinstead of an array char. You can do this using TCHAR, which, like GetCurrentDirectory, depends on the unicode parameter and always represents the corresponding character type.

Remember to add '\0'to Lto make literal unicode char too!

+5
source

, UNICODE, _UNICODE. szCurrentDirectory char TCHAR.

+5

Source: https://habr.com/ru/post/1793799/


All Articles