BSAPI
error_handler.cpp

This example shows how to write and use an error handler.

#include <stdio.h>
#include <stdlib.h>
#include "bsapi.h"

class ErrorHandler : public SErrorCallbackI 
{
  public:
    virtual void BSAPI_METHOD OnTextMessage(SUnknownI *pSender, message_type type, unsigned int messageId, const char *pMessage)
    {
      unsigned int iid = pSender ? pSender->GetIID() : SIID_UNDEFINED;
      char *ptext_iid = BSAPIInterfaceId2Text(iid);
    
      switch(type)
      {
        case mtError:
          fprintf(stderr, "ERROR: %s - %s\n", ptext_iid, pMessage);
          break;
        case mtWarning:
          fprintf(stderr, "WARNING: %s - %s\n", ptext_iid, pMessage);       
          break;
        case mtLog:
          fprintf(stdout, "%s\n", pMessage);
         break;
      }
    }
} gErrorHandler;

main()
{
  
  SSpeechRecI *psrec;
  psrec = static_cast<SSpeechRecI *>(BSAPICreateInstance(SIID_SPEECHREC));
  if(!psrec)
  {
    fprintf(stderr, "ERROR: Memory allocation error\n");
    exit(1);
  }  

  psrec->SetErrorHandler(&gErrorHandler);

  if(psrec->Init(...))
    exit(1);

  ...

  psrec->Release();

  return 0;
}