Sample

Previous  Next

 

This is a sample Extension Library that implements 3 functions, SAMPLE.INC, SAMPLE.ATAN and SAMPLE.MAKE_STRING.  The sample was generated with MS VS6 but should work with minor changes for any C/C++ environment.

 


SampleExtLib.h

 

/////////////////////////////////////////////////////////////////////////////

//

// The following ifdef block is the standard way of creating macros which make exporting

// from a DLL simpler. All files within this DLL are compiled with the SAMPLEEXTLIB_EXPORTS

// symbol defined on the command line. this symbol should not be defined on any project

// that uses this DLL. This way any other project whose source files include this file see

// SAMPLEEXTLIB_API functions as being imported from a DLL, wheras this DLL sees symbols

// defined with this macro as being exported.

//

/////////////////////////////////////////////////////////////////////////////

// Need to include MyBASIC2 and enable parsing macros

//

#define _MB2_PARSE_MACROS

#include "MyBASIC2.h"

 

/////////////////////////////////////////////////////////////////////////////

//

#ifdef SAMPLEEXTLIB_EXPORTS

#define SAMPLEEXTLIB_API __declspec(dllexport)

#else

#define SAMPLEEXTLIB_API __declspec(dllimport)

#endif

 

/////////////////////////////////////////////////////////////////////////////

//

extern "C" SAMPLEEXTLIB_API int mb_ext_load( mb_interpreter_t* s );

extern "C" SAMPLEEXTLIB_API int mb_ext_unload( mb_interpreter_t* s );

 


SampleExtLib.cpp

 

/////////////////////////////////////////////////////////////////////////////

//

// SampleExtLib.cpp : Defines the entry point for the DLL application.

//

/////////////////////////////////////////////////////////////////////////////

//

#include "stdafx.h"

#include "SampleExtLib.h"

 

/////////////////////////////////////////////////////////////////////////////

//

BOOL APIENTRY DllMain( HANDLE hModule,

                      DWORD  ul_reason_for_call,

                      LPVOID lpReserved

                                        )

{

   switch (ul_reason_for_call)

       {

               case DLL_PROCESS_ATTACH:

               case DLL_THREAD_ATTACH:

               case DLL_THREAD_DETACH:

               case DLL_PROCESS_DETACH:

                       break;

   }

   return TRUE;

}

 

//////////////////////////////////////////////////////

// Increment INTEGER parameter by 1

// i = SAMPLE.INC( i )

//

int _Sample_inc( mb_interpreter_t* s, void** l )

{

  BEGIN_PARSE( s, l )

     POP_INT( nPar )

  END_PARSE

 

  RETURN_INT( ++nPar );

}

 

/////////////////////////////////////////////////////////////////////////////

// real = SAMPLE.ATAN( p1 )

// real = SAMPLE.ATAN( p1, p2 )

//

int _Sample_atan( mb_interpreter_t* s, void** l )

{

  float fAtan = 0;

 

  BEGIN_PARSE( s, l )

     POP_REAL( p1 )

     OPT_REAL( p2, 0 )

  END_PARSE

 

  if ( SKIPPED_LAST )

  {

     // Use atan( x )

 

     fAtan = float( atan( p1 ) );

  }

  else

  {

     // Use atan( y, x )

 

     fAtan = float( atan2( p1, p2 ) );

  }

 

  RETURN_REAL( fAtan );

}

 

/////////////////////////////////////////////////////////////////////////////

// string = SAMPLE.MAKE_STRING( prompt, param )

//

int _Sample_make_string( mb_interpreter_t* s, void** l )

{

  char  buffer[2048];

 

  BEGIN_PARSE( s, l )

     POP_STRING( str )

     POP_REAL( fReal )

  END_PARSE

 

  int nCount = sprintf( buffer, "%s: %1.5f", str, fReal );

 

  // Need to create an output string.   This is important

  // since MyBASIC2 will dispose of the string using mb_free

 

  COPY_STRING( pOutput, buffer );

 

  // Return the string

 

  RETURN_STRING( pOutput );

}

 

/////////////////////////////////////////////////////////////////////////////

// Register our functions.  Use NameSpace SAMPLE

//

SAMPLEEXTLIB_API int mb_ext_load( mb_interpreter_t* s )

{

  mb_create_namespace( s, "SAMPLE" );

     mb_register_func( s, ".INC", _Sample_inc );

     mb_register_func( s, ".ATAN", _Sample_atan );

     mb_register_func( s, ".MAKE_STRING", _Sample_make_string );

  mb_end_namespace( s );

 

  return MB_FUNC_OK;

}

 

/////////////////////////////////////////////////////////////////////////////

// Need to remove our functions when library is unloaded

//

SAMPLEEXTLIB_API int mb_ext_unload( mb_interpreter_t* s )

{

  mb_use_namespace( s, "SAMPLE" );

     mb_remove_func( s, ".INC" );

     mb_remove_func( s, ".ATAN" );

     mb_remove_func( s, ".MAKE_STRING" );

  mb_end_namespace( s );

 

  return MB_FUNC_OK;

}

 


SampleExtLibTest.bas

 

' ---------------------------------------------------------------

'

' Small MyBASIC2 Script to test the functions in SampleExtLib.dll

'

' ---------------------------------------------------------------

 

$LIBRARY "SampleExtLib"

 

DIM i AS INTEGER

DIM at AS REAL

DIM s AS STRING

 

' Test SAMPLE.INC

i = 13

i = SAMPLE.INC( i )

PRINT "Incremented I = "; i,

 

' Next, test SAMPLE.ATAN with one parameter

at = SAMPLE.ATAN( 12 )

s = SAMPLE.MAKE_STRING( "ATAN( 12 )    ", at )

PRINT s,

 

' Last, test SAMPLE.ATAN with two parameters

at = SAMPLE.ATAN( 12, 21 )

s = SAMPLE.MAKE_STRING( "ATAN( 12, 21 )", at )

PRINT s,

 

END