home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 14 / IOPROG_14.ISO / soft / sdkjava / sdkjava.exe / SDKJava.cab / Samples / native_raw / native / natlib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-03-05  |  4.7 KB  |  188 lines

  1.  
  2. /*
  3.  
  4. Copyright (c) 1996-1997  Microsoft Corporation
  5.  
  6. */
  7.  
  8. #include <windows.h>
  9. #include <native.h>
  10. #include "JMain.h"
  11. #include "JFoo.h"
  12. #include "JThread1.h"
  13. #include "JThread2.h"
  14.  
  15. ////////////////////////////////////////////////////////////////////////////////
  16. //
  17. //    RNIGetCompatibleVersion
  18. //
  19. //    Called by the virtual machine to determine what level of RNI compatibility
  20. //    this DLL offers.
  21. //
  22. ////
  23. DWORD __cdecl RNIGetCompatibleVersion()
  24. {
  25.     return RNIVER;
  26. }
  27.  
  28. ////////////////////////////////////////////////////////////////////////////////
  29. //
  30. //    JMain_Square()
  31. //
  32. //    returns the square of its parameter;  simple callout from java to
  33. //    native code to perform this operation.
  34. //
  35. ////
  36. __declspec(dllexport) long __cdecl JMain_Square(struct HJMain *pThis, long lVal)
  37. {
  38.     return lVal * lVal ;
  39. }
  40.  
  41. ////////////////////////////////////////////////////////////////////////////////
  42. //
  43. //    JMain_DoubleInstanceVar()
  44. //
  45. //    executes a java-implemented method to retrieve the value of an
  46. //    instance variable;  returns twice the retrieved value.
  47. //
  48. ////
  49. __declspec(dllexport) void __cdecl JMain_DoubleInstanceVar(struct HJMain *pThis)
  50. {
  51.     long lVal ;
  52.  
  53.     lVal = execute_java_dynamic_method(0, (HObject*) pThis, "GetiVar1", "()I") ;
  54.  
  55.     pThis->iVar1 = lVal << 1 ;
  56. }
  57.  
  58. ////////////////////////////////////////////////////////////////////////////////
  59. //
  60. //    Jmain_showMemberArray()
  61. //
  62. //    executes a class method which returns a private member array of
  63. //    ints (java); loops through and displays the array members.
  64. //
  65. ////
  66. __declspec(dllexport) void __cdecl JMain_showMemberArray(struct HJMain *pThis)
  67. {
  68.     ClassArrayOfInt *paiVar1 ;
  69.     long    *plVar2 ;
  70.     int    cIndex ;
  71.  
  72.     // get a pointer to an instance array of int, and display it.
  73.     paiVar1 = (ClassArrayOfInt *) execute_java_dynamic_method(0, (HObject *) pThis, "GetiCVar2", "()[I") ;
  74.     plVar2 = paiVar1->body ;
  75.  
  76.     for (cIndex = 0; cIndex < paiVar1->length; cIndex++)
  77.     {
  78.         printf("%5d", *plVar2++) ;
  79.     }
  80.     printf("\n") ;
  81. }
  82.  
  83. ////////////////////////////////////////////////////////////////////////////////
  84. //
  85. //    JMain_Various()
  86. //
  87. //    finds JFoo class; executes a static method in that class
  88. //
  89. ////
  90. __declspec(dllexport) void __cdecl JMain_Various(struct HJMain *pThis)
  91. {
  92.     _int64 i64Val = 10 ;
  93.     ClassClass *pcls = FindClass(NULL, "JFoo", TRUE) ;
  94.  
  95.     if (pcls)
  96.     {
  97.         // and exec the static method
  98.         execute_java_static_method(NULL, pcls, "StaticMethod", "(J)V", i64Val) ;
  99.     }
  100. }
  101.  
  102. ////////////////////////////////////////////////////////////////////////////////
  103. //
  104. //    w32CreateEvent()
  105. //
  106. //    wrapper for win32 API CreateEvent()
  107. //
  108. ////
  109. __declspec(dllexport) long __cdecl JMain_w32CreateEvent(struct HJMain *pThis, long bInit)
  110. {
  111.     BOOL    bInitialState ;
  112.     HANDLE    hEvent ;
  113.  
  114.     // determine if initial state is signaled or non-signaled.
  115.     if (bInit)
  116.     {
  117.         bInitialState = TRUE ;
  118.     }
  119.     else
  120.     {
  121.         bInitialState = FALSE ;
  122.     }
  123.  
  124.     // create Event object and return handle
  125.     return (long) CreateEvent(NULL, FALSE, bInitialState, NULL) ;
  126. }
  127.  
  128. ////////////////////////////////////////////////////////////////////////////////
  129. //
  130. //    w32PulseEvent()
  131. //
  132. //    wrapper for win32 API PulseEvent()
  133. //
  134. ////
  135. __declspec(dllexport) void __cdecl JThread1_w32PulseEvent(struct HJThread1 *pThis,long hEvent)
  136. {
  137.     PulseEvent((HANDLE) hEvent) ;
  138. }
  139.  
  140. ////////////////////////////////////////////////////////////////////////////////
  141. //
  142. //    w32CloseHandle()
  143. //
  144. //    wrapper for win32 API CloseHandle()
  145. //
  146. ////
  147. __declspec(dllexport) long __cdecl JThread2_w32CloseHandle(struct HJThread2 *pThis, long hObject)
  148. {
  149.     return (long) CloseHandle((HANDLE) hObject) ;
  150. }
  151.  
  152. ////////////////////////////////////////////////////////////////////////////////
  153. //
  154. //    GCSafeNative()
  155. //
  156. //    method demonstrates GC safe way of performing GC-sensitive operations
  157. //    in native code.
  158. //
  159. ////
  160. __declspec(dllexport) void __cdecl JThread2_GCSafeNative(struct HJThread2 *pThis, long hEvent)
  161. {
  162.     struct
  163.     {
  164.         HJFoo *pJFoo1, *pJFoo2 ;
  165.     } gcSafe ;                // struct holds out pointers to JObjects
  166.     GCFrame gcf ;
  167.  
  168.     // GCFramePush() creates storage for GC-sensitive data (JObjects in this case)
  169.     GCFramePush(&gcf, &gcSafe, sizeof(gcSafe)) ;
  170.  
  171.     // set our member variables to point to two JObjects
  172.     gcSafe.pJFoo1 = (HJFoo *) execute_java_constructor(0, "JFoo", 0, "(I)", 10) ;
  173.     gcSafe.pJFoo2 = (HJFoo *) execute_java_constructor(0, "JFoo", 0, "()") ;
  174.  
  175.     // call GCEnable() - we don't know how long we might be blocked
  176.     GCEnable() ;
  177.  
  178.     // block
  179.     WaitForSingleObject((HANDLE) hEvent, INFINITE) ;
  180.  
  181.     // display instance variable values for above-created JObjects.
  182.     printf("gcSafe.pJFoo1->m_iVal = %d\tgcSafe.pJFoo2->m_iVal = %d\n", gcSafe.pJFoo1->m_iVal, gcSafe.pJFoo2->m_iVal) ;
  183.  
  184.     // disable GC and pop our storage
  185.     GCDisable() ;
  186.     GCFramePop(&gcf) ;
  187. }
  188.