Nerdworks logo "The nerd shall inherit the earth."

Nerdworks Blogorama

Nerdspeak

PathIsDirectory woes!
Technobabble
4/18/2007 1:21:32 PM  

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!

Link Comment (2)
 
blogorama home
about this blog
email the author
where on earth am i?
subscribe to mailing list
feeds Use these links for feed syndication
rss  |  atom
by category
technobabble (60)
philosophical crud (3)
irrelevant stuff (7)
archive
november, 2011 (2)
october, 2011 (1)
september, 2011 (7)
july, 2011 (3)
june, 2011 (2)
may, 2011 (3)
april, 2011 (1)
march, 2011 (1)
february, 2011 (1)
february, 2010 (1)
october, 2009 (1)
september, 2009 (1)
july, 2009 (5)
march, 2009 (2)
august, 2008 (2)
march, 2008 (1)
january, 2008 (1)
september, 2007 (2)
april, 2007 (1)
february, 2007 (2)
december, 2006 (1)
october, 2006 (1)
september, 2006 (4)
august, 2006 (3)
july, 2006 (4)
june, 2006 (3)
may, 2006 (6)
april, 2006 (2)
recent entries
Implementing variab...
Debugging existing...
Screen scraping wit...
Building an Instagr...
Building an Instagr...
Organizing your Jav...
297927 hits