3 アプリケーションプログラミング
3.12 サンプルコーディング
サンプルコーディングとして次のようなプログラムを用意しました。仕様とプログラムリストは次 の通りですので、アプリケーション作成時の参考として活用してください。
【仕様】
● アイコン状態で動作する。
● 通信ライブラリを使用する。
● 起動側連絡モードの動作のみを行う。
● 動作シーケンスは「3.10 シーケンス」のパターン1と同じ。
● 各電文に設定する項目のデータや送信ファイル等はグローバル変数で定義する。
【プログラムリスト】
/****************************************************************************
サンプルアプリケーションプログラム
Copyright(C) LOGIC Corp. 1998. All rights reserved.
****************************************************************************/
//============================== インクルードファイル定義 ==============================
#include <windows.h>
#include <stdio.h>
#include "FBIF.h"
//============================== 記号定数・構造体 ==============================
#define MAJOR_VERSION 1 // メジャーバージョン
#define MINOR_VERSION 00 // マイナーバージョン
// 電文区分の定義(通信制御電文)
#define msgOPENREQ 0x00 // 開局要求
#define msgOPENANS 0x01 // 開局回答
#define msgCLOSEREQ 0x02 // 閉局要求
#define msgCLOSEANS 0x03 // 閉局回答
#define msgMODEREQ 0x04 // モード変更要求
#define msgMODEANS 0x05 // モード変更回答
// 電文区分の定義(ファイル制御電文)
#define msgSTARTREQ 0x10 // 開始要求
#define msgSTARTANS 0x11 // 開始回答
#define msgENDREQ 0x12 // 終了要求
#define msgENDANS 0x13 // 終了回答
#define msgRETRYREQ 0x14 // 再送要求
//============================== グローバル変数 ==============================
HINSTANCE ghInst ; // current instance
char gszAppName[] = "Sample" ; // The name of this application
char gszTitle[80] ; // The title bar text (Making strings is in InitInstance) BYTE g_byComName[] = "SAMPLE" ; // 通信アプリケーション識別文字
76
BYTE g_byIPAddr[] = "172.20.20.36" ; // 相手端末 IP アドレス
BOOL g_bDialUp = FALSE ; // ダイアルアップネットワーク接続指定
BYTE g_byDialEntry[] = "" ; // ダイアルアップネットワークエントリ名
BYTE g_byDialNumber[] = "" ; // 相手の電話番号
BYTE g_byDialUser[] = "" ; // 相手の認証を受ける時のユーザ名
BYTE g_byDialPass[] = "" ; // 相手の認証を受ける時のパスワード
BYTE g_byPeerCode[] = "99999999000001" ; // 相手センター確認コード BYTE g_byMyCode[] = "11111111000001" ; // 当方センター確認コード BYTE g_byPassword[] = "ABCD" ; // パスワード
BYTE g_byAppID = '0' ; // アプリケーション ID
BYTE g_byMode = '0' ; // モード
BYTE g_byFileName[] = "200000000001" ; // ファイル名
BYTE g_byFileAccKey[] = "200001" ; // ファイルアクセスキー
BYTE g_byRecordID = '0' ; // レコード ID
WORD g_wRecordLen = 2043 ; // レコード長
BYTE g_byCompressID = '0' ; // データ圧縮 ID
BYTE g_byPathName[] = "C:\\temp\\t4212.txt" ; // 送受信ファイル名
//============================== 関数プロトタイプ宣言 ==============================
BOOL InitApplication( HANDLE ) ; BOOL InitInstance( HANDLE, int ) ;
LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM ) ; VOID CallingProc( HWND hWnd, LPARAM lParam ) ; VOID StrToBCD( BYTE *pStr, BYTE *pBCD ) ;
//============================== 関数 ==============================
/****************************************************************************
FUNCTION: WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
****************************************************************************/
int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow ) {
MSG msg;
HANDLE hAccelTable;
if( !hPrevInstance ) {
if( !InitApplication( hInstance ) ) {
return( FALSE ) ; }
}
if( !InitInstance( hInstance, nCmdShow ) ) {
return( FALSE ) ; }
hAccelTable = LoadAccelerators( hInstance, gszAppName ) ; while( GetMessage( &msg, NULL, 0, 0 ) )
{
if( !TranslateAccelerator( msg.hwnd, hAccelTable, &msg ) ) {
TranslateMessage( &msg ) ; DispatchMessage( &msg ) ; }
}
return( msg.wParam ) ; lpCmdLine ;
}
/****************************************************************************
77 FUNCTION: InitApplication(HINSTANCE)
PURPOSE: Initializes window data and registers window class
****************************************************************************/
BOOL InitApplication( HINSTANCE hInstance ) {
WNDCLASS wc;
wc.style = CS_HREDRAW | CS_VREDRAW;// Class style(s).
wc.lpfnWndProc = (WNDPROC)WndProc; // Window Procedure
wc.cbClsExtra = 0; // No per-class extra data.
wc.cbWndExtra = 0; // No per-window extra data.
wc.hInstance = hInstance; // Owner of this class
wc.hIcon = LoadIcon (hInstance, gszAppName); // Icon name from .RC wc.hCursor = LoadCursor(NULL, IDC_ARROW);// Cursor
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);// Default color wc.lpszMenuName = gszAppName; // Menu name from .RC wc.lpszClassName = gszAppName; // Name to register as return( RegisterClass( &wc ) ) ;
}
/****************************************************************************
FUNCTION: InitInstance(HINSTANCE, int)
PURPOSE: Saves instance handle and creates main window
****************************************************************************/
BOOL InitInstance( HINSTANCE hInstance, int nCmdShow ) {
HWND hWnd;
ghInst = hInstance;
wsprintf(gszTitle, "サンプル APP V%d.%02d", MAJOR_VERSION, MINOR_VERSION);
hWnd = CreateWindow(
gszAppName, // See RegisterClass() call.
gszTitle, // Text for window title bar.
WS_OVERLAPPEDWINDOW,// Window style.
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, // Use default positioning NULL, // Overlapped windows have no parent.
NULL, // Use the window class menu.
hInstance, // This instance owns this window.
NULL // We don't use any data in our WM_CREATE );
if (!hWnd) {
return (FALSE);
}
ShowWindow(hWnd, SW_SHOWMINNOACTIVE); // Show the window as ICON return (TRUE);
}
/****************************************************************************
FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM) PURPOSE: Processes messages
****************************************************************************/
LRESULT CALLBACK WndProc( HWND hWnd, UINT message, WPARAM uParam, LPARAM lParam ) {
char szMsg[100] ;
BOOL bRes ;
switch( message ) {
case WM_CREATE :
bRes = FBOpenService( hWnd, g_byComName ) ; if( bRes == FALSE )
78 {
sprintf( szMsg, "起動できません" ) ;
MessageBox( hWnd, szMsg, "FBtest", MB_OK ) ; return( 0 ) ;
}
FBOpenLine( g_byIPAddr, g_bDialUp, g_byDialEntry, "", "", "" ) ; return( 0 ) ;
case WM_DESTROY : case WM_CLOSE :
PostQuitMessage( 0 ) ; ExitProcess( 0 ) ; return( 0 ) ;
case WM_COPYDATA :
CallingProc( hWnd, lParam ) ; return( 0 ) ;
}
return( DefWindowProc( hWnd, message, uParam, lParam ) ) ; }
/****************************************************************************
FUNCTION: CallingProc( HWND hWnd, LPARAM, lParam )
PURPOSE: 通信ライブラリ(FBIF.dll)を使った起動側の処理を行う
****************************************************************************/
VOID CallingProc( HWND hWnd, LPARAM lParam ) {
DWORD dwMsgNo, dwResult ; SessionInfo Sess ;
FileInfo File ;
FBGetMsgNo( lParam, &dwMsgNo, &dwResult ) ;
// イベントメッセージで分岐する switch( dwMsgNo )
{
case EVENT_MsgOpenedLine : //=== MsgOpenedLine メッセージ受信 ===
// 開局要求電文を送信する処理 memset( &Sess, 0, sizeof(Sess) ) ;
Sess.byMsgID = msgOPENREQ ;
StrToBCD( g_byPeerCode, Sess.byPeerCode ) ; StrToBCD( g_byMyCode, Sess.byMyCode ) ;
memset( Sess.byPassword, ' ', sizeof(Sess.byPassword) ) ; memcpy( Sess.byPassword, g_byPassword, strlen(g_byPassword) ) ; Sess.byAppID = '0' ;
Sess.byMode = g_byMode ; ReplyMessage( 0 ) ; FBOpenSession( &Sess ) ; break ;
case EVENT_MsgOpenedSession : //=== MsgOpenedSession メッセージ受信 ===
// 開始要求電文を送信する処理 memset( &File, 0, sizeof(File) ) ;
File.byMsgID = msgSTARTREQ ;
memset( File.byFileName, ' ', sizeof(File.byFileName) ) ; memcpy( File.byFileName, g_byFileName, strlen(g_byFileName) ) ; memset( File.byFileAccKey, ' ', sizeof(File.byFileAccKey) ) ;
memcpy( File.byFileAccKey, g_byFileAccKey, strlen(g_byFileAccKey) ) ; File.byRecordID = '0' ;
File.wRecordLen = g_wRecordLen ; File.byCompressID = g_byCompressID ; ReplyMessage( 0 ) ;
79 FBStartFile( &File, g_byPathName ) ; break ;
case EVENT_MsgTermedFile : //=== MsgTermedFile メッセージ受信 ===
// 閉局要求電文を送信する処理 memset( &Sess, 0, sizeof(Sess) ) ;
Sess.byMsgID = msgCLOSEREQ ;
StrToBCD( g_byPeerCode, Sess.byPeerCode ) ; StrToBCD( g_byMyCode, Sess.byMyCode ) ;
memset( Sess.byPassword, ' ', sizeof(Sess.byPassword) ) ; memcpy( Sess.byPassword, g_byPassword, strlen(g_byPassword) ) ; Sess.byAppID = '0' ;
Sess.byMode = g_byMode ; ReplyMessage( 0 ) ; FBCloseSession( &Sess ) ; break ;
case EVENT_MsgClosedSession : //=== MsgClosedSession メッセージ受信 ===
// 回線を切断する処理 ReplyMessage( 0 ) ;
FBCloseLine() ; break ;
case EVENT_MsgClosedLine : // MsgClosedLine メッセージ受信 ReplyMessage( 0 ) ;
FBCloseService() ;
PostMessage( hWnd, WM_DESTROY, 0, 0 ) ; break ;
default :
ReplyMessage( 0 ) ; break ;
} }
/****************************************************************************
FUNCTION: StrToBCD( BYTE *pStr, BYTE *pBCD ) PURPOSE: 文字列を BCD に変換する
****************************************************************************/
VOID StrToBCD( BYTE *pStr, BYTE *pBCD ) {
char ch ; DWORD dwCnt ;
for( dwCnt = 0 ; dwCnt < strlen( pStr ) ; dwCnt++ ) {
if( dwCnt % 2 ) {
ch = *(pStr+dwCnt) & 0x0F ;
*(pBCD+dwCnt/2) |= ch ; }
else {
ch = *(pStr+dwCnt) & 0x0F ; ch <<= 4 ;
*(pBCD+dwCnt/2) = ch ; }
} }
80