Debian Codegraph @

project/debug/windows/testJVM.cpp

raw symbols SCIP
1 #include <Windows.h>
2 #include <string>
3 //#include <jni.h>
4 #include "../../../native/jni_include/jni.h"
5
6 HINSTANCE jvmLibrary;
7 jint(JNICALL * CreateJVM_Method)(JavaVM **pvm, void **penv, void *args);
8 #define USE_JNI_VERSION JNI_VERSION_1_4
9 JavaVM *jvm;
10
11 std::string formatMessage(DWORD msgCode)
12 {
13 LPVOID lpMsgBuf;
14 DWORD rc = ::FormatMessage(
15 FORMAT_MESSAGE_ALLOCATE_BUFFER |
16 FORMAT_MESSAGE_FROM_SYSTEM |
17 FORMAT_MESSAGE_IGNORE_INSERTS,
18 NULL,
19 msgCode,
20 0,
21 (LPTSTR) & lpMsgBuf,
22 0, NULL );
23 if (rc == 0)
24 {
25 printf("format message returned 0 (%d)", ::GetLastError());
26 exit(-1);
27 }
28
29 std::string res((LPTSTR) lpMsgBuf);
30 ::LocalFree(lpMsgBuf);
31 return res;
32 }
33
34 void loadLibrary(const char* path)
35 {
36 jvmLibrary = ::LoadLibrary(path);
37 if (jvmLibrary == NULL)
38 {
39 printf("Failed to load JVM from %s\n", path);
40 printf("LastError %d\n", GetLastError());
41 printf("Message: %s\n", formatMessage(GetLastError()).c_str());
42 exit(-1);
43 }
44 }
45
46 void unloadLibrary()
47 {
48 if (::FreeLibrary(jvmLibrary) == 0)
49 {
50 printf(" Failed to unload JVM\n");
51 printf(" LastError %d\n", GetLastError());
52 printf(" Message: %s\n", formatMessage(GetLastError()).c_str());
53 exit(-1);
54 }
55 }
56
57 void* getSymbol(const char* name)
58 {
59 void* res = (void*) ::GetProcAddress(jvmLibrary, name);
60 if (res == NULL)
61 {
62 printf(" Failed to get Symbol %s\n", name);
63 printf(" LastError %d\n", GetLastError());
64 printf(" Message: %s\n", formatMessage(GetLastError()).c_str());
65 exit(-1);
66 }
67 return res;
68 }
69
70 void loadEntryPoints()
71 {
72 CreateJVM_Method = (jint(JNICALL *)(JavaVM **, void **, void *) )getSymbol("JNI_CreateJavaVM");
73 printf(" Entry point found %p\n", CreateJVM_Method);
74 }
75
76 int main(int argc, char** argv)
77 {
78 if (argc < 2)
79 {
80 printf("Usage: %s %%JAVA_HOME%%/bin/server/jvm.dll [JVM_ARGS]", argv[0]);
81 exit(-1);
82 }
83
84 printf("Check paths\n");
85 char directory[1024];
86 int sz = ::GetSystemDirectoryA(directory, 1024);
87 if (sz == 0)
88 {
89 printf("GetSystemDirectory failed\n");
90 exit(-1);
91 }
92 printf(" SystemDirectory: %s\n", std::string(directory, sz).c_str());
93 sz = ::GetWindowsDirectoryA(directory, 1024);
94 if (sz == 0)
95 {
96 printf("GetWindowsDirectory failed\n");
97 exit(-1);
98 }
99 printf(" WindowsDirectory: %s\n", std::string(directory, sz).c_str());
100
101 printf("Load library\n");
102 loadLibrary(argv[1]);
103
104 printf("Load entry points\n");
105 loadEntryPoints();
106
107 // Pack the arguments
108 printf("Pack JVM arguments\n");
109 JavaVMInitArgs jniArgs;
110 jniArgs.options = NULL;
111 jniArgs.version = USE_JNI_VERSION;
112 jniArgs.ignoreUnrecognized = false;
113 jniArgs.nOptions = (jint) argc - 2;
114 printf(" Num options: %d\n", jniArgs.nOptions);
115 jniArgs.options = (JavaVMOption*) malloc(sizeof (JavaVMOption) * jniArgs.nOptions);
116 memset(jniArgs.options, 0, sizeof (JavaVMOption) * jniArgs.nOptions);
117 for (int i = 0; i < jniArgs.nOptions; i++)
118 {
119 printf(" Option %s\n", argv[i + 2]);
120 jniArgs.options[i].optionString = (char*) argv[i + 2];
121 }
122
123 // Launch the JVM
124 printf("Create JVM\n");
125 JNIEnv* env;
126 jint rc = CreateJVM_Method(&jvm, (void**) &env, (void*) &jniArgs);
127 free(jniArgs.options);
128 if (rc != JNI_OK)
129 {
130 printf(" Create JVM failed. (%d)\n", rc);
131 exit(-1);
132 }
133
134 // Create some resources
135 printf("Create Java resources\n");
136 jclass cls = env->FindClass("java/lang/Object");
137 jmethodID mth = env->GetMethodID(cls, "toString", "()Ljava/lang/String;");
138 mth = env->GetMethodID(cls, "equals", "(Ljava/lang/Object;)Z");
139 mth = env->GetMethodID(cls, "hashCode", "()I");
140 mth = env->GetMethodID(cls, "getClass", "()Ljava/lang/Class;");
141
142
143 // Destroy the JVM
144 printf("Destroy JVM\n");
145 jvm->DestroyJavaVM();
146
147 printf("Unload library\n");
148 unloadLibrary();
149 printf("Success\n");
150 }