简体   繁体   中英

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

#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.

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.

You are calling function that expects wide character string ( FindFirstFileW ). You either change file to use wchar_t* file = L"d:\\tester"; or use an ASCII version of the function FindFirstFileA .

You are compiling with UNICODE defined and yet passing an ANSI string as your first parameter. Replace your line that beings char * with

TCHAR *file=TEXT("d:\tester");

and things should be fine.

Martyn

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM