Reputation:
I have implemented the following function to list the contents of a directory but when I compile and linked the program, the compiler gives the following two errors to me:
error C2664: 'HANDLE FindFirstFileW(LPCWSTR,LPWIN32_FIND_DATAW)': cannot convert argument 2 from 'LPWIN32_FIND_DATA *' to 'LPWIN32_FIND_DATAW'
error C2664: 'BOOL FindNextFileW(HANDLE,LPWIN32_FIND_DATAW)': cannot convert argument 2 from 'LPWIN32_FIND_DATA *' to 'LPWIN32_FIND_DATAW'
How can I fix this code to work with the Unicode format:
bool FmListDirectoryContents(LPWSTR arg_path_dir)
{
LPWIN32_FIND_DATA wfd_Data;
wchar_t c_Path[2048];
wprintf(c_Path, "%s\\*", arg_path_dir);
HANDLE h_Find = FindFirstFileW(c_Path, &wfd_Data);
if (h_Find != INVALID_HANDLE_VALUE) {
PrintColorful(5, L"\n\t%s\t\t\t%s", "-----------", "-----------");
PrintColorful(5, L"\n\t%s\t\t\t%s", "File Size", "File Name");
PrintColorful(5, L"\n\t%s\t\t\t%s\n", "-----------", "-----------");
do {
ULONGLONG FileSize = wfd_Data->nFileSizeHigh;
FileSize <<= sizeof(wfd_Data->nFileSizeHigh) * 8;
FileSize |= wfd_Data->nFileSizeLow;
printf("\n\t%llu\t\t\t\t%s", FileSize, wfd_Data->cFileName);
} while (FindNextFileW(h_Find, &wfd_Data));
printf("\n\n");
FindClose(h_Find);
}
return true;
}
Upvotes: 0
Views: 1225
Reputation: 595402
You are passing FindFirstFileW()
and FindNextFileW()
a pointer to a pointer to a WIN32_FIND_DATA
, but that is not what they are expecting, hence the compiler errors. They want a pointer to a WIN32_FIND_DATAW
instead.
You are also misusing printf()
and wprintf()
.
Try this:
bool FmListDirectoryContents(LPWSTR arg_path_dir)
{
WIN32_FIND_DATAW wfd_Data; // <-- not LPWIN32_FIND_DATA!
wchar_t c_Path[2048];
swprintf(c_Path, L"%s\\*", arg_path_dir); // <-- not wprintf!
HANDLE h_Find = FindFirstFileW(c_Path, &wfd_Data);
if (h_Find != INVALID_HANDLE_VALUE) {
PrintColorful(5, L"\n\t%s\t\t\t%s", "-----------", "-----------");
PrintColorful(5, L"\n\t%s\t\t\t%s", "File Size", "File Name");
PrintColorful(5, L"\n\t%s\t\t\t%s\n", "-----------", "-----------");
do {
ULARGE_INTEGER FileSize;
FileSize.HighPart = wfd_Data->nFileSizeHigh;
FileSize.LowPart = wfd_Data->nFileSizeLow;
wprintf(L"\n\t%llu\t\t\t\t%s", FileSize.QuadPart, wfd_Data->cFileName); // <-- not printf!
}
while (FindNextFileW(h_Find, &wfd_Data));
FindClose(h_Find);
wprintf(L"\n\n");
}
return true;
}
Upvotes: 0
Reputation: 75062
You choose that you will specify to use Unicode API manually, so you have to use LPWIN32_FIND_DATAW
(with explicit suffix W
) instead of LPWIN32_FIND_DATA
.
Also, the 2nd argument of FindFirstFileW
and FindNextFileW
are type LPWIN32_FIND_DATAW
, so it is wrong to pass a pointer to LPWIN32_FIND_DATAW
.
LPWIN32_FIND_DATAW
is already a pointer to WIN32_FIND_DATAW
, so you have to allocate a variable having type WIN32_FIND_DATAW
and pass a pointer to that.
references:
Upvotes: 1