Example of a user exit program
The following is an example of a user exit program. This program example takes an input file of image data where the record size is 16 bytes and converts the data to ASCII character strings for output.
(1) For Windows:
a) Content of userexit.c:
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* USER_EXITINFO struct */
typedef struct user_exitinfo {
int whereno; /* Extraction condition number */
int outfileno; /* Output file number */
char outitemname[255+1]; /* Output field name */
int outitemno; /* Output field number */
char outitemtype; /* Definition field type */
int outitemsize; /* Definition field size */
char outitemflg; /* Digits after decimal point of the definition */
}USER_EXITINFO;
/* Function that obtains data corresponding to the field number */
typedef int (__stdcall *PROC_ed_getindata_itemno) (void *ptr1, void *ptr2, int itemno, char **getbuf, int *getsize);
/* Function that obtains the memory */
typedef void *(__stdcall *PROC_ed_malloc) (size_t size);
/* Function that releases the memory */
typedef void (__stdcall *PROC_ed_free)(void *ptr);
/* DLL entry point */
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;
}
/* Initial processing function */
int __stdcall ed_userexit_init( short *dtlerrno )
{
return 0;
}
/* Main function */
int __stdcall ed_userexit_main( USER_EXITINFO *user_exitinfo,
char **outbuf,
int *outsize,
short *dtlerrno,
void *ptr1,
void *ptr2 )
{
/* For obtaining data */
char *getbuf;
/* For storing data size */
int getsize = 0;
/* For storing module handle */
HMODULE hModule;
/* Function that obtains data corresponding to the field number*/
PROC_ed_getindata_itemno ed_getindata_itemno;
/* Function to obtain memory */
PROC_ed_malloc ed_malloc;
/* Function to release memory */
PROC_ed_free ed_free;
/* Loading the function to obtain the input data */
hModule = GetModuleHandle(NULL);
ed_getindata_itemno =
(PROC_ed_getindata_itemno)GetProcAddress(hModule, "ed_getindata_itemno");
if( ed_getindata_itemno == NULL ){
*dtlerrno = 1;
return -1;
}
/* Loading the function to obtain memory */
ed_malloc = (PROC_ed_malloc)GetProcAddress(hModule, "ed_malloc"); {
if( ed_malloc == NULL ){
*dtlerrno = 2;
return -1;
}
/* Loading the function to release memory */
ed_free = (PROC_ed_free)GetProcAddress(hModule, "ed_free");
if( ed_free == NULL ){
*dtlerrno = 3;
return -1;
}
/* Obtaining the data corresponding to the field number 1 */
if( ed_getindata_itemno( ptr1, ptr2, 1, &getbuf, &getsize ) != 0 ){
*dtlerrno = 4;
return -1;
}
/* Allocating the output buffer*/
*outbuf = (char *)ed_malloc( 64 );
if( *outbuf == NULL ){
*dtlerrno = 5;
return -1;
}
sprintf((char *)*outbuf,
"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
(unsigned char)getbuf[0],(unsigned char)getbuf[1],
(unsigned char)getbuf[2],(unsigned char)getbuf[3],
(unsigned char)getbuf[4],(unsigned char)getbuf[5],
(unsigned char)getbuf[6],(unsigned char)getbuf[7],
(unsigned char)getbuf[8],(unsigned char)getbuf[9],
(unsigned char)getbuf[10],(unsigned char)getbuf[11],
(unsigned char)getbuf[12],(unsigned char)getbuf[13],
(unsigned char)getbuf[14],(unsigned char)getbuf[15]);
*outsize=32;
/* Releasing the obtained data */
ed_free( getbuf );
return 0;
}
/* End function */
void ed_userexit_end( void )
{
return;
}
b) Content for userexit.def
LIBRARY "userexit"
EXPORTS
ed_userexit_init @1
ed_userexit_main @2
ed_userexit_end @3
(2) For UNIX
a) Content of userexit.c
#include <stdio. h>
#include <stdlib. h>
/* USER_EXITINFO struct */
typedef struct user_exitinfo {
int whereno; /* Extraction condition number */
int outfileno; /* Output file number */
char outitemname[255+1]; /* Output field name */
int outitemno; /* Output field number */
char outitemtype; /* Definition field type */
int outitemsize; /* Definition field size */
char outitemflg; /* Digits after decimal point of the definition */
} USER_EXITINFO;
/* Function that obtains data corresponding to the field number */
int *ed_getindata_itemno ( void *ptr1, void *ptr2, int itemno,
char **getbuf, int *getsize);
void *ed_malloc (size_t size);
void ed_free (void *ptr);
/* Initial processing function */
int ed_userexit_init( short *dtlerrno )
{
return 0;
}
/* Main function */
int ed_userexit_main( USER_EXITINFO *user_exitinfo,
char **outbuf,
int *outsize,
short *dtlerrno,
void *ptr1,
void *ptr2 )
{
char *getbuf; /* For obtaining data */
int getsize = 0; /* For storing data size */
/* Obtaining the data corresponding to the field number 1 */
if( ed_getindata_itemno( ptr1, ptr2, 1, &getbuf, &getsize ) != 0 ){
*dtlerrno = 1;
return -1;
}
/* Allocating the output buffer*/
*outbuf = (char *)ed_malloc( 64 );
if( *outbuf == NULL ){
*dtlerrno = 2;
return -1;
}
sprintf((char *)*outbuf,
"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
(unsigned char)getbuf[0],(unsigned char)getbuf[1],
(unsigned char)getbuf[2],(unsigned char)getbuf[3],
(unsigned char)getbuf[4],(unsigned char)getbuf[5],
(unsigned char)getbuf[6],(unsigned char)getbuf[7],
(unsigned char)getbuf[8],(unsigned char)getbuf[9],
(unsigned char)getbuf[10],(unsigned char)getbuf[11],
(unsigned char)getbuf[12],(unsigned char)getbuf[13],
(unsigned char)getbuf[14],(unsigned char)getbuf[15]);
*outsize=32;
/* Releasing the obtained data */
ed_free( getbuf );
return 0;
}
/* End function */
void ed_userexit_end( void )
{
return;
}