Clarification of SearchPath() Return Value

Last reviewed: September 25, 1995
Article ID: Q115826
The information in this article applies to:
  • Microsoft Win32 Application Programming Interface (API) included with:

        - Microsoft Windows NT version 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 reference words: 3.10
KBCategory: kbprg
KBSubcategory: BseFileio


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: September 25, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.