home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 39 / IOPROG_39.ISO / SOFT / sdkjava40.exe / data1.cab / fg_Samples / Samples / Security / rni / rnisamp.cpp < prev   
Encoding:
C/C++ Source or Header  |  2000-05-04  |  3.2 KB  |  135 lines

  1. // rnisamp.cpp
  2. //
  3. //
  4. // (C) Copyright 1995 - 1999 Microsoft Corporation.  All rights reserved.
  5. //
  6.  
  7. #include <windows.h>
  8. #include <stdarg.h>
  9. #include <native.h>
  10.  
  11. #include "rnisamp.h"
  12.  
  13. typedef struct Hjava_lang_SecurityManager HSecurity;
  14.  
  15.  
  16. __declspec(dllexport)
  17. DWORD __cdecl RNIGetCompatibleVersion()
  18. {
  19.     return RNIVER;
  20. }
  21.  
  22.  
  23. __declspec(dllexport)
  24. struct HArrayOfByte * __cdecl rnisamp_MyRNIObject_RNICheckedLoad (
  25.     struct Hrnisamp_MyRNIObject *_jthis,
  26.     struct Hjava_lang_String *_jfilename
  27.     )
  28. {
  29.     GCFrame gcf;
  30.     struct {
  31.         Hrnisamp_MyRNIObject *jthis;
  32.         HString *jfilename;
  33.     } gc;
  34.     GCFramePush(&gcf, &gc, sizeof(gc));
  35.     gc.jthis = _jthis;
  36.     gc.jfilename = _jfilename;
  37.  
  38.     HSecurity *jsecurity = java_lang_System_getSecurityManager();
  39.     if (jsecurity != NULL)
  40.     {
  41.         jsecurity->checkRead(gc.jfilename);
  42.         if (exceptionOccurred(EE()))
  43.         {
  44.             GCFramePop(&gcf);
  45.             return NULL;
  46.         }
  47.     }
  48.  
  49.     GCFramePop(&gcf);
  50.     return rnisamp_MyRNIObject_JavaCheckedLoad0(gc.jthis, gc.jfilename);
  51. }
  52.  
  53. __declspec(dllexport)
  54. struct HArrayOfByte * __cdecl rnisamp_MyRNIObject_JavaCheckedLoad0 (
  55.     struct Hrnisamp_MyRNIObject *_jthis,
  56.     struct Hjava_lang_String *_jfilename
  57.     )
  58. {
  59.     CHAR filename[MAX_PATH];
  60.  
  61.     if (_jfilename == NULL)
  62.     {
  63.         SignalError(EE(), JAVAPKG "NullPointerException", NULL);
  64.         return NULL;
  65.     }
  66.  
  67.     javaString2CString(_jfilename, filename, sizeof(filename));
  68.     HArrayOfByte *jdata = NULL;
  69.     BOOL outofmemory = FALSE;
  70.  
  71.     GCEnable();
  72.     HANDLE file = CreateFile(
  73.             filename,
  74.             GENERIC_READ,
  75.             FILE_SHARE_READ,
  76.             NULL,
  77.             OPEN_EXISTING,
  78.             FILE_FLAG_SEQUENTIAL_SCAN,
  79.             NULL
  80.             );
  81.     GCDisable();
  82.     if (file != INVALID_HANDLE_VALUE)
  83.     {
  84.         DWORD size = GetFileSize(file, NULL);
  85.         if (size != 0xffffffff)
  86.         {
  87.             jdata = (HArrayOfByte*)ArrayAlloc(T_BYTE, size);
  88.             if (jdata != NULL)
  89.             {
  90.                 HObject **jdatapin = GCNewPinnedHandle((HObject*)jdata);
  91.                 if (jdatapin != NULL)
  92.                 {
  93.                     DWORD read;
  94.                     if (!(ReadFile(
  95.                             file,
  96.                             &jdata->body[0],
  97.                             jdata->length,
  98.                             &read,
  99.                             NULL
  100.                             )
  101.                           && read == size
  102.                           ))
  103.                     {
  104.                         jdata = NULL;
  105.                     }
  106.  
  107.                     GCFreePinnedHandle(jdatapin);
  108.                 }
  109.                 else
  110.                 {
  111.                     outofmemory = TRUE;
  112.                     jdata = NULL;
  113.                 }
  114.             }
  115.             else
  116.             {
  117.                 outofmemory = TRUE;
  118.             }
  119.         }
  120.  
  121.         CloseHandle(file);
  122.     }
  123.  
  124.     if (jdata != NULL)
  125.         return jdata;
  126.  
  127.     if (outofmemory)
  128.         SignalError(EE(), JAVAPKG "OutOfMemoryError", NULL);
  129.     else
  130.         SignalError(EE(), "java/io/IOException", filename);
  131.     return NULL;
  132. }
  133.  
  134.  
  135.