home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 39 / IOPROG_39.ISO / SOFT / sdkjava40.exe / data1.cab / fg_Samples / Samples / COM / CustomMarshal / ansistr / AnsiMarshaler.c next >
Encoding:
C/C++ Source or Header  |  2000-05-04  |  3.1 KB  |  155 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 CHAR *PSTR, **PPSTR;
  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 "AnsiMarshaler" with the name of your hook class. 
  23. #define JAVAMETHOD(typ, name) \
  24.     __declspec(dllexport) \
  25.     EXTERNC \
  26.     typ __cdecl AnsiMarshaler_##name
  27.  
  28.  
  29. #define LOGCALL(hookname) OutputDebugString("AnsiMarshaler." hookname "() called.\n")
  30.  
  31. // Export that tells what RNI version we were built under.
  32. DWORD __cdecl RNIGetCompatibleVersion()
  33. {
  34.   return RNIVER;
  35. }
  36.  
  37.  
  38. //==========================================================================
  39. // toJava
  40. //==========================================================================
  41. JAVAMETHOD(HString*, toJava) (OBJECT*x, PPSTR pPSTR, int flags)
  42. {
  43.     PSTR pstr = *pPSTR;
  44.  
  45.     LOGCALL("toJava");
  46.  
  47.     if (pstr == NULL) {
  48.         return NULL;
  49.     } else {
  50.         return makeJavaString(pstr, lstrlen(pstr));
  51.     }
  52. }
  53.  
  54.  
  55.  
  56.  
  57. //==========================================================================
  58. // toExternal
  59. //==========================================================================
  60. JAVAMETHOD(void, toExternal) (OBJECT*x, HString* javaval, int flags, PPSTR pPSTR)
  61. {
  62.     PSTR pstr = NULL;
  63.  
  64.     LOGCALL("toExternal");
  65.  
  66.     if (javaval != NULL) {
  67.  
  68.         DWORD len = javaStringLength(javaval);
  69.         pstr = (LPSTR)CoTaskMemAlloc(len+1);
  70.         if (!pstr) {
  71.             SignalErrorPrintf("java/lang/OutOfMemoryError", "Could not allocate from task heap!");
  72.         } else {
  73.             char msg[100];
  74.             DWORD i;
  75.             unicode *pwc = javaStringStart(javaval);
  76.  
  77.             wsprintf(msg, "Hook created new string at %lxh\n", pstr);
  78.             OutputDebugString(msg);
  79.  
  80.             for (i = 0; i < len; i++) {
  81.                 pstr[i] = (CHAR)(pwc[i]);
  82.             }
  83.             pstr[i] = '\0';
  84.         }
  85.  
  86.     }
  87.     *pPSTR = pstr;
  88. }
  89.  
  90.  
  91. //==========================================================================
  92. // releaseExternal
  93. //==========================================================================
  94. JAVAMETHOD(void, releaseExternal) (OBJECT*x, PPSTR pPSTR, int flags)
  95. {
  96.     PSTR pstr = *pPSTR;
  97.  
  98.     LOGCALL("releaseExternal");
  99.  
  100.     if (pstr) {
  101.         char msg[100];
  102.         wsprintf(msg, "Hook freeing string at %lxh\n", pstr);
  103.         OutputDebugString(msg);
  104.  
  105.         CoTaskMemFree(pstr);
  106.     }
  107. }
  108.  
  109.  
  110.  
  111.  
  112.  
  113. //==========================================================================
  114. // TODO: Remove this copy of DllMain if you are embedding this file in your
  115. //  own DLL.
  116. // DLL Loader entry point.
  117. //==========================================================================
  118. BOOL WINAPI DllMain(HINSTANCE hmod, DWORD dwReason,
  119.                                 PVOID pvReserved)
  120. {
  121.    return TRUE;
  122. }
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.