home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 39 / IOPROG_39.ISO / SOFT / sdkjava40.exe / data1.cab / fg_Samples / Samples / COM / CustomMarshal / vardate / VarDate.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-04  |  8.4 KB  |  344 lines

  1. /*
  2.  * File: VarDate.cpp
  3.  *
  4.  * Java/COM marshaling sample demonstrating conversion from VARIANT DATE
  5.  *  values (VT_DATE) to Java Date objects.
  6.  *
  7.  * (C) Copyright 1999 Microsoft Corporation.  All rights reserved.
  8.  */
  9.  
  10. #include <objbase.h>
  11. #include "VarDate.h"
  12.  
  13. #include <stdio.h>
  14.  
  15.  
  16. ///////////////////////////////////////////////////////////////////////////////
  17. // utility methods.
  18. ///////////////////////////////////////////////////////////////////////////////
  19.  
  20. void
  21. PrintDateValue(
  22.     LPCSTR  pcszContext,
  23.     DATE    date )
  24. {
  25.     VARIANT var;
  26.     char    buffer[ 256 ];
  27.  
  28.     // convert the DATE value to a BSTR.
  29.     var.vt = VT_DATE;
  30.     var.date = date;
  31.  
  32.     if( SUCCEEDED( VariantChangeType( &var, &var, 0, VT_BSTR ) ) )
  33.     {
  34.         sprintf( buffer, "C, %s: %ls.\n", pcszContext, var.bstrVal );
  35.  
  36.         // make sure we free the BSTR.
  37.         VariantClear( &var );
  38.     }
  39.     else
  40.     {
  41.         sprintf( buffer, "C, %s: UNMAPPABLE.\n", pcszContext );
  42.     }
  43.  
  44.     printf( buffer );
  45. }
  46.  
  47. // returns TRUE if successful.
  48. HRESULT
  49. CreateNewDate(
  50.     BOOL    fWait,
  51.     DATE    *pDateOut )
  52. {
  53.     SYSTEMTIME  systemTime;
  54.     INT         nResult;
  55.  
  56.     if( fWait )
  57.     {      
  58.         printf( "C, waiting for ~2 seconds.\n" );
  59.  
  60.         // sleep for ~2 seconds.
  61.         Sleep( 2000 );
  62.     }
  63.  
  64.     // get the current local time and convert it to a DATE value.
  65.     GetLocalTime( &systemTime );
  66.     nResult = SystemTimeToVariantTime( &systemTime, pDateOut );
  67.  
  68.     return( nResult ? S_OK : E_FAIL );
  69. }
  70.  
  71. ///////////////////////////////////////////////////////////////////////////////
  72. // VarDateSample impl
  73. ///////////////////////////////////////////////////////////////////////////////
  74.  
  75. class VarDateSampleNative : public IVarDateSample
  76. {
  77. private:
  78.     ULONG   m_cRef;
  79.  
  80. public:
  81.     VarDateSampleNative( void );
  82.  
  83.     HRESULT _stdcall    QueryInterface( REFIID riid, void **ppvObject );
  84.     ULONG _stdcall      AddRef( void );
  85.     ULONG _stdcall      Release( void );
  86.  
  87.     HRESULT __stdcall   ByValue( /* [in] */ DATE date );
  88.         
  89.     HRESULT __stdcall   ByReferenceRetval( /* [retval][out] */ DATE *pDate );
  90.         
  91.     HRESULT __stdcall   ByReferenceIn( /* [in] */ DATE *pDate );
  92.         
  93.     HRESULT __stdcall   ByReferenceOut( /* [out] */ DATE *pDate );
  94.         
  95.     HRESULT __stdcall   ByReferenceInOut( /* [out][in] */ DATE *pDate );
  96.         
  97.     HRESULT __stdcall   TestJavaToNative(
  98.         /* [in] */ IVarDateSample *pIDateSampleNative );
  99. };
  100.  
  101.  
  102. VarDateSampleNative::VarDateSampleNative( void )
  103. {
  104.     m_cRef = 1;
  105. }
  106.  
  107.  
  108. HRESULT _stdcall
  109. VarDateSampleNative::QueryInterface(
  110.     REFIID  riid,
  111.     void    **ppvObject )
  112. {
  113.     if( (riid == IID_IUnknown) ||
  114.             (riid == IID_IVarDateSample) )
  115.     {
  116.         *ppvObject = (IVarDateSample *)this;
  117.     }
  118.     else
  119.     {
  120.         *ppvObject = NULL;
  121.         return E_NOINTERFACE;
  122.     }
  123.  
  124.     ((IUnknown *)*ppvObject)->AddRef();
  125.  
  126.     return S_OK;
  127. }
  128.  
  129.  
  130. ULONG _stdcall
  131. VarDateSampleNative::AddRef( void )
  132. {
  133.     return (ULONG)InterlockedIncrement( (LONG *)&m_cRef );
  134. }
  135.  
  136.  
  137. ULONG _stdcall
  138. VarDateSampleNative::Release( void )
  139. {
  140.     ULONG cRefRet;
  141.  
  142.     cRefRet = (ULONG)InterlockedDecrement( (LONG *)&m_cRef );
  143.  
  144.     if( cRefRet == 0 )
  145.         delete this;
  146.  
  147.     return cRefRet;
  148. }
  149.  
  150.  
  151. HRESULT _stdcall
  152. VarDateSampleNative::ByValue(
  153.     DATE    date )
  154. {
  155.     PrintDateValue( "ByValue() got", date );
  156.  
  157.     return S_OK;
  158. }
  159.         
  160. HRESULT __stdcall
  161. VarDateSampleNative::ByReferenceRetval(
  162.     DATE *pDate )
  163. {
  164.     HRESULT hr;
  165.  
  166.     // create new date.
  167.     hr = CreateNewDate( TRUE, pDate );
  168.     if( SUCCEEDED( hr ) )
  169.     {
  170.         PrintDateValue( "ByReferenceRetval() \"returning\"", *pDate );
  171.     }
  172.  
  173.     return hr;
  174. }
  175.         
  176. HRESULT __stdcall
  177. VarDateSampleNative::ByReferenceIn(
  178.     DATE *pDate )
  179. {
  180.     PrintDateValue( "ByReferenceIn() got", *pDate);
  181.  
  182.     return S_OK;
  183. }
  184.         
  185. HRESULT __stdcall
  186. VarDateSampleNative::ByReferenceOut(
  187.     DATE *pDate )
  188. {
  189.     HRESULT hr;
  190.  
  191.     // create new date.
  192.     hr = CreateNewDate( TRUE, pDate );
  193.     if( SUCCEEDED( hr ) )
  194.     {
  195.         PrintDateValue( "ByReferenceOut() \"returning\"", *pDate );
  196.     }
  197.  
  198.     return hr;
  199. }
  200.  
  201.  
  202. HRESULT __stdcall
  203. VarDateSampleNative::ByReferenceInOut(
  204.     DATE *pDate )
  205. {
  206.     HRESULT hr;
  207.  
  208.     PrintDateValue( "ByReferenceInOut() got", *pDate );
  209.  
  210.     // create new date.
  211.     hr = CreateNewDate( TRUE, pDate );
  212.     if( SUCCEEDED( hr ) )
  213.     {
  214.         PrintDateValue( "ByReferenceInOut() \"returning\"", *pDate );
  215.     }
  216.  
  217.     return hr;
  218. }
  219.  
  220. HRESULT _stdcall
  221. VarDateSampleNative::TestJavaToNative(
  222.     IVarDateSample *pIDateSampleNative )
  223. {
  224.     // this method called only on java-implemented objects.
  225.     return E_NOTIMPL;
  226. }
  227.  
  228. ///////////////////////////////////////////////////////////////////////////////
  229. // main
  230. ///////////////////////////////////////////////////////////////////////////////
  231. int main( int argc, char **argv )
  232. {
  233.     HRESULT hr;
  234.  
  235.     hr = CoInitialize( NULL );
  236.  
  237.     if( SUCCEEDED( hr ) )
  238.     {
  239.         IVarDateSample  *pIDateSampleJava;
  240.  
  241.         hr = CoCreateInstance( CLSID_VarDateSample, NULL,
  242.                                 CLSCTX_INPROC_SERVER, IID_IVarDateSample,
  243.                                 (void **)&pIDateSampleJava );
  244.  
  245.         if( SUCCEEDED( hr ) )
  246.         {
  247.             VarDateSampleNative *pDateSampleNative = new VarDateSampleNative();
  248.  
  249.             if( pDateSampleNative != NULL )
  250.             {
  251.                 DATE    date;
  252.  
  253.                 // create a new DATE value and then call the Java
  254.                 //  implementation of ByValue().
  255.                 hr = CreateNewDate( FALSE, &date );
  256.                 if( SUCCEEDED( hr ) )
  257.                 {
  258.                     PrintDateValue( "before calling ByValue()", date );
  259.  
  260.                     hr = pIDateSampleJava->ByValue( date );
  261.                 }
  262.  
  263.                 // call the Java implementation of ByReferenceRetval().
  264.                 if( SUCCEEDED( hr ) )
  265.                 {
  266.                     hr = pIDateSampleJava->ByReferenceRetval( &date );
  267.  
  268.                     if( SUCCEEDED( hr ) )
  269.                     {
  270.                         PrintDateValue( "received from ByReferenceRetval()",
  271.                             date );
  272.                     }
  273.                 }
  274.  
  275.                 // create a new DATE value and then call the Java
  276.                 //  implementation of ByReferenceIn().
  277.                 if( SUCCEEDED( hr ) )
  278.                 {
  279.                     hr = CreateNewDate( TRUE, &date );
  280.  
  281.                     PrintDateValue( "before calling ByReferenceIn()", date );
  282.  
  283.                     hr = pIDateSampleJava->ByReferenceIn( &date );
  284.                 }
  285.  
  286.                 // call the Java implementation of ByReferenceOut().
  287.                 if( SUCCEEDED( hr ) )
  288.                 {
  289.                     hr = pIDateSampleJava->ByReferenceOut( &date );
  290.  
  291.                     if( SUCCEEDED( hr ) )
  292.                     {
  293.                         PrintDateValue( "received from ByReferenceOut()",
  294.                             date );
  295.                     }
  296.                 }
  297.  
  298.                 // create a new DATE value and then call the Java
  299.                 //  implementation of ByReferenceInOut().
  300.                 if( SUCCEEDED( hr ) )
  301.                 {
  302.                     hr = CreateNewDate( TRUE, &date );
  303.  
  304.                     PrintDateValue( "before calling ByReferenceInOut()", date );
  305.  
  306.                     hr = pIDateSampleJava->ByReferenceInOut( &date );
  307.  
  308.                     if( SUCCEEDED( hr ) )
  309.                     {
  310.                         PrintDateValue( "received from ByReferenceInOut()",
  311.                             date );
  312.                     }
  313.                 }
  314.  
  315.                 if( SUCCEEDED( hr ) )
  316.                 {
  317.                     // ask Java object to conduct java -> native marshaling
  318.                     //  tests.
  319.                     pIDateSampleJava->TestJavaToNative(
  320.                         (IVarDateSample *)pDateSampleNative );
  321.                 }
  322.  
  323.                 // release the native implementation of IVarDateSample.
  324.                 pDateSampleNative->Release();
  325.             }
  326.  
  327.             pIDateSampleJava->Release();
  328.         }
  329.  
  330.         CoUninitialize();
  331.     }
  332.  
  333.     if( SUCCEEDED( hr ) )
  334.     {
  335.         printf( "tests done!\n" );
  336.     }
  337.     else
  338.     {
  339.         printf( "test failed!\n" );
  340.     }
  341.  
  342.     return 0;
  343. }
  344.