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

  1. // (C) Copyright 1995 - 1999 Microsoft Corporation.  All rights reserved.
  2.  
  3. #pragma hdrstop
  4.  
  5. #define INITGUID 1
  6. #include <windows.h>
  7. #include "native.h" // Raw Native Interface declarations.
  8.  
  9.  
  10. typedef RECT *PRECT, **PPRECT;
  11.  
  12.  
  13.  
  14. #ifdef __cplusplus
  15. #define EXTERNC     extern "C"
  16. #else
  17. #define EXTERNC
  18. #endif
  19.  
  20.  
  21. // Macro for declaring RNI Java methods's (and encapsulating the java class package.)
  22. // TODO: Replace "RectMarshaler" with the name of your hook class. 
  23. #define JAVAMETHOD(typ, name) \
  24.     __declspec(dllexport) \
  25.     EXTERNC \
  26.     typ __cdecl RectMarshaler_##name
  27.  
  28. #define LOGCALL(hookname) OutputDebugString("RectMarshaler." hookname "() called.\n")
  29.  
  30. // Export that tells what RNI version we were built under.
  31. DWORD __cdecl RNIGetCompatibleVersion()
  32. {
  33.   return RNIVER;
  34. }
  35.  
  36. // Cache the various class and fields we will need.
  37. ClassClass*         gRectangleClass;
  38. struct fieldblock*  gRectangleXField;
  39. struct fieldblock*  gRectangleYField;
  40. struct fieldblock*  gRectangleWidthField;
  41. struct fieldblock*  gRectangleHeightField;
  42.  
  43.  
  44.  
  45. //==========================================================================
  46. // toJava
  47. //==========================================================================
  48. JAVAMETHOD(OBJECT*, toJava) (OBJECT*x, PPRECT ppRECT, int flags)
  49. {
  50.  
  51.     PRECT prect = *ppRECT;
  52.  
  53.     LOGCALL("toJava");
  54.  
  55.  
  56.  
  57.     return execute_java_constructor(NULL,
  58.                                     NULL,
  59.                                     gRectangleClass,
  60.                                     "(IIII)",
  61.                                     prect->left,
  62.                                     prect->top,
  63.                                     prect->right - prect->left,
  64.                                     prect->bottom - prect->top);
  65. }
  66.  
  67.  
  68.  
  69. void copyToExternalWorker(OBJECT *javaval, PRECT pRECT)
  70. {
  71.  
  72.     int x, y, width, height;
  73.  
  74.     x      = Field_GetInt(javaval, gRectangleXField);
  75.     y      = Field_GetInt(javaval, gRectangleYField);
  76.     width  = Field_GetInt(javaval, gRectangleWidthField);
  77.     height = Field_GetInt(javaval, gRectangleHeightField);
  78.  
  79.     pRECT->left   = x;
  80.     pRECT->top    = y;
  81.     pRECT->right  = x + width;
  82.     pRECT->bottom = y + height;
  83. }
  84.  
  85.  
  86. //==========================================================================
  87. // copyToExternal
  88. //==========================================================================
  89. JAVAMETHOD(void, copyToExternal) (OBJECT*x, OBJECT* javaval, PPRECT ppRECT, int flags)
  90. {
  91.     LOGCALL("copyToExternal");
  92.  
  93.     copyToExternalWorker(javaval, *ppRECT);
  94. }
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101. //==========================================================================
  102. // toExternal
  103. //==========================================================================
  104. JAVAMETHOD(void, toExternal) (OBJECT*x, OBJECT* javaval, int flags, PPRECT ppRECT)
  105. {
  106.     PRECT pRECT = NULL;
  107.  
  108.     LOGCALL("toExternal");
  109.  
  110.     pRECT = (RECT*)CoTaskMemAlloc(sizeof(RECT));
  111.     if (!pRECT) {
  112.         SignalErrorPrintf("java/lang/OutOfMemoryError", "Could not allocate from task heap!");
  113.     } else {
  114.         char msg[100];
  115.         wsprintf(msg, "Hook allocated RECT at %lxh\n", pRECT);
  116.         OutputDebugString(msg);
  117.         copyToExternalWorker(javaval, pRECT);
  118.         *ppRECT = pRECT;
  119.     }
  120. }
  121.  
  122.  
  123. //==========================================================================
  124. // releaseExternal
  125. //==========================================================================
  126. JAVAMETHOD(void, releaseExternal) (OBJECT*x, PPRECT ppRECT, int flags)
  127. {
  128.     PRECT pRECT = *ppRECT;
  129.     char msg[100];
  130.  
  131.     LOGCALL("releaseExternal");
  132.     wsprintf(msg, "Hook freeing RECT at %lxh\n", pRECT);
  133.     OutputDebugString(msg);
  134.     if (pRECT) {
  135.         CoTaskMemFree(pRECT);
  136.     }
  137. }
  138.  
  139.  
  140. //==========================================================================
  141. // classInit (one time initialization routine)
  142. //==========================================================================
  143. JAVAMETHOD(void, classInit) (OBJECT*x)
  144. {
  145.     gRectangleClass = FindClass(NULL, "java/awt/Rectangle", TRUE);
  146.     if (!gRectangleClass) {
  147.         SignalErrorPrintf("java/lang/ClassNotFoundException", "Could not preload java.awt.Rectangle class!");
  148.     }
  149.  
  150.     gRectangleXField = Class_GetField(gRectangleClass, "x");
  151.     if (!gRectangleXField) {
  152.         SignalErrorPrintf("java/lang/ClassNotFoundException", "Bogus java.awt.Rectangle class!");
  153.     }
  154.  
  155.     gRectangleYField = Class_GetField(gRectangleClass, "y");
  156.     if (!gRectangleYField) {
  157.         SignalErrorPrintf("java/lang/ClassNotFoundException", "Bogus java.awt.Rectangle class!");
  158.     }
  159.  
  160.     gRectangleWidthField = Class_GetField(gRectangleClass, "width");
  161.     if (!gRectangleWidthField) {
  162.         SignalErrorPrintf("java/lang/ClassNotFoundException", "Bogus java.awt.Rectangle class!");
  163.     }
  164.  
  165.     gRectangleHeightField = Class_GetField(gRectangleClass, "height");
  166.     if (!gRectangleHeightField) {
  167.         SignalErrorPrintf("java/lang/ClassNotFoundException", "Bogus java.awt.Rectangle class!");
  168.     }
  169. }
  170.  
  171.  
  172.  
  173.  
  174.  
  175. //==========================================================================
  176. // TODO: Remove this copy of DllMain if you are embedding this file in your
  177. //  own DLL.
  178. // DLL Loader entry Rectangle.
  179. //==========================================================================
  180. BOOL WINAPI DllMain(HINSTANCE hmod, DWORD dwReason,
  181.                                 PVOID pvReserved)
  182. {
  183.    return TRUE;
  184. }
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217.