Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cant convert parameter from char[#] to LPWSTR

Tags:

visual-c++

When I compile this code in Visual C++, I got the below error. Can help me solve this issue..

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
like image 992
karikari Avatar asked Dec 29 '22 01:12

karikari


1 Answers

Your program is configured to be compiled as unicode. Thats why GetCurrentDirectory is GetCurrentDirectoryW, which expects a LPWSTR (wchar_t*).

GetCurrentDirectoryW expects a wchar_t instead of char array. You can do this using TCHAR, which - like GetCurrentDirectory - depends on the unicode setting and always represents the appropriate character type.

Don't forget to prepend your '\0' with an L in order to make the char literal unicode, too!

like image 66
Timbo Avatar answered Jan 13 '23 10:01

Timbo