Fog Creek Software
Discussion Board




Listing directory via Win32, C++

I would be most grateful if someone can enlighten me on
how to do this. I can find nothing on google, and after
checking MSDN I tried the following code snippet, but it falls
over:

#include <iostream.h>
#include <windows.h>

void main(void)
{
LPWIN32_FIND_DATA myResults;
HANDLE            myList;
LPCTSTR          myPath;
char*            temp;
myList = FindFirstFile("C:\\", myResults);
cout<<(myResults->cFileName); //this line causes errors
FindClose(myList);
}

Curious
Wednesday, December 25, 2002

I don't have any experiance with Win32 (Well I do, but so long ago as it not mattering and I don't have a windows dev box around), but I can take a stab at it. Take with a grain or 10e6 of salt, though.

My guess is this: that cout<< line causes an error because one of myResults or myResults->cFileName is NULL. Some error checking is in order, I think.

A quick look at MSDN makes me think that you need to search for the pattern "C:\\*" instead of "C:\\".

I'm guessing that you're just not finding any files and the FindFirstFile call is just failing because there are zero matches. But the MSDN page on that function seems unclear to me on the behavior when no files are present.

A slight aside: Even when writing quick tests or quick 'n dirty hacks like this, use some error checking. I have often fallen into the trap of not using it because "Well, it has to work!", and then been busted into writing the code anyway because I get errors.

Merry Christmas, if you celebrate it. And have a merry today anyway, even if you don't!

Mike Swieton
Wednesday, December 25, 2002

This line:

LPWIN32_FIND_DATA myResults;

allocates a pointer to a WIN32_FIND_DATA structure.

You should change it to:

WIN32_FIND_DATA myResults;

and then change the function call to:

myList = FindFirstFile("C:\\", &myResults);

Too-Many Windows Books
Wednesday, December 25, 2002

This is covered very clearly in MSDN, including sample source:

http://msdn.microsoft.com/library/en-us/fileio/base/findfirstfile.asp?frame=true

The other guys have already pointed out some of the errors in your code.

The Anti-Bella
Wednesday, December 25, 2002

Also, when asking for code help, just saying "This causes errors" doesn't help. Please say WHAT the errors are. Compile errors? If so, what are they? Runtime errors? What happens?

Chris Tavares
Thursday, December 26, 2002

Many thanks for your advice!

Curious
Thursday, December 26, 2002

In the spirit of Christmas, here is my directory-listing-on-Win32 code, feel free to use it. I use custom Array/String classes but it should be easy to figure out :)

void lsdir(const char *path, Array<String> &results)
{
        WIN32_FIND_DATA fdata;
        HANDLE dhandle;

        // must append \* to the path
        {
                char buf[MAX_PATH];
                snprintf(buf, sizeof(buf), "%s\\*", path);
                if((dhandle = FindFirstFile(buf, &fdata)) == INVALID_HANDLE_VALUE) {
                        throw Error("FindFirstFile");
                }
        }

        // even an "empty" directory will give two results - . and ..
        results.append(String(fdata.cFileName));

        while(1) {
                if(FindNextFile(dhandle, &fdata)) {
                        results.append(String(fdata.cFileName));
                } else {
                        if(GetLastError() == ERROR_NO_MORE_FILES) {
                                break;
                        } else {
                                FindClose(dhandle);
                                throw Error("FindNextFile");
                        }
                }
        }

        if(FindClose(dhandle) == 0) {
                throw Error("FindClose");
        }
}

Dan Maas
Thursday, December 26, 2002

Wow! Thanks!

Curious
Friday, December 27, 2002

*  Recent Topics

*  Fog Creek Home