Figures 18, 19 and 20 for An Introduction to Extending the Rexx Language

The Rexx API - An Introduction to Extending the Rexx Language


Figures 18, 19 and 20
/* =================================================================== * * Module: myfuncs.c * Type: C * Description: this is a sample external function definition for * Personal Rexx for Microsoft Windows NT. * * Usage: the following describes the sample functions within * this package and how they are used by a calling EXEC: * * ++++ f2c( n ): convert fahrenheit to celsius. * * RxFuncAdd( 'f2c', 'myfuncs', 'f2c' ) * c_temp = f2c( f_temp ) * * ++++ c2f( n ): convert celsius to fahrenheit. * * RxFuncAdd( 'c2f', 'myfuncs', 'c2f' ) * f_temp = c2f( c_temp ) * * ++++ Load(): this function registers all of the functions * in the myfuncs package with the interpreter * * RxFuncAdd( 'Load', 'myfuncs', 'Load' ) * ret_code = Load() * * example: * rc = Load() * * ++++ UnLoad(): this function de-registers all of the functions * in the myfuncs package with the interpreter * * RxFuncAdd( 'UnLoad', 'myfuncs', 'UnLoad' ) * ret_code = UnLoad() * * example: * rc = UnLoad() * * ++++ RxUserName(): return the user name * * RxFuncAdd( 'RxUserName', 'myfuncs', 'RxUserName' ) * user_name = RxUserName() * * example: * user_name = GetUserName() * */ #include #include #define INCL_RXFUNC #include #define RX_OK 0 #define RX_ERROR 1 typedef struct _tagEXTFUNCTIONS { char *szFName; char *szDll; char *szPName; } EXTFUNCTIONS; EXTFUNCTIONS func[] = { { "f2c", "myfuncs", "c2f" }, { "c2f", "myfuncs", "c2f" }, { "Load", "myfuncs", "Load" }, { "UnLoad", "myfuncs", "UnLoad" }, { "RxUserName", "myfuncs", "RxUserName" }, }; #define MAX_FUNCS sizeof( func ) / sizeof( EXTFUNCTIONS ) /* * function prototypes, f2c and the others use the provided * typedef, while c2f does not, for comparison. */ RexxFunctionHandler f2c; DWORD c2f( LPCSTR, DWORD, PRXSTRING, LPCSTR, PRXSTRING ); RexxFunctionHandler Load; RexxFunctionHandler UnLoad; RexxFunctionHandler RxUserName; /* -------------------------------------------------------------------- * f2c * * This function converts the value passed to degrees celsius. * * Usage: * RxFuncAdd( 'f2c', 'myfuncs', 'f2c' ) * c_temp = f2c( f_temp ) */ DWORD f2c( LPCSTR szFunctionName, DWORD ArgCount, PRXSTRING Arg, LPCSTR szQueueName, PRXSTRING lprxsReturn ) { float n; if( (int)ArgCount == 1 ) sscanf( Arg->strptr, "%f", &n ); else return RX_ERROR; n = 5*(n-32)/9; lprxsReturn->strlength = sprintf( lprxsReturn->strptr, "%f", n ); return RX_OK; } /* -------------------------------------------------------------------- * c2f * * This function converts the value passed to degrees fahrenheit * * Usage: * RxFuncAdd( 'c2f', 'myfuncs', 'c2f' ) * f_temp = c2f( c_temp ) */ DWORD c2f( LPCSTR szFunctionName, /* function name */ DWORD Argc, /* argc */ PRXSTRING Argv, /* argv */ LPCSTR szQueueName, /* ext. data queue */ PRXSTRING lprxsReturn ) /* return string */ { float n; /* var for the user's arg */ if( (int)Argc == 1 ) sscanf( Argv->strptr, "%f", &n ); else return RX_ERROR; n = 9*(n+32)/5; lprxsReturn->strlength = sprintf( lprxsReturn->strptr, "%f", n ); return RX_OK; } /* -------------------------------------------------------------------- * Load * * This function registers all of the external functions defined in * the myfuncs dll. * * Usage: * RxFuncAdd( 'Load', 'myfuncs', 'Load' ) * ret_code = Load() * */ DWORD Load( LPCSTR szFunctionName, DWORD Argc, PRXSTRING Argv, LPCSTR szQueueName, PRXSTRING lprxsReturn ) { DWORD dwRC; int i; for( i=0; istrlength = wsprintf( lprxsReturn->strptr, (LPSTR)"ERROR" ); return RXFUNC_OK; break; } } } lprxsReturn->strlength = wsprintf( lprxsReturn->strptr, (LPSTR)"OK" ); return RXFUNC_OK; } /* -------------------------------------------------------------------- * UnLoad * * This function de-registers all of the external functions defined in * the myfuncs dll. * * Usage: * RxFuncAdd( 'UnLoad', 'myfuncs', 'UnLoad' ) * ret_code = UnLoad() * */ DWORD UnLoad( LPCSTR szFunctionName, DWORD ArgCount, PRXSTRING Arg, LPCSTR szQueueName, PRXSTRING lprxsReturn ) { DWORD dwRC; int i; for( i=0; istrlength = wsprintf( lprxsReturn->strptr, (LPSTR)"ERROR" ); return RXFUNC_OK; break; } } } lprxsReturn->strlength = wsprintf( lprxsReturn->strptr, (LPSTR)"OK" ); return RXFUNC_OK; } /* -------------------------------------------------------------------- * RxUserName * * This function returns the user name associated with the * currently logged in user that is running the EXEC. * * Usage: * RxFuncAdd( 'RxUserName', 'myfuncs', 'RxUserName' ) * user_name = RxUserName() */ DWORD RxUserName( LPCSTR szFunctionName, DWORD ArgCount, PRXSTRING Arg, LPCSTR szQueueName, PRXSTRING lprxsReturn ) { LPTSTR szBuffer; DWORD dwBuffer = 1024; HGLOBAL hRC = GlobalAlloc( GHND, 1024 ); if ( !hRC ) return RXFUNC_NOMEM; szBuffer = GlobalLock( hRC ); GetUserName( (LPTSTR)szBuffer, &dwBuffer ); MAKERXSTRING( *lprxsReturn, szBuffer, strlen( szBuffer )); return RXFUNC_OK; } /* * eof myfuncs.c */

Figure 19.

/* =================================================================== * * Module: myfuncs.def * Type: definition file * Description: this is the definition file for the myfuncs function * package. * * * */ LIBRARY myfuncs EXPORTS f2c @1 c2f @2 Load @3 UnLoad @4 RxUserName @5

Figure 20.

# =================================================================== # # Module: makefile # Type: make # Description: this is the makefile for the myfuncs function # package. # !include libs = $(guilibsmt) advapi32.lib wrexx32.lib all: myfuncs .c.obj: $(cc) $(cdebug) $(cflags) $(cvars) $< myfuncs.lib: myfuncs.obj myfuncs.def $(implib) \ -machine:$(CPU) \ -def:myfuncs.def \ -out:myfuncs.lib \ myfuncs.obj myfuncs: myfuncs.obj myfuncs.lib $(link) $(linkdebug) \ -base:0x1C000000 \ -subsystem:windows \ -dll \ -out:myfuncs.dll \ -entry:_DllMainCRTStartup$(DLLENTRY) \ myfuncs.obj myfuncs.exp \ $(libs)

Return to Contents


Bill Potvin

bill.potvin@network.com