#include <windows.h>
#include "ctimedta.h"

extern UINT cfThreadId ;
extern CTimeData *g_pTimeDataObj ;
extern BOOL g_ThreadShutdownFlag ;

unsigned long __stdcall TimerProc (void *pStream)
{
	IAdviseSink *pAdvSink ;

/*
Unmarshal the interface pointer.
*/

	HRESULT hr = CoGetInterfaceAndReleaseStream (
		(IStream *)pStream, 
		IID_IAdviseSink, 
		(void **)&pAdvSink) ;

	if (hr != S_OK)
	{
		MessageBox (NULL, "GetInterface fail", "", 0) ;
	}


	for (;;)
	{
/*
Check shutdown conditions. Quit when signaled to do so.
*/

		if (g_ThreadShutdownFlag == TRUE || g_pTimeDataObj == NULL)
		{
			pAdvSink->Release() ;
			return 0 ;
		}

/*
Send data change to waiting client app.
*/

		FORMATETC fe ;	STGMEDIUM stg ;

		fe.cfFormat = cfThreadId ;
		fe.tymed = TYMED_HGLOBAL ;
		fe.dwAspect = DVASPECT_CONTENT ;
		fe.lindex = -1 ;
		fe.ptd = NULL ;

		DWORD *pdw ;
		pdw = (DWORD *)GlobalAlloc(GMEM_FIXED, sizeof (DWORD));		
		*pdw = GetCurrentThreadId ( ) ;
		
	    stg.tymed = TYMED_HGLOBAL ;
    	stg.hGlobal = (HGLOBAL) pdw ;
	   	stg.pUnkForRelease = NULL ; 

		pAdvSink->OnDataChange (&fe, &stg) ;

/*
Sleep for a second, then do it again.
*/
		Sleep (1000) ;
	}

	return 0 ;
}

