ユーザ出口プログラムの作成例
ユーザ出口プログラムの作成例を以下に示します。この作成例では、1レコード16バイトのイメージデータである入力ファイルをASCII文字列に変換し出力データとしています。
(1) Windowsの場合
a) userexit.cの内容
#include <windows.h> #include <stdio.h> #include <stdlib.h> #include <string.h> /* USER_EXITINFO 構造体 */ typedef struct user_exitinfo { int whereno; /* 抽出条件番号 */ int outfileno; /* 出力ファイル番号 */ char outitemname[255+1]; /* 出力項目名 */ int outitemno; /* 出力項目番号 */ char outitemtype; /* 定義項目タイプ */ int outitemsize; /* 定義項目サイズ */ char outitemflg; /* 定義小数部桁数 */ }USER_EXITINFO; /* 項目番号に該当するデータを取得する関数 */ typedef int (__stdcall *PROC_ed_getindata_itemno) (void *ptr1, void *ptr2, int itemno, char **getbuf, int *getsize); /* メモリを取得する関数 */ typedef void *(__stdcall *PROC_ed_malloc) (size_t size); /* メモリを解放する関数 */ typedef void (__stdcall *PROC_ed_free)(void *ptr); /* DLL エントリーポイント */ 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; } /* 初期化関数 */ int __stdcall ed_userexit_init( short *dtlerrno ) { return 0; } /* メイン関数 */ int __stdcall ed_userexit_main( USER_EXITINFO *user_exitinfo, char **outbuf, int *outsize, short *dtlerrno, void *ptr1, void *ptr2 ) { /* データ取得用 */ char *getbuf; /* データサイズ格納用 */ int getsize = 0; /* モジュールハンドル格納用 */ HMODULE hModule; /* 項目番号からデータを取得する関数 */ PROC_ed_getindata_itemno ed_getindata_itemno; /* メモリ取得関数 */ PROC_ed_malloc ed_malloc; /* メモリ解放関数 */ PROC_ed_free ed_free; /* 入力データ取得関数のロード */ hModule = GetModuleHandle(NULL); ed_getindata_itemno = (PROC_ed_getindata_itemno)GetProcAddress(hModule, "ed_getindata_itemno"); if( ed_getindata_itemno == NULL ){ *dtlerrno = 1; return -1; } /* メモリ取得関数のロード */ ed_malloc = (PROC_ed_malloc)GetProcAddress(hModule, "ed_malloc"); if( ed_malloc == NULL ){ *dtlerrno = 2; return -1; } /* メモリ解放関数のロード */ ed_free = (PROC_ed_free)GetProcAddress(hModule, "ed_free"); if( ed_free == NULL ){ *dtlerrno = 3; return -1; } /* 項目番号1 のデータを取得 */ if( ed_getindata_itemno( ptr1, ptr2, 1, &getbuf, &getsize ) != 0 ){ *dtlerrno = 4; return -1; } /* 出力用バッファをアロケート */ *outbuf = (char *)ed_malloc( 64 ); if( *outbuf == (char *)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; /* 取得データ領域の解放 */ ed_free( getbuf ); return 0; } /* 終了関数 */ void __stdcall ed_userexit_end( void ) { return; }
b) userexit.defの内容
LIBRARY "userexit" EXPORTS ed_userexit_init @1 ed_userexit_main @2 ed_userexit_end @3
(2) UNIXの場合
a) userexit.cの内容
#include <stdio.h> #include <stdlib.h> /* USER_EXITINFO 構造体 */ typedef struct user_exitinfo { int whereno; /* 抽出条件番号 */ int outfileno; /* 出力ファイル番号 */ char outitemname[255+1]; /* 出力項目名 */ int outitemno; /* 出力項目番号 */ char outitemtype; /* 定義項目タイプ */ int outitemsize; /* 定義項目サイズ */ char outitemflg; /* 定義小数部桁数 */ } USER_EXITINFO; /* 項目番号に該当するデータを取得する関数 */ 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); /* 初期化関数 */ int ed_userexit_init( short *dtlerrno ) { return 0; } /* メイン関数 */ int ed_userexit_main( USER_EXITINFO *user_exitinfo, char **outbuf, int *outsize, short *dtlerrno, void *ptr1, void *ptr2 ) { char *getbuf; /* データ取得用 */ int getsize = 0; /* データサイズ格納用 */ /* 項目番号1 のデータを取得 */ if( ed_getindata_itemno( ptr1, ptr2, 1, &getbuf, &getsize ) != 0 ){ *dtlerrno = 1; return -1; } /* 出力用バッファをアロケート */ *outbuf = (char *)ed_malloc( 64 ); if( *outbuf == (char *)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; /* 取得データを解放 */ ed_free( getbuf ); return 0; } /* 終了関数 */ void ed_userexit_end( void ) { return; }