Sample RAPI Program

The following code example shows the basics of initializing the RAPI client, making calls, and handling errors.

#include <stdio.h>
#include <rapi.h>      
#include <string.h>

void PrintDirectory( LPWSTR Path, UINT Indent )
{
   if ( !Path )
      return;

   DWORD          foundCount;
   LPCE_FIND_DATA findDataArray;

   WCHAR searchPath[MAX_PATH];
   wcscpy( searchPath, Path );
   wcscat( searchPath, L"*" );

   if ( !CeFindAllFiles( searchPath,
                           FAF_ATTRIBUTES | FAF_NAME,
                           &foundCount,
                           &findDataArray ) )
   {
      printf( "*** CeFindAllFiles failed. ***\n" );

      if ( CeGetLastError() != ERROR_SUCCESS )
         printf( "failure occurred on the HPC function\n" );

      return;
   }

   if ( !foundCount )
   {
      for ( UINT indCount = 0 ; indCount < Indent ; indCount++ )
         printf( "  " );
      printf( "No files found.\n" );
      return;
   }

   for ( UINT i = 0 ; i < foundCount ; i++ )
   {
      for ( UINT indCount = 0 ; indCount < Indent ; indCount++ )
         printf( "  " );
      wprintf( findDataArray[i].cFileName );
      printf( "\n" );

      if ( findDataArray[i].dwFileAttributes & 
FILE_ATTRIBUTE_DIRECTORY )
      {
         WCHAR newPath[MAX_PATH];

         wcscpy( newPath, Path );
         wcscat( newPath, findDataArray[i].cFileName );
         wcscat( newPath, L"\\" );
         PrintDirectory( newPath, Indent + 1 );
      }
   }

   CeRapiFreeBuffer( findDataArray )
}

void main()
{
   HRESULT hr = CeRapiInit();
   if ( FAILED(hr) ) 
   {
      printf( "*** CeRapiInit() failed. ***\n" );
      return;
   }

   PrintDirectory( L"\\", 0 );

   CeRapiUninit();

}