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!

 
yasuhiko yoshimura 1/26/2008 12:52:31 PM
Yes, that is a fact.
Here is my resolve:

if (PathIsDirectory(...) >= 1)

My email address ia valid after replacing '__@__' to '@'.
 
The Nerd 1/26/2008 3:35:10 PM
Thanks for your comment. Yes, that should work as well. Hopefully there aren't too many documentation gaffe's such as this in the MSDN!
 

Please fill this form and click on the "Submit" button to post a comment. All fields except the comment box are optional. You don't have to give me your name and email, but if you do then that might allow me to follow up with you on your comment. Also, I won't publish your email address here or anywhere else.

 
Your Name :
Your Email :
Your Comment :
   

What in your opinion do you get when you multiply the number 5 by the number 2?

Your answer will help me figure out whether you are human or a spam bot. If you're a spam bot I hope your kernel core dumps and your CPU bursts into flames.

   

Please click here to go back to the blog.

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 (54)
philosophical crud (3)
irrelevant stuff (7)
archive
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
IE9 web cast / Chen...
Partial function ap...
Web Camps, Virtual...
176873 hits