home *** CD-ROM | disk | FTP | other *** search
/ Chip Special: HTML & Java / Chip-Special_1997-01_HTML-a-Java.bin / javasdk / sdk-java.exe / SDKJava.cab / Samples / native_raw / native / natlib.c < prev   
Encoding:
C/C++ Source or Header  |  1996-10-10  |  4.4 KB  |  175 lines

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