home *** CD-ROM | disk | FTP | other *** search
- /*
- * File: VarDate.cpp
- *
- * Java/COM marshaling sample demonstrating conversion from VARIANT DATE
- * values (VT_DATE) to Java Date objects.
- *
- * (C) Copyright 1999 Microsoft Corporation. All rights reserved.
- */
-
- #include <objbase.h>
- #include "VarDate.h"
-
- #include <stdio.h>
-
-
- ///////////////////////////////////////////////////////////////////////////////
- // utility methods.
- ///////////////////////////////////////////////////////////////////////////////
-
- void
- PrintDateValue(
- LPCSTR pcszContext,
- DATE date )
- {
- VARIANT var;
- char buffer[ 256 ];
-
- // convert the DATE value to a BSTR.
- var.vt = VT_DATE;
- var.date = date;
-
- if( SUCCEEDED( VariantChangeType( &var, &var, 0, VT_BSTR ) ) )
- {
- sprintf( buffer, "C, %s: %ls.\n", pcszContext, var.bstrVal );
-
- // make sure we free the BSTR.
- VariantClear( &var );
- }
- else
- {
- sprintf( buffer, "C, %s: UNMAPPABLE.\n", pcszContext );
- }
-
- printf( buffer );
- }
-
- // returns TRUE if successful.
- HRESULT
- CreateNewDate(
- BOOL fWait,
- DATE *pDateOut )
- {
- SYSTEMTIME systemTime;
- INT nResult;
-
- if( fWait )
- {
- printf( "C, waiting for ~2 seconds.\n" );
-
- // sleep for ~2 seconds.
- Sleep( 2000 );
- }
-
- // get the current local time and convert it to a DATE value.
- GetLocalTime( &systemTime );
- nResult = SystemTimeToVariantTime( &systemTime, pDateOut );
-
- return( nResult ? S_OK : E_FAIL );
- }
-
- ///////////////////////////////////////////////////////////////////////////////
- // VarDateSample impl
- ///////////////////////////////////////////////////////////////////////////////
-
- class VarDateSampleNative : public IVarDateSample
- {
- private:
- ULONG m_cRef;
-
- public:
- VarDateSampleNative( void );
-
- HRESULT _stdcall QueryInterface( REFIID riid, void **ppvObject );
- ULONG _stdcall AddRef( void );
- ULONG _stdcall Release( void );
-
- HRESULT __stdcall ByValue( /* [in] */ DATE date );
-
- HRESULT __stdcall ByReferenceRetval( /* [retval][out] */ DATE *pDate );
-
- HRESULT __stdcall ByReferenceIn( /* [in] */ DATE *pDate );
-
- HRESULT __stdcall ByReferenceOut( /* [out] */ DATE *pDate );
-
- HRESULT __stdcall ByReferenceInOut( /* [out][in] */ DATE *pDate );
-
- HRESULT __stdcall TestJavaToNative(
- /* [in] */ IVarDateSample *pIDateSampleNative );
- };
-
-
- VarDateSampleNative::VarDateSampleNative( void )
- {
- m_cRef = 1;
- }
-
-
- HRESULT _stdcall
- VarDateSampleNative::QueryInterface(
- REFIID riid,
- void **ppvObject )
- {
- if( (riid == IID_IUnknown) ||
- (riid == IID_IVarDateSample) )
- {
- *ppvObject = (IVarDateSample *)this;
- }
- else
- {
- *ppvObject = NULL;
- return E_NOINTERFACE;
- }
-
- ((IUnknown *)*ppvObject)->AddRef();
-
- return S_OK;
- }
-
-
- ULONG _stdcall
- VarDateSampleNative::AddRef( void )
- {
- return (ULONG)InterlockedIncrement( (LONG *)&m_cRef );
- }
-
-
- ULONG _stdcall
- VarDateSampleNative::Release( void )
- {
- ULONG cRefRet;
-
- cRefRet = (ULONG)InterlockedDecrement( (LONG *)&m_cRef );
-
- if( cRefRet == 0 )
- delete this;
-
- return cRefRet;
- }
-
-
- HRESULT _stdcall
- VarDateSampleNative::ByValue(
- DATE date )
- {
- PrintDateValue( "ByValue() got", date );
-
- return S_OK;
- }
-
- HRESULT __stdcall
- VarDateSampleNative::ByReferenceRetval(
- DATE *pDate )
- {
- HRESULT hr;
-
- // create new date.
- hr = CreateNewDate( TRUE, pDate );
- if( SUCCEEDED( hr ) )
- {
- PrintDateValue( "ByReferenceRetval() \"returning\"", *pDate );
- }
-
- return hr;
- }
-
- HRESULT __stdcall
- VarDateSampleNative::ByReferenceIn(
- DATE *pDate )
- {
- PrintDateValue( "ByReferenceIn() got", *pDate);
-
- return S_OK;
- }
-
- HRESULT __stdcall
- VarDateSampleNative::ByReferenceOut(
- DATE *pDate )
- {
- HRESULT hr;
-
- // create new date.
- hr = CreateNewDate( TRUE, pDate );
- if( SUCCEEDED( hr ) )
- {
- PrintDateValue( "ByReferenceOut() \"returning\"", *pDate );
- }
-
- return hr;
- }
-
-
- HRESULT __stdcall
- VarDateSampleNative::ByReferenceInOut(
- DATE *pDate )
- {
- HRESULT hr;
-
- PrintDateValue( "ByReferenceInOut() got", *pDate );
-
- // create new date.
- hr = CreateNewDate( TRUE, pDate );
- if( SUCCEEDED( hr ) )
- {
- PrintDateValue( "ByReferenceInOut() \"returning\"", *pDate );
- }
-
- return hr;
- }
-
- HRESULT _stdcall
- VarDateSampleNative::TestJavaToNative(
- IVarDateSample *pIDateSampleNative )
- {
- // this method called only on java-implemented objects.
- return E_NOTIMPL;
- }
-
- ///////////////////////////////////////////////////////////////////////////////
- // main
- ///////////////////////////////////////////////////////////////////////////////
- int main( int argc, char **argv )
- {
- HRESULT hr;
-
- hr = CoInitialize( NULL );
-
- if( SUCCEEDED( hr ) )
- {
- IVarDateSample *pIDateSampleJava;
-
- hr = CoCreateInstance( CLSID_VarDateSample, NULL,
- CLSCTX_INPROC_SERVER, IID_IVarDateSample,
- (void **)&pIDateSampleJava );
-
- if( SUCCEEDED( hr ) )
- {
- VarDateSampleNative *pDateSampleNative = new VarDateSampleNative();
-
- if( pDateSampleNative != NULL )
- {
- DATE date;
-
- // create a new DATE value and then call the Java
- // implementation of ByValue().
- hr = CreateNewDate( FALSE, &date );
- if( SUCCEEDED( hr ) )
- {
- PrintDateValue( "before calling ByValue()", date );
-
- hr = pIDateSampleJava->ByValue( date );
- }
-
- // call the Java implementation of ByReferenceRetval().
- if( SUCCEEDED( hr ) )
- {
- hr = pIDateSampleJava->ByReferenceRetval( &date );
-
- if( SUCCEEDED( hr ) )
- {
- PrintDateValue( "received from ByReferenceRetval()",
- date );
- }
- }
-
- // create a new DATE value and then call the Java
- // implementation of ByReferenceIn().
- if( SUCCEEDED( hr ) )
- {
- hr = CreateNewDate( TRUE, &date );
-
- PrintDateValue( "before calling ByReferenceIn()", date );
-
- hr = pIDateSampleJava->ByReferenceIn( &date );
- }
-
- // call the Java implementation of ByReferenceOut().
- if( SUCCEEDED( hr ) )
- {
- hr = pIDateSampleJava->ByReferenceOut( &date );
-
- if( SUCCEEDED( hr ) )
- {
- PrintDateValue( "received from ByReferenceOut()",
- date );
- }
- }
-
- // create a new DATE value and then call the Java
- // implementation of ByReferenceInOut().
- if( SUCCEEDED( hr ) )
- {
- hr = CreateNewDate( TRUE, &date );
-
- PrintDateValue( "before calling ByReferenceInOut()", date );
-
- hr = pIDateSampleJava->ByReferenceInOut( &date );
-
- if( SUCCEEDED( hr ) )
- {
- PrintDateValue( "received from ByReferenceInOut()",
- date );
- }
- }
-
- if( SUCCEEDED( hr ) )
- {
- // ask Java object to conduct java -> native marshaling
- // tests.
- pIDateSampleJava->TestJavaToNative(
- (IVarDateSample *)pDateSampleNative );
- }
-
- // release the native implementation of IVarDateSample.
- pDateSampleNative->Release();
- }
-
- pIDateSampleJava->Release();
- }
-
- CoUninitialize();
- }
-
- if( SUCCEEDED( hr ) )
- {
- printf( "tests done!\n" );
- }
- else
- {
- printf( "test failed!\n" );
- }
-
- return 0;
- }
-