1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
   | #ifdef __BORLANDC__
  #pragma argsused
#endif
 
#ifdef _WIN32
#define PATH_SEPARATOR ';'
#else
#define PATH_SEPARATOR ':'
#endif
 
#include <jni.h>
#include <windows.h>
 
 
/**
*  You can find all installed JVMs' information in the Registry:
*  HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment
*/
 
 
// typedef of functions needed to create the JVM dynamically
typedef jint (JNICALL GetCreatedJavaVMs_t)( JavaVM**, jsize, jsize* );
typedef jint (JNICALL CreateJavaVM_t)( JavaVM**, void **, void* );
 
// pointer declaration of those functions
GetCreatedJavaVMs_t *GetCreatedJavaVMs;
CreateJavaVM_t *CreateJavaVM;
 
// handling reference to jvm.dll
HINSTANCE hJvmDll;
 
 
int main( int argc, char * argv[] )
{
  jsize  nVMs;
  // pointer to the Virtual Machine instance interface
  JavaVM *jvm = (JavaVM *)0;
  // pointer to the thread instances interface which invokes Native APIs
  JNIEnv *env = (JNIEnv *)0;
 
  JavaVMInitArgs vm_args;
 
  jint status = -1;
  jclass cls;
  jmethodID mid;
  jint square, argsres;
  jboolean bNot;
 
  JavaVMOption options[1];
  options[0].optionString = "-Djava.class.path=.";
//  options[1].optionString = "-verbose";
 
 
  size_t bufLen = 1;
  jvm = (JavaVM *) malloc( sizeof(JavaVM) * bufLen );
 
  char debugStr[256];
 
  hJvmDll =
   LoadLibrary( "C:\\Program Files\\Java\\jre1.5.0_04\\bin\\client\\jvm.dll" );
 
  if ( hJvmDll != NULL )
  {
     GetCreatedJavaVMs =
       (GetCreatedJavaVMs_t*)GetProcAddress( hJvmDll, "JNI_GetCreatedJavaVMs" );
 
     CreateJavaVM =
       (CreateJavaVM_t*)GetProcAddress( hJvmDll, "JNI_CreateJavaVM" );
 
     if ( GetCreatedJavaVMs != NULL && CreateJavaVM != NULL)
        {
          status = GetCreatedJavaVMs( &jvm, bufLen, &nVMs);
 
          if ( status == JNI_OK )
          {
              sprintf( debugStr, "There are %d VM(s) running\r\n", nVMs );
              printf(debugStr);
          }
 
          if (nVMs == 0)
          {
             vm_args.version = JNI_VERSION_1_4;
             vm_args.options = options;
             vm_args.nOptions = 1;
             vm_args.ignoreUnrecognized = JNI_TRUE;
 
             status = CreateJavaVM( &jvm, (void**) &env, &vm_args );
             if ( status == JNI_OK )
             {
                sprintf( debugStr, "Launching VM okay.\r\n" );
                printf(debugStr);
             }
          }
        }
  }
 
 
  if (status == JNI_OK)
  {
      cls = (env)->FindClass("Sample2");
      if(cls !=0)
      {   mid = (env)->GetStaticMethodID( cls, "intMethod", "(I)I");
	        if(mid !=0)
	        {  square = (env)->CallStaticIntMethod(cls, mid, 5);
		       printf("Result of intMethod: %d\n", square);
	        }
 
	        mid = (env)->GetStaticMethodID(cls, "booleanMethod", "(Z)Z");
	        if(mid !=0)
 	        {  bNot = (env)->CallStaticBooleanMethod(cls, mid, 1);
 		       printf("Result of booleanMethod: %d\n", bNot);
 	        }
      }
      (jvm)->DestroyJavaVM();
      return 0;
  }
  else
     return status;
} | 
Partager