// Chap10Demo.cpp : Implementation of CChap10Demo
#include "stdafx.h"
#include "AsynchDemoDll.h"
#include "Chap10Demo.h"

/////////////////////////////////////////////////////////////////////////////
// CChap10Demo


STDMETHODIMP CChap10Demo::DoSomething(long InputParm, long *pOutputParm)
{

/*
Get our call context for checking whether or not there has been a cancel request.
*/

	ICancelMethodCallsPtr pCancel ;
	HRESULT hr = CoGetCallContext (IID_ICancelMethodCalls, (void **) &pCancel) ;

/*
Sleep for the number of seconds specified in the input parameter,
pausing every second to see if we are supposed to cancel.
*/
	int i ;
	for (i = 0 ; i < InputParm ; i ++)
	{
		Sleep (1000) ;

		if (pCancel->TestCancel ( ) == RPC_E_CALL_CANCELED)
		{
			MessageBox (NULL, "Cancel detected by server", "", 0) ;
			return RPC_E_CALL_CANCELED ;
		}
	}
	
/*
Return the input parameter incremented by one, so the caller knows
that we have successfully done what he asked for.
*/

	*pOutputParm = InputParm + 1 ;
	return S_OK;
}
