Clarification of SearchPath() Return Value

ID: Q115826


The information in this article applies to:
  • Microsoft Win32 Application Programming Interface (API), included with:
    • Microsoft Windows NT 3.1


SUMMARY

The entry for "SearchPath()" in the "Win32 Programmer's Reference" says that the return value will be one of the following:

  • The length (in characters) of the string copied to the buffer.

    -or-


  • 0 if the function fails [see "GetLastError()" in the "Win32 Programmer's Reference" for more information].


If the specified file is not found, then the return value is 0, but the value retrieved by GetLastError() is not changed.

In order to distinguish the "file not found" condition from another error (out of memory, invalid parameter, and so forth), call SetLastError() with a value of NO_ERROR before calling SearchPath().


MORE INFORMATION

This behavior is by design. The recommended way to check the return status for SearchPath() is:


   return value >  buffer length    buffer too small
   return value =  0                file not found or another error
   return value <= buffer length    file found   
Handle the case where the return value is 0 as follows:

   TCHAR  szFilename[] = "MyFile.Txt";
   TCHAR  szPathname[MAX_PATH];
   LPTSTR lpszFilename;

   SetLastError( NO_ERROR );
   if( !SearchPath( NULL, szFilename, NULL, MAX_PATH, szPathname,
                    &lpszFilename ) )
   {
      if( GetLastError() == NO_ERROR )
         Display( "File not found." );
      else
         Display( "SearchPath failed!" );
   } 

Additional query words: 3.10


Keywords          : 
Version           : winnt:3.1
Platform          : winnt 
Issue type        : 


Last Reviewed: October 19, 1999
© 1999 Microsoft Corporation. All rights reserved. Terms of Use.