FindFirstFile is a macro defined as follows:
#ifdef UNICODE
#define FindFirstFile FindFirstFileW
#else
#define FindFirstFile FindFirstFileA
#endif // !UNICODE
What this means is that it expands to the one with a W
when compiled with UNICODE
defined, and it expands to the one with an A
otherwise.
Now, FindFirstFile
's first parameter is either LPCSTR
or LPWCSTR
. LPCSTR
is a typedef for const char*
while LPWCSTR
is a typedef for const wchar_t*
. In your error message, you try passing a type of const char*
as the first argument to FindFirstFileW
which takes an argument of type const wchar_t*
, hence the error.
In order for the types to match up, you need to pass an object of type const wchar_t*
, you have several options:
std::wstring path1 = L"..."; // 1
const wchar_t* path2 = L"..."; // 2
wchar_t path3[] = L"..."; // 3
WIN32_FIND_DATA w32fd;
FindFirstFile(path1.c_str(), &w32fd); // 1
FindFirstFile(path2, &w32fd); // 2
FindFirstFile(path3, &w32fd); // 3
FindFirstFile(L"...", &w32fd);
How do I take a filename of type const char* and put it into a form
that FindFirstType will accept?
If your filename only contains characters in the basic ASCII character set, then you can convert it to a std::wstring
like the following: std::wstring path(std::begin(filename), std::end(filename));
. Otherwise, you would need to use MultiByteToWideChar or many of the options shown here. Another option would be to call FindFirstFileA
directly, but if you are on windows, it generally would be better to use wchar_t
to begin with.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…