I have a small piece of code that is meant to list the contents of a directory using the Windows API.
CODE:
#include <iostream>
#include <Windows.h>
int wmain() {
WIN32_FIND_DATA data;
std::string dir = "c:\\* ";
HANDLE hFind = FindFirstFileA(dir.c_str(), &data); // DIRECTORY
if (hFind != INVALID_HANDLE_VALUE) {
do {
std::cout << data.cFileName << std::endl;
} while (FindNextFileW(hFind, &data));
FindClose(hFind);
}
return 0;
}
Error:
Error C2664 'HANDLE FindFirstFileA(LPCSTR,LPWIN32_FIND_DATAA)': cannot convert argument 2 from 'WIN32_FIND_DATA *' to 'LPWIN32_FIND_DATAA'
The errors change depending on my attempts at fixes. I have tried the following:
- Tried to change
FindFirstFile
toFindFirstFileW
/FindNextFileA
- Tried to put an
L
in front of directory stringL"C:\\*"
I have looked at the following related questions:
- Listing directory contents using C and Windows
- How to list files in a directory using the Windows API?
But did not get it to successfully list the contents. Closest I have got is this code which compiles without errors:
#include <windows.h>
#include <iostream>
int main()
{
WIN32_FIND_DATA data;
HANDLE hFind = FindFirstFileW(L"C:\\*", &data);
if (hFind != INVALID_HANDLE_VALUE) {
do {
std::cout << data.cFileName << std::endl;
} while (FindNextFileW(hFind, &data));
FindClose(hFind);
}
}
This compiles but when I run the binary, it outputs this:
0000009A9DBAF32C 0000009A9DBAF32C 0000009A9DBAF32C 0000009A9DBAF32C 0000009A9DBAF32C 0000009A9DBAF32C 0000009A9DBAF32C 0000009A9DBAF32C 0000009A9DBAF32C 0000009A9DBAF32C 0000009A9DBAF32C 0000009A9DBAF32C 0000009A9DBAF32C 0000009A9DBAF32C 0000009A9DBAF32C
While we are at it, what's the difference between FindNextFile
, FindNextFileA
and FindNextFileW
? I feel like the issue is somehow related to these?