#include <windows.h>
#include <tchar.h>
#include <sicstus/sicstus.h>


extern "C" int SPCDECL user_main(int argc,char *argv[])
{
  SP_pred_ref pred;
  char const * name = "cplus";
  LPCTSTR result;
  int exit_code;

  /* assume error */
  result = _T("Test Failed");
  exit_code = 1;

  if (!SP_initialize(argc,argv,NULL)) goto barf;

  /* Typically you would use SP_restore to restore a .sav file containing all your prolog code. */
  if (SP_load(name) != SP_SUCCESS) goto barf;

  pred = SP_predicate("main",0,"user"); /* look up user:main/0 */
  if (pred == NULL) goto barf;

  /* Note: in SICStus 3.9 you can use SP_read_from_string to easily
     call a goal specified as a string, see the SICStus Prolog
     Manual. This is much easier than building Prolog terms manually
     etc. */

  switch (SP_query(pred))       /* call user:main */
    {
    case SP_SUCCESS:
      result = _T("Test Succeeded");
      exit_code = 0;
      break;
    case SP_FAILURE:
      result = _T("Test Failed (Predicate failed)");
      break;
    case SP_ERROR:
      /* Could use SP_exception_term() here to see what exception happened. */
      result = _T("ERROR: Predicate got an exception");
      break;
    default:
      result = _T("ERROR: Impossible return code from SP_query(), report to sicstus-support@sics.se");
      break;
    }
  SP_deinitialize();

 barf:
    
  MessageBox(NULL, result, _T("C++ test"), MB_OK|MB_TASKMODAL);
  return exit_code;
}

