#include "stdafx.h"
#include "Sample.h"

HMODULE	hModule = NULL;		// handle for dll
INT	nSessionId = 0;			// Session ID for dll's function	
TCHAR pMsg[1024] = {0};



//	Callback status function sample
EXPORT INT CALLBACK CBFuncSampProc( DWORD dwStatus )
{
	static	INT n = 0;
	TCHAR pCbs[1024];
	wsprintf( pCbs, TEXT("[%02d] 0x%08x"), n++, dwStatus ); 
	::SendMessage( ::GetDlgItem( AfxGetApp()->m_pMainWnd->m_hWnd, IDC_CB_LIST ), 
						LB_INSERTSTRING, 0, (LPARAM)pCbs );
	return 0;
}


//	Message of RpGetPrnCapability 
VOID DispMsg( LPBYTE pRecBuf, DWORD dwReturned )
{
	HWND	hGetDataMsg = ::GetDlgItem( AfxGetApp()->m_pMainWnd->m_hWnd, IDC_GET_DATA_MSG );
	DWORD	dwIndex	= 0;	

	if( dwReturned )
	{
		while( dwReturned-- )
		{
			TCHAR	pTmpMsg[32];
			wsprintf( pTmpMsg, TEXT("0x%02x "), pRecBuf[ dwIndex++ ] ); 
			lstrcat( pMsg, pTmpMsg ); 
			if( !( dwIndex % 6 ) )
			{
				lstrcat( pMsg, TEXT("\n") ); 
			}
			else if( dwIndex > 30 )
			{
				lstrcat( pMsg, TEXT("....") );
				break;
			}
		}
	}
	else
	{
		lstrcat( pMsg, TEXT("NO DATA !!") ); 
	}
	::SetWindowText( hGetDataMsg, pMsg ); 
}


// Load dll
BOOL LoadDll()
{
	if( hModule = LoadLibrary( (LPCTSTR)DLL_FILE_NAME ) )
	{
		if(	PFN_OPEN			&&	
			PFN_CLOSE			&&
			PFN_DIRECTIO_EX		&&
			PFN_GET_CAPABILITY	&&
			PFN_SET_CALLBACK_STATUS		)
		{
			return TRUE;
		}
		FreeLibrary( hModule );
	}

	TCHAR pErr[1024] = {0};
	wsprintf( pErr, TEXT("Loading the [[ %s ]] file was failed."), DLL_FILE_NAME );
	AfxMessageBox( pErr );
	return FALSE;
}


// Release dll
VOID UnloadDll()
{
	if( nSessionId )
	{
		PFN_CLOSE( nSessionId );
		nSessionId = 0;
	}

	FreeLibrary( hModule );
}




//	Sample of RpOpenMonPrinter
BOOL OpenSamp( LPTSTR lpPrinterName )
{
	if( nSessionId )
	{
		PFN_CLOSE( nSessionId );
		nSessionId = 0;
	}

	// Open Printer
	nSessionId = PFN_OPEN( 2, lpPrinterName );
	return ( nSessionId < 0 ) ? FALSE : TRUE;  
}

//	Sample of RpDirectIOEx 
VOID WriteSamp()
{
	static BYTE	byIndex = 0;
	BYTE	pFooter[]	= FOOTER_CMD;
	BYTE	pBuf[]		= "Seiko Instruments Inc.\n";
	BYTE	prBuf[255];
	DWORD	cbrBuf		= sizeof(prBuf);
	INT		nResult;
	
	lstrcpy( pMsg, TEXT("[RpDirectIOEx Rec]\n\n") );


	pFooter[2] = byIndex++%0x10;
	nResult = PFN_DIRECTIO_EX( nSessionId, sizeof(pBuf), pBuf, NULL, NULL, 90000, TRUE, 0 );		
	nResult = PFN_DIRECTIO_EX( nSessionId, sizeof(pFooter), pFooter, &cbrBuf, prBuf, 90000, TRUE, 0 );	

	if( nResult < 0 ){
		TCHAR pErr[1024] = {0};
		wsprintf( pErr, TEXT("DirectIOEx was failed.") );
		AfxMessageBox( pErr );
	}else{
		DispMsg( prBuf, cbrBuf );
	}
}

//	Sample of RpSetStatusBackFunction
VOID CallbackSamp( BOOL bStart )
{
	if( bStart )
	{
		PFN_SET_CALLBACK_STATUS( nSessionId, (FNC_CB_STATUS)CBFuncSampProc );
	}
	else
	{
		PFN_SET_CALLBACK_CANCEL( nSessionId );
	}
}




//	Sample of RpGetPrnCapability 
VOID ReadSamp()
{
	static UINT	nIndexCmdTbl = 0;
	INT		nResult = 0;
	BYTE	pRecBuf[255] = {0};	
	BYTE	byReturned = (BYTE)sizeof(pRecBuf);
	HWND	hGetDataMsg = ::GetDlgItem( AfxGetApp()->m_pMainWnd->m_hWnd, IDC_GET_DATA_MSG );

	if( !lstrlen( TBL_GETPRNCAP[ nIndexCmdTbl ].pCmdName ) )
	{
		nIndexCmdTbl = 0;
	}

	wsprintf( pMsg, TEXT("[ %s ]\n\n"), TBL_GETPRNCAP[ nIndexCmdTbl ].pCmdName ); 
	::SetWindowText( hGetDataMsg, pMsg ); 

	nResult = PFN_GET_CAPABILITY( nSessionId, TBL_GETPRNCAP[ nIndexCmdTbl ].nIndex, &byReturned, pRecBuf );
	if( !nResult )
	{
		DispMsg( pRecBuf, byReturned );
	}
	else
	{
		wsprintf( pMsg, TEXT("Error ( %d ) : [ %s ]"), nResult, TBL_GETPRNCAP[ nIndexCmdTbl ].pCmdName ); 
		::SetWindowText( hGetDataMsg, pMsg ); 
	}

	nIndexCmdTbl++;
}
