KCBP同步调用开发范例

//C风格的KCBPCLI编程例子
//

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//#include "kcxpapi.h"
#include "KCBPCli.h"

#define KCBP_OPTION_AUTHENTICATION	14

int main(int argc, char* argv[])
{
	int nRet, nRow;
	char szCode[13], szMsg[256];
	int  nCol = 0, nVal = 0;
	char szColNames[256], szDat[1024];
	char  szServerName[32]={"KCBP01"};
	KCBPCLIHANDLE hHandle = NULL;
	tagKCBPConnectOption stKCBPConnection;

	nRet = KCBPCLI_Init(&hHandle);
	if(nRet != 0)
	{
		printf("error: KCBPCLI_Init return %d", nRet);
		return 1;
	}

	memset(&stKCBPConnection, 0, sizeof(stKCBPConnection));

	strcpy(stKCBPConnection.szServerName, szServerName);
	stKCBPConnection.nProtocal = 0;
	strcpy(stKCBPConnection.szAddress, "127.0.0.1");
	stKCBPConnection.nPort = 21000;
	strcpy(stKCBPConnection.szSendQName, "req-1");
	strcpy(stKCBPConnection.szReceiveQName, "ans-1");

	nRet = KCBPCLI_SetOptions(hHandle, KCBP_OPTION_CONNECT ,&stKCBPConnection, sizeof(stKCBPConnection));
	if (nRet != 0)
	{
		printf("KCBPCLI_SetOptions return %d\n", nRet);
		goto L_Exit;
	}

	//不登录
	KCBPCLI_SetOptions(hHandle, KCBP_OPTION_AUTHENTICATION ,&nVal, sizeof(nVal));
	

	printf("KCBPCLI_ConnectServer.\n");
	nRet = KCBPCLI_ConnectServer(hHandle, szServerName, "KCXP00", "888888");
	if(nRet != 0)
	{
		szMsg[0] = 0;
		KCBPCLI_GetErrorMsg(hHandle, szMsg);
		printf("KCBPCLI_ConnectServer return %d, msg=%s\n", nRet, szMsg);
		goto L_Exit;
	}
	printf("KCBPCLI_ConnectServer OK.\n");

	/*设置发送缓冲区*/
	KCBPCLI_BeginWrite(hHandle);
	
	/*设置固定参数*/
	KCBPCLI_SetValue(hHandle, "zjzh", "110100022");
	KCBPCLI_SetValue(hHandle, "jys", "1");
	KCBPCLI_SetValue(hHandle, "bankcode", "0050");
	
	/*调用服务端程序*/
	nRet = KCBPCLI_CallProgramAndCommit(hHandle, "11001001");
	if(nRet != 0)
	{
		printf("KCBPCLI_CallProgramAndCommit return %d\n", nRet);
		//有错,取错误信息
		KCBPCLI_GetErrorMsg( hHandle, szMsg );
		printf("KCBPCLI_CallProgramAndCommit ErrMsg:%s\n", szMsg );
		goto L_Exit;
	}
	
	/*打开返回的结果集*/
	nRet = KCBPCLI_RsOpen(hHandle);
	if(nRet != 0)
	{
		printf("error: KCBPCLI_RsOpen return %d\n", nRet);
		goto L_Exit;
	}

	/*从结果集中读一行*/
	nRet = KCBPCLI_RsFetchRow(hHandle);
	if(nRet != 0)
	{
		printf("error: KCBPCLI_RsFetchRow return %d\n", nRet);
		goto L_Exit;
	}

	//通用取应答首结果集
	szColNames[0] = 0;
	KCBPCLI_RsGetColNames(hHandle, szColNames, sizeof(szColNames) - 1);
	if(strstr(szColNames, "MSG_CODE") != NULL)
	{	//UNIX风格
		//KCBPCLI_RsGetColByName( hHandle, "MSG_LEVEL", szLevel );
		KCBPCLI_RsGetColByName( hHandle, "MSG_TEXT", szMsg );
		nRet = KCBPCLI_RsGetColByName( hHandle, "MSG_CODE", szCode);
	}
	else
	{	//Win风格
		//KCBPCLI_RsGetColByName( hHandle, "LEVEL", szLevel );
		KCBPCLI_RsGetColByName( hHandle, "MSG", szMsg );
		nRet = KCBPCLI_RsGetColByName( hHandle, "CODE", szCode);
	}
	
	if (nRet != 0)
	{
		printf( "get code fail, ret=%d\n ", nRet);
		goto L_Exit;
	}
	if (atoi(szCode) != 0)
	{
		printf( "LBM Exec error code:%s, msg:%s\n ", szCode, szMsg);
		goto L_Exit;
	}
	printf( "LBM Exec succ.\n ");

	/*查询是否存在后续结果集*/
	nRet = KCBPCLI_RsMore(hHandle);
	if(nRet != 0)
	{
		printf("error: KCBPCLI_RsMore return %d\n", nRet);
		goto L_Exit;
	}

	nRow = 0;
	while(1)
	{
		nRet = KCBPCLI_RsFetchRow(hHandle);
		if(nRet != 0)
		{
			break;
		}
		printf("row %d\n", nRow++);
		nCol = 0;
		KCBPCLI_RsGetColNum(hHandle, &nCol);
		for( int i = 1; i <= nCol; i++ )
		{
			KCBPCLI_RsGetColName( hHandle, i, szDat, sizeof(szDat) );
			printf( "%s", szDat );
			KCBPCLI_RsGetColByName( hHandle, szDat, szDat );
			printf( " = %s\n", szDat );
		}
		printf( "\n" );
	}

	KCBPCLI_RsClose(hHandle);

L_Exit:
	KCBPCLI_DisConnect(hHandle);
	KCBPCLI_Exit(hHandle);
	return 0;
}