Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: argument of type char* is incompatible with parameter of type LPCWSTR

Tags:

c++

visual-c++

#include <windows.h>
#include <iostream>
using namespace std;
int main() {
char* file="d:/tester";
WIN32_FIND_DATA FindFileData;
    HANDLE hFind;
hFind = FindFirstFile(file, &FindFileData);  // line of error says argument of type char* is incompatible with parameter of type LPCWSTR
}

I can't understand the error.What is it and how can I solve the error?

I am making a console app and need to check if files are in there in the directory .

like image 735
Suhail Gupta Avatar asked Jul 04 '11 06:07

Suhail Gupta


1 Answers

the type LPCWSTR is a const pointer to wide char

the file in char* file="d:/tester"; is a pointer to an ordinary char

Ordinary char usually uses 1 byte of memory, while wide char usually uses 2 bytes. What will happen if the file name contains cyrillic or japanese letters? You will not be able to open it without specifying the encoding. Windows API accepts wide chars to FindFirstFile function, because file name can be in a unicode. So, if you write L"foo_bar" the compiler will interpret it as wide character string. Therefore you can write wchar_t* file = L"d:\\tester"; to match parameter types, so compilation will be successful.

like image 70
rmflow Avatar answered Sep 27 '22 21:09

rmflow