AGGTEST.CPP


/////////////////////////////////////////////////////////////////////////////
//
// Includes
//

#include <stdio.h>
#include <windows.h>
#include <tchar.h>
#include <crtdbg.h>
#include <initguid.h>

#include "..\scardagg.h"
#include "scardsrv.h"
#include "scarddat.h"
#include "scardmgr.h"
#define IID_DEFINED
#include "..\scardagg_i.c"
#include "scardsrv_i.c"
#include "scarddat_i.c"
#include "scardmgr_i.c"

/////////////////////////////////////////////////////////////////////////////
//
// Globals
//
LPSCARD g_pISCard = NULL;
LPSCARDDATABASE g_pISCardDatabase = NULL;
LPSCARDLOCATE g_pISCardLocate = NULL;
LPSCARDNEWINTERFACE g_pISCardNewInterface = NULL;
LPSCARDISO7816 g_pISCardISO7816 = NULL;
LPSCARDTYPECONV g_pISCardTypeConv = NULL;
LPSCARDCMD g_pISCardCmd = NULL;

/////////////////////////////////////////////////////////////////////////////
//
// Macros
//

/////////////////////////////////////////////////////////////////////////////
//
// Macros
//

#ifndef SafeCast
#define SafeCast(cast,pt)((cast)(pt))
#endif

#ifndef ASSERT
#define ASSERT_ASSERTE
#endif

#ifndef IN
#define IN
#endif

/////////////////////////////////////////////////////////////////////////////
//
// Functions
//

void _cdecl
ConTrace(
IN LPCTSTR lpszFormat, ...)
{
//
// Helper to do print traces...
//

va_list args;
va_start(args, lpszFormat);

int nBuf;
TCHAR szBuffer[512];
ZeroMemory(szBuffer, SafeCast(DWORD,(512*sizeof(TCHAR))));

nBuf = _vstprintf(szBuffer, lpszFormat, args);
ASSERT(nBuf < sizeof(szBuffer));

_tprintf(szBuffer);
OutputDebugString(szBuffer);
va_end(args);
}


void _cdecl
CallServer(
void)
{
HRESULThresult = S_OK;

ConTrace(_T("\nSTARTING Object Creation\n=========================\n"));
ConTrace(_T("Calling CoCreateInstance()...SCardDat\n"));


hresult = CoCreateInstance(CLSID_CSCard,
NULL,
CLSCTX_ALL,
IID_ISCard,
(LPVOID*) &g_pISCard);
if (FAILED(hresult))
{
ConTrace(_T("Failed to create CSCard object\n"));
return;
}
ConTrace(_T("Object created\n"));

hresult = CoCreateInstance(CLSID_CSCardTypeConv,
NULL,
CLSCTX_ALL,
IID_ISCardTypeConv,
(LPVOID*) &g_pISCardTypeConv);
if (FAILED(hresult))
{
ConTrace(_T("Failed to create CSCardTypeConv object\n"));
return;
}
ConTrace(_T("Object created\n"));


//***************************************************************
// Create the New interface and the aggregated interface
hresult = CoCreateInstance(CLSID_CSCardNewInterface,
NULL,
CLSCTX_ALL,
IID_ISCardNewInterface,
(LPVOID*) &g_pISCardNewInterface);
if (FAILED(hresult))
{
ConTrace(_T("Failed to create CSCardNewInterface object\n"));
return;
}
ConTrace(_T("Object created\n"));

hresult = CoCreateInstance(CLSID_CSCardNewInterface,
NULL,
CLSCTX_ALL,
IID_ISCardISO7816,
(LPVOID*) &g_pISCardISO7816);
if (FAILED(hresult))
{
ConTrace(_T("Failed to create CSCardISO7816 Aggregated object\n"));
return;
}
ConTrace(_T("Object created\n"));
//
//******************************************************************

hresult = CoCreateInstance(CLSID_CSCardCmd,
NULL,
CLSCTX_ALL,
IID_ISCardCmd,
(LPVOID*) &g_pISCardCmd);
if (FAILED(hresult))
{
ConTrace(_T("Failed to create CSCardCmd object\n"));
return;
}
ConTrace(_T("Object created\n"));

ConTrace(_T("\nDONE!!! with object Creation\n=========================\n"));
}


void _cdecl
CallAggreg()
{
// Locals
HRESULThresult = S_OK;
LPBYTEBUFFERpBuffer1 = NULL,
pBuffer2 = NULL;

// Create a couple of byte buffers for testing
hresult = g_pISCardTypeConv->CreateByteBuffer((ULONG) 1,
&pBuffer1);
if (FAILED(hresult)) {
ConTrace(_T("Failed to create IByteBuffer\n"));
return;
};// if
hresult = g_pISCardTypeConv->CreateByteBuffer((ULONG) 1,
&pBuffer2);
if (FAILED(hresult)) {
ConTrace(_T("Failed to create IByteBuffer\n"));
return;
};// if

// Call one of the aggregated methods
ConTrace(_T("Build/Execute: AppendRecord\n"));
hresult = g_pISCardISO7816->AppendRecord(0,
pBuffer1,
&g_pISCardCmd);
if (FAILED(hresult)){
ConTrace(_T("Failed on AppendRecord\n"));
return;
}

// Call the new interface method
ConTrace(_T("Build/Execute: NewInterface\n"));
hresult = g_pISCardNewInterface->NewInterface(0,
0,
pBuffer1,
&g_pISCardCmd);
if (FAILED(hresult)){
ConTrace(_T("Failed on NewInterface\n"));
return;
}

}


int
main(
IN int argc,
IN char *argv[ ])
{
int nRet = -1;

try
{
if (FAILED(CoInitialize(NULL)))
throw;

CallServer();
CallAggreg();

#ifdef _DEBUG
_CrtDumpMemoryLeaks();
#endif

CoUninitialize();

nRet = 0;
}

catch (...)
{
ConTrace(_T("Unhandled exception!!!"));
}

return (nRet);
}

/////////////////////////////////////////////////////////////////////////////