Thread: FindFirstFile() Always Returns INVALID_HANDLE_VALUE

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    66

    FindFirstFile() Always Returns INVALID_HANDLE_VALUE

    I'm trying to create a console application that reads all files in a directory, and count the total line count in it with FindFirstFile() but it always returns an INVALID_HANDLE_VALUE.
    Can someone help me to solve this problem?

    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <vector>
    #include <windows.h> 
    
    #ifndef WIN32_LEAN_AND_MEAN
    #define WIN32_LEAN_AND_MEAN
    #endif
    
    #define NUM_EXTENSIONS 7
    
    const char *Extensions[NUM_EXTENSIONS] = 
    {
    	"\\*.c", "\\*.h", "\\*.cpp", "\\*.cc", "\\*.cxx", "\\*.hh", "\\*.hpp"
    };
    
    int main(int argc, char *argv[])
    {
            HANDLE hFind;
    	int ErrorCode;
            WIN32_FIND_DATA FindData;
    	std::vector<std::string> FileList;
    	
    	std::cout << "Source Code Line Counter For C/C++\n";
    
    	int FileCount = 0;
    	for(unsigned i = 0;i < NUM_EXTENSIONS;++i)
    	{
    		hFind = FindFirstFile(Extensions[i], &FindData);

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Do you have many source code files in your c:\ root directory? (I put a .c file in there, just to test, and sure enough now I only get six invalid handles.)

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    66
    Well, not many only about 6 .cpp files, but if I change Extension[0] to \\*.cpp, it still returns an INVALID_HANDLE_VALUE.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Let me show what I did so that I could actually compile what you posted (since who knows what's in your for loop):
    Code:
    #include <stdio.h>
    #include <windows.h>
    
    #ifndef WIN32_LEAN_AND_MEAN
    #define WIN32_LEAN_AND_MEAN
    #endif
    
    #define NUM_EXTENSIONS 7
    
    const char *Extensions[NUM_EXTENSIONS] =
    {
    	"\\*.c", "\\*.h", "\\*.cpp", "\\*.cc", "\\*.cxx", "\\*.hh", "\\*.hpp"
    };
    
    int main(int argc, char *argv[])
    {
        HANDLE hFind;
        int ErrorCode;
        WIN32_FIND_DATA FindData;
    
    	printf("Source Code Line Counter For C/C++\n");
    
    	int FileCount = 0;
    	for(unsigned i = 0;i < NUM_EXTENSIONS;++i)
    	{
    		hFind = FindFirstFile(Extensions[i], &FindData);
    		if (hFind == INVALID_HANDLE_VALUE) {
    		    printf("Invalid handle for %s.\n", Extensions[i]);
    		}
    	}
    	return 0;
    }
    My root directory has (now, just for you) a .cpp file, but none of the others. I get six lines printed -- one for each extension other than .cpp. Does that work for you?

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    What is the value of GetLastError() after you get an INVALID_HANDLE_VALUE where you know there's a file with that extension?

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    To be honest, you are not correctly using the handles anyway. You need to use FindClose() when a valid one is found (which you never really check if that occurs). Also,

    Code:
    #ifndef WIN32_LEAN_AND_MEAN
    #define WIN32_LEAN_AND_MEAN
    #endif
    should occur prior to including windows.h. And no, this isn't effecting your code nor is it related to your bug. You are just misusing WIN32_LEAN_AND_MEAN.

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    66
    Well, I got seven lines.
    I have a .cpp file in my project folder of course.
    If I put a .cpp file in D: and changed Extension[0] to "D:\\*.cpp", then it works.
    I really don't know what's wrong with this, I tried to debug and hFind is always 0xfffffff.

    Btw, GetLastError() returns 2.

    Here's the actual code:
    Code:
    for(unsigned i = 0;i < NUM_EXTENSIONS;++i)
    	{
    		hFind = FindFirstFile(Extensions[i], &FindData);
    
    		if(hFind != INVALID_HANDLE_VALUE)
    		{
    			do
    			{
    				std::string newString = FindData.cFileName;
    				int Position = newString.find( Extensions[i] + 3 );
    				if( Position != std::string::npos && newString.length() - 3 >= Position)
    					FileList.push_back(newString);
    			}
    			while(FindNextFile(hFind, &FindData));
    			int ErrorCode;
    
    			if((ErrorCode = GetLastError()) != ERROR_NO_MORE_FILES)
    			{
    				std::cout << "Error when looking for files.\nError Code: " << ErrorCode << "\n";
    				FileList.clear();
    				return 0;
    			}
    
    			if (!FindClose(hFind))
    			{
    				ErrorCode = GetLastError();
    				std::cout << "FindClose() returned error code " << ErrorCode << std::endl;
    				FileList.clear();
    				return 0;
    			}
    		}
    	}
    And, how do I use WIN32_LEAN_AND_MEAN correctly?

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by ThLstN View Post
    Well, I got seven lines.
    I have a .cpp file in my project folder of course.
    What does your project folder have to do with anything? Your program is looking in C:\, not in your project folder.

  9. #9
    Registered User
    Join Date
    Jan 2008
    Posts
    66

    Unhappy

    Oh is it looking in C: ?
    Sorry, I didn't know that, I thought it was a relative path.
    So, how can I use a relative path?

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by ThLstN View Post
    Oh is it looking in C: ?
    Sorry, I didn't know that, I thought it was a relative path.
    So, how can I use a relative path?
    \*.cpp is an absolute path -- to the root directory (\), and then *.cpp there.
    If you don't want an absolute path, don't use \.

  11. #11
    Registered User
    Join Date
    Jan 2008
    Posts
    66
    Oh thanks, I didn't know that!
    Edit: Now it's working

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 03:27 AM
  2. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  3. SetFileTime returns invalid handle
    By leonv in forum Windows Programming
    Replies: 5
    Last Post: 01-21-2007, 06:57 PM
  4. Replies: 2
    Last Post: 04-12-2004, 01:37 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM