Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FindFirstFile fails on the root path

I'm using the following code to obtain information about a file system directory:

LPCTSTR pStrPath = L"D:\\1";
WIN32_FIND_DATA wfd;
HANDLE hDummy = ::FindFirstFile(pStrPath, &wfd);
if(hDummy != INVALID_HANDLE_VALUE)
{
    //Use 'wfd' info
    //...

    ::FindClose(hDummy);
}
else
{
    int error = ::GetLastError();
}

The code works just fine, unless I specify a root path:

  • D:\ - error code ERROR_FILE_NOT_FOUND
  • D: - error code ERROR_FILE_NOT_FOUND
  • \\SRVR-1\share - error code ERROR_BAD_NET_NAME
  • \\SRVR-1\share\ - error code ERROR_BAD_NET_NAME
  • \\SRVR-1\HiddenShare$ - error code ERROR_BAD_NET_NAME

But it works in the following cases:

  • D:\1 - no error
  • \\SRVR-1\share\1 - no error
  • \\SRVR-1\HiddenShare$\1 - no error

Any idea why?

like image 554
c00000fd Avatar asked Feb 13 '14 00:02

c00000fd


1 Answers

FindFirstFile() is meant to be used to enumerate the contents of a directory. As such it is meant to be used with a file pattern, such as D:\*.

When you use D:\1 you are just using a very restrictive file pattern (1) to filter the files in D:\, but when you use just D:\ or D: there is no pattern at all!

And the same is true for shared resources. Note that \\SRV-1\share does not count as a pattern, because \\SRV-1 cannot be considered a directory.

like image 115
rodrigo Avatar answered Sep 25 '22 14:09

rodrigo