PathIsDirectory woes!

Here's what MSDN has to say about PathIsDirectory's return value:

Returns TRUE if the path is a valid directory, or FALSE otherwise.

One of the architectural goals for the product that I am working on is the enabling of cross platform source code portability with minimal development effort. To this extent we have a practice of wrapping platform specific API calls via simple functions that mostly just forward the call to the OS API. In this spirit therefore I wrote the simplest of wrapper routines for the PathIsDirectory Win32 API like so (not the actual function name of course!).

bool IsThePathADangDirectory( const TCHAR *pszPath )
{
    return ( PathIsDirectory( pszPath ) == TRUE );
}

I had a perfectly normal directory at the location "C:\WINDDK" and guess what this helper routine returned when I called it like this?

_tprintf( _T( "%d\n" ),
      IsThePathADangDirectory( _T( "C:\\WINDDK" ) ) );

It returned a big fat false! As it turns out PathIsDirectory does not in fact return TRUE when the path is valid. It returns instead, a non-zero value. The difference is important because in this case it returns the number 16 when it is happy with the path! Since we were explicitly checking for the return value TRUE, IsPathADangDirectory dutifully reported that it wasn't a dang directory! The right way to write the function would therefore be,

bool IsThePathADangDirectory( const TCHAR *pszPath )
{
    return !( PathIsDirectory( pszPath ) == FALSE );
}

Grrr... Trust no one I say, not even the documentation!

comments powered by Disqus