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 120 121 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
|
#include <Windows.h>
#include <string>
//#include <jni.h>
#include "../../../native/jni_include/jni.h"
HINSTANCE jvmLibrary;
jint(JNICALL * CreateJVM_Method)(JavaVM **pvm, void **penv, void *args);
#define USE_JNI_VERSION JNI_VERSION_1_4
JavaVM *jvm;
std::string formatMessage(DWORD msgCode)
{
LPVOID lpMsgBuf;
DWORD rc = ::FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
msgCode,
0,
(LPTSTR) & lpMsgBuf,
0, NULL );
if (rc == 0)
{
printf("format message returned 0 (%d)", ::GetLastError());
exit(-1);
}
std::string res((LPTSTR) lpMsgBuf);
::LocalFree(lpMsgBuf);
return res;
}
void loadLibrary(const char* path)
{
jvmLibrary = ::LoadLibrary(path);
if (jvmLibrary == NULL)
{
printf("Failed to load JVM from %s\n", path);
printf("LastError %d\n", GetLastError());
printf("Message: %s\n", formatMessage(GetLastError()).c_str());
exit(-1);
}
}
void unloadLibrary()
{
if (::FreeLibrary(jvmLibrary) == 0)
{
printf(" Failed to unload JVM\n");
printf(" LastError %d\n", GetLastError());
printf(" Message: %s\n", formatMessage(GetLastError()).c_str());
exit(-1);
}
}
void* getSymbol(const char* name)
{
void* res = (void*) ::GetProcAddress(jvmLibrary, name);
if (res == NULL)
{
printf(" Failed to get Symbol %s\n", name);
printf(" LastError %d\n", GetLastError());
printf(" Message: %s\n", formatMessage(GetLastError()).c_str());
exit(-1);
}
return res;
}
void loadEntryPoints()
{
CreateJVM_Method = (jint(JNICALL *)(JavaVM **, void **, void *) )getSymbol("JNI_CreateJavaVM");
printf(" Entry point found %p\n", CreateJVM_Method);
}
int main(int argc, char** argv)
{
if (argc < 2)
{
printf("Usage: %s %%JAVA_HOME%%/bin/server/jvm.dll [JVM_ARGS]", argv[0]);
exit(-1);
}
printf("Check paths\n");
char directory[1024];
int sz = ::GetSystemDirectoryA(directory, 1024);
if (sz == 0)
{
printf("GetSystemDirectory failed\n");
exit(-1);
}
printf(" SystemDirectory: %s\n", std::string(directory, sz).c_str());
sz = ::GetWindowsDirectoryA(directory, 1024);
if (sz == 0)
{
printf("GetWindowsDirectory failed\n");
exit(-1);
}
printf(" WindowsDirectory: %s\n", std::string(directory, sz).c_str());
printf("Load library\n");
loadLibrary(argv[1]);
printf("Load entry points\n");
loadEntryPoints();
// Pack the arguments
printf("Pack JVM arguments\n");
JavaVMInitArgs jniArgs;
jniArgs.options = NULL;
jniArgs.version = USE_JNI_VERSION;
jniArgs.ignoreUnrecognized = false;
jniArgs.nOptions = (jint) argc - 2;
printf(" Num options: %d\n", jniArgs.nOptions);
jniArgs.options = (JavaVMOption*) malloc(sizeof (JavaVMOption) * jniArgs.nOptions);
memset(jniArgs.options, 0, sizeof (JavaVMOption) * jniArgs.nOptions);
for (int i = 0; i < jniArgs.nOptions; i++)
{
printf(" Option %s\n", argv[i + 2]);
jniArgs.options[i].optionString = (char*) argv[i + 2];
}
// Launch the JVM
printf("Create JVM\n");
JNIEnv* env;
jint rc = CreateJVM_Method(&jvm, (void**) &env, (void*) &jniArgs);
free(jniArgs.options);
if (rc != JNI_OK)
{
printf(" Create JVM failed. (%d)\n", rc);
exit(-1);
}
// Create some resources
printf("Create Java resources\n");
jclass cls = env->FindClass("java/lang/Object");
jmethodID mth = env->GetMethodID(cls, "toString", "()Ljava/lang/String;");
mth = env->GetMethodID(cls, "equals", "(Ljava/lang/Object;)Z");
mth = env->GetMethodID(cls, "hashCode", "()I");
mth = env->GetMethodID(cls, "getClass", "()Ljava/lang/Class;");
// Destroy the JVM
printf("Destroy JVM\n");
jvm->DestroyJavaVM();
printf("Unload library\n");
unloadLibrary();
printf("Success\n");
}
|