/*@P************************************************************************
 * File           : TSTSB.C
 * Project        : EVERLOCK Windows SIB Test Application
 * Author         : M. Brendan Van Horn
 * Compiler       : Borland C++ v4.02
 *
 * Copyright (c) 1994-95 Az-Tech Software, Inc.
 * ALL RIGHTS RESERVED
 ************************************************************************P@*/

#define STRICT
#include <windows.h>
#include <windowsx.h>
#include <toolhelp.h>
#include <stdio.h>
#include <string.h>
#include <memory.h>
#include "sb.h"

// disable Microsoft warning for unknown pragmas
#pragma warning (disable: 4068)

/***************************************************************************
 * SIB created on the global heap for finding by a user application.
 ***************************************************************************/
typedef struct tagWINSB
{
   HINSTANCE hi;
   SB sb;
} WINSB;

typedef WINSB *PWINSB;
typedef WINSB __far *LPWINSB;
typedef WINSB __near *NPWINSB;

/***************************************************************************
 * Function Prototypes.
 ***************************************************************************/
LPWINSB FindSB( HINSTANCE hInst );

/***************************************************************************
 * Global Variables.
 ***************************************************************************/
char szAppName[] = "EVERLOCK SB Test Application";
UINT errFlags = MB_ICONSTOP | MB_OK;
UINT okFlags = MB_ICONINFORMATION | MB_OK;
LPWINSIB lpWS;
char szText[255];

#pragma argsused
int PASCAL WinMain( HINSTANCE hInstance, HINSTANCE hPrevInst, LPSTR lpCmdLine,
                    int nCmdShow )
{
   HCURSOR hSaveCursor;

   /* turn on the waiting cursor */
   hSaveCursor = SetCursor( LoadCursor( NULL, IDC_WAIT ) );

   /* hunt for the SB */
   lpWS = FindSB( hInstance );

   /* restore the original cursor */
   SetCursor( hSaveCursor );

   /* display the results of the check */
   if ( NULL == lpWS )
   {
      /* display the error message */
      MessageBox( NULL, "Can't locate the SB!", szAppName, errFlags );

      /* return control to the OS */
      return 0;
   }

   /* display the success message */
   MessageBox( NULL, (LPCSTR) lpWS->sb.startup, szAppName, okFlags );

   /* unlock the SB since we are done with it */
   GlobalUnlockPtr( lpWS );

   /* return to the OS */
   return 0;
}

LPWINSB FindSB( HINSTANCE hInst )
{
   LPWINSB lpSB;
   GLOBALENTRY ge;

   /* initialize the global entry structure with the proper values */
   memset( &ge, 0, sizeof( GLOBALENTRY ) );
   ge.dwSize = sizeof( GLOBALENTRY );

   /* start the walk through the global heap */
   if ( !GlobalFirst( &ge, GLOBAL_ALL ) )
   {
      MessageBox( NULL, "GlobalFirst() Fails!", szAppName, errFlags );
      return NULL;
   }

   /* loop to process each entry of the global heap */
   do
   {
      /* first make sure this entry is even large enough for the SB */
      if ( sizeof( WINSB ) > ge.dwBlockSize )
         continue;

      /* make sure that this is an private (i.e. allocated) block */
      if ( GT_UNKNOWN != ge.wType )
         continue;

      /* make sure there is an actual memory block associated with this entry */
      if ( !ge.hBlock )
         continue;

      /* create a far pointer for accessing this entry */
      lpSB = (LPWINSB) GlobalLock( ge.hBlock );

      /* if this is our SB block then return the pointer for it */
      if ( hInst == lpSB->hi &&
               *(DWORD __far *) (lpSB->sb.id) == *(DWORD *) "AZSB" )
         return lpSB;

      /* unlock the entries memory block */
      GlobalUnlock( ge.hBlock );

   } while ( GlobalNext( &ge, GLOBAL_ALL ) );

   /* if we fell through to here we did not find the SB */
   return NULL;
}
