开发者

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

#include <windows.h>
#include <iostream>
using namespace std;
int main() {
ch开发者_如何学Goar* 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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜