bobthemac
bobthemac

Reputation: 1172

FindFirstFile Issues can't get any example to work.

I keep having issues with the FindFirstFile and FindNextFile I need to get them to list all dlls into an array but I cant get it to list any files. I have tried using and editing the example code from MSDN but that doesn't work either they pass the wrong type of variable to a function. The code I have now is below sorry if it's a mess but I am trying everything to get it to work. I was also using argv[1] because I believe that gives the directory of the .exe which is what I need because that were the dlls will be stored. I am completely confused by why all the examples I try don't work and why I can't amend them to work.

WIN32_FIND_DATA FindFileData;
HANDLE hFind = INVALID_HANDLE_VALUE;

string directorySearch = "E:\\My Documents\\Visual Studio 2010\\Projects\\SEP-Asignment-One\\Debug\\*";

// Find the first file in the directory.
hFind = FindFirstFile(LPCWSTR("E:\\My Documents\\Visual Studio 2010\\Projects\\SEP-Asignment-One\\Debug\\*"), &FindFileData);

if (hFind == INVALID_HANDLE_VALUE) 
{
  printf ("Invalid file handle. Error is %u.\n", GetLastError());
} 
else
{
  printf ("First file name is %s.\n", FindFileData.cFileName);

                    // List all the other files in the directory.
  while (FindNextFile(hFind, &FindFileData) != 0) 
  {
     printf ("Next file name is %s.\n", FindFileData.cFileName);
  }
  FindClose(hFind);

Any Help would be apreceated.

Upvotes: 1

Views: 9706

Answers (2)

Edward Clements
Edward Clements

Reputation: 5132

Instead of hFind = FindFirstFile(LPCWSTR("...")..., try hFind = FindFirstFile(_T("...")

Upvotes: 0

Cheers and hth. - Alf
Cheers and hth. - Alf

Reputation: 145204

Use std::wstring, wide string literals like L"Hello", and remember to defined UNICODE before including <windows.h> (but that's done by default in a Visual Studio project).

Upvotes: 4

Related Questions