Thread: FindFirstFile problem

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    15

    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);
          }
       }

  2. #2
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    >> 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

  3. #3
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    > 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.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    15
    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.

  5. #5
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    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
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    15
    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);

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Feb 2006
    Posts
    15
    It turns out dwBufferLenght in GetFullPathName() was initialiazed to 0 and I didn't change it.

    Thanks for the help everyone

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM