-
FindFirstFile problem
Can someone tell me what's wrong with his code?
This is a direct modification of the sample code from msdn. It's a simple directory listing.
I'm using Visual Studio 2005
Code:
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#define _WIN32_WINNT 0x0501
WIN32_FIND_DATA FindFileData;
HANDLE hFind = INVALID_HANDLE_VALUE;
DWORD dwError;
char DirSpec[] = "C:\*";
DWORD dwBufferLenght;
printf ("Target directory is %s.\n",DirSpec);
printf ("Size of name dir %d\n",dwBufferLenght);
/*########################### PROBLEM: type mismatch with *DirSpec* variable*/
/*IF TYPECAST, IT COMILES BUT DOESN'T WORK*/
// Find the first file in the directory.
hFind = FindFirstFile((LPCWSTR)DirSpec, &FindFileData);
/*###########################*/
if (hFind == INVALID_HANDLE_VALUE)
{
printf ("Invalid file handle. Error is %u.\n", GetLastError());
return (-1);
}
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);
}
dwError = GetLastError();
FindClose(hFind);
if (dwError != ERROR_NO_MORE_FILES)
{
printf ("FindNextFile error. Error is %u.\n", dwError);
return (-1);
}
}
-
>> IT COMILES BUT DOESN'T WORK
If C:\ is an NTFS partition, it is probably hanging up on the hidden, system folder called: System Volume Information
-
> PROBLEM: type mismatch with *DirSpec* variable*/
> /*IF TYPECAST, IT COMILES BUT DOESN'T WORK*/
That compiles? You have a code fragment just floating there, not even within a function.
Why don't you post code that we can actually compile, and explain in detail what "doesn't work" means.
-
Code:
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#define _WIN32_WINNT 0x0501
int main(int argc, char *argv[]) <========== CHANGE
{
WIN32_FIND_DATA FindFileData;
HANDLE hFind = INVALID_HANDLE_VALUE;
DWORD dwError;
char DirSpec[] = "C:\*";
DWORD dwBufferLenght;
printf ("Target directory is %s.\n",DirSpec);
printf ("Size of name dir %d\n",dwBufferLenght);
// Find the first file in the directory.
hFind = FindFirstFile((LPCWSTR)DirSpec, &FindFileData);
if (hFind == INVALID_HANDLE_VALUE)
{
printf ("Invalid file handle. Error is %u.\n", GetLastError());
return (-1);
}
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);
}
dwError = GetLastError();
FindClose(hFind);
if (dwError != ERROR_NO_MORE_FILES)
{
printf ("FindNextFile error. Error is %u.\n", dwError);
return (-1);
}
}
} <=========CHANGE
Now it compiles. *FindFirstFile()* doesn't return a valid handle. The function exits with the error FILE_NOT_FOUND. At first I thougt that it might be data type issue so I tryed calling a searchpath through GetCurrentDirectory() at passing it to FindFirsFile() but that didn't work.
I tryied other paths, non-root directories but still no result. All drives were NTFS.
-
And you don't get any warnings? Perhaps on this line:
Code:
char DirSpec[] = "C:\*";
And these lines (which could cause an assertion error or runtime check failure)
Code:
DWORD dwBufferLenght;
//...
printf ("Size of name dir %d\n",dwBufferLenght);
As for the first problem, two things:
1) You're compiling with UNICODE defined (I think this is default in VC++ 2005). To fix this you should use an array of TCHAR or LPCWSTR and use the TEXT macro around your string literal:
Code:
TCHAR DirSpec[] = TEXT("C:/*");
2) To use a backslash in a string use the escape sequence '\\' (you could also use a forward slash instead)
There may be other problems as well
-
Thanks :), I did like you suggested and it worked, FindFirstFile() returns a valid handle, but when it printf the filename for some reason it printf only the first character.
Also when I use GetFullPathName() and pass the returned string to FindFirstFile(), I again get the same FILE_NOT_FOUND error.
This is the main goal of my project, to scan the directory of a running proccess for files.
Code:
TCHAR DirSpec[520];
DWORD dwBufferLenght = 0;
TCHAR lpBuffer[] = TEXT("E://My Documents//Visual Studio 2005//Projects//repl//debug//file.exe");
if ((GetFullPathName(lpBuffer,dwBufferLenght,DirSpec,NULL) ==0)){
printf("GetFullPathName error: %d",GetLastError());
}
/*###########################################################*/
hFind = FindFirstFile(DirSpec, &FindFileData);
printf ("Size of name file %d\n",sizeof(FindFileData.cFileName));
if (hFind == INVALID_HANDLE_VALUE)
{
printf ("Invalid file handle. Error is %u.\n", GetLastError());
return (-1);
}
else
{
printf ("First file name is %s.\n", FindFileData.cFileName);
-
Bookmark MSDN
http://msdn.microsoft.com/library/de...characters.asp
Since you're compiling with UNICODE enabled, you need either
Code:
printf ("First file name is %S.\n", FindFileData.cFileName); // Note capital S
wprintf ("First file name is %s.\n", FindFileData.cFileName); // Note lowercase s
-
It turns out dwBufferLenght in GetFullPathName() was initialiazed to 0 and I didn't change it.
Thanks for the help everyone :D