?
Es geht auch umgekehrt. Ich arbeite mich da gerade ein:
#include "jni.h"
#include <tchar.h>
#include <stdio.h>
#include <windows.h>
#include <shlwapi.h>
#include "main.h"
#include "MpuTools.h"
int main() {
JavaVM *jvm = NULL;
JavaVMInitArgs vm_args;
JavaVMOption options[1];
JNIEnv *env = NULL;
// Optionen und JVM Argumente setzen
options[0].optionString = "-Djava.class.path=..\\JavaDemo";
memset(&vm_args, 0, sizeof(vm_args));
vm_args.version = JNI_VERSION_1_4;
vm_args.options = options;
vm_args.nOptions = 1;
vm_args.ignoreUnrecognized = JNI_FALSE;
// DLL laden
HINSTANCE hLib = LoadLibrary(DLLPATH);
if (hLib == NULL) {
LPTSTR pszBuf;
pszBuf = SysErrorMessage(0, NULL, 0);
_tprintf(_T("%s\n"), pszBuf);
LocalFree(pszBuf);
return -1;
}
else {
_tprintf(_T("DLL %s geladen. (0x%x)\n"), PathFindFileName(DLLPATH), (int)hLib);
}
// Prozedureinsprungspunkt holen
JNI_CREATEJAVAVM jni_createjavavm = (JNI_CREATEJAVAVM) GetProcAddress(hLib, DLL_JNI_CREATE_JVM);
if (jni_createjavavm == NULL) {
LPTSTR pszBuf;
pszBuf = SysErrorMessage(0, NULL, 0);
_tprintf(_T("%s\n"), pszBuf);
LocalFree(pszBuf);
getchar();
return -1;
}
else {
_tprintf(_T("Prozedureinsprungspunkt gefunden. (0x%x)\n\n"), (int)jni_createjavavm);
}
// JVM starten
int status = jni_createjavavm(&jvm, (void **)&env, &vm_args);
if (status != JNI_OK) {
switch (status) {
case JNI_ERR:
printf("Error: %d\nunknown error\n", status);
break;
case JNI_EDETACHED:
printf("Error: %d\nthread detached from the VM\n", status);
break;
case JNI_EVERSION:
printf("Error: %d\nJNI version error\n", status);
break;
case JNI_ENOMEM:
printf("Error %d:\nnot enough memory\n", status);
break;
case JNI_EEXIST:
printf("Error %d:\nVM already created\n", status);
break;
case JNI_EINVAL:
printf("Error %d\ninvalid arguments\n", status);
break;
}
return -1;
}
else {
_tprintf(_T("JVM created successfully\n\n"));
}
// Klasse der aufzurufenden Methode suchen
jclass cls = env->FindClass("HelloWorld");
if (cls == 0) {
_tprintf(_T("Klasse 'HelloWorld' nicht gefunden\n"));
jvm->DestroyJavaVM();
return -1;
}
else {
_tprintf(_T("Klasse 'HelloWorld' gefunden\n"));
}
// Konstruktor aufrufen
jmethodID mid = env->GetMethodID(cls, "<init>", "()V");
if (mid == 0) {
_tprintf(_T("Konstruktor nicht gefunden.\n"));
jvm->DestroyJavaVM();
return -1;
}
else {
_tprintf(_T("Konstruktor gefunden.\n"));
}
// Objektinstanz erzeugen
jobject instance = env->NewObject(cls, mid);
if (instance == 0) {
_tprintf(_T("Instanz konnte nicht erzeugt werden.\n"));
jvm->DestroyJavaVM();
return -1;
}
else {
_tprintf(_T("Instanz erzeugt. (0x%x)\n\n"), (int)instance);
}
// Java Methode IntTest
mid = env->GetMethodID(cls, "intTest", "()I");
if (mid == 0) {
_tprintf(_T("Methode 'iniTest' nicht gefunden.\n"));
jvm->DestroyJavaVM();
return -1;
}
else {
_tprintf(_T("Methode 'iniTest' gefunden.\n"));
}
int res = env->CallIntMethod(instance, mid);
_tprintf(_T("Rückgabewert der Methode 'initTest': %i\n\n"), res);
// Java Methode intTest2
mid = env->GetMethodID(cls, "intTest2", "(I)I");
if (mid == 0) {
_tprintf(_T("Methode 'iniTest2' nicht gefunden.\n"));
jvm->DestroyJavaVM();
return -1;
}
else {
_tprintf(_T("Methode 'iniTest2' gefunden.\n"));
}
res = env->CallIntMethod(instance, mid, 7);
_tprintf(_T("Rückgabewert der Methode 'initTest2': %i\n\n"), res);
// Java Methode strTest
mid = env->GetMethodID(cls, "strTest", "()Ljava/lang/String;");
if (mid == 0) {
_tprintf(_T("Methode 'strTest' nicht gefunden.\n"));
jvm->DestroyJavaVM();
return -1;
}
else {
_tprintf(_T("Methode 'strTest' gefunden.\n"));
}
jstring js = (jstring)env->CallObjectMethod(instance, mid);
const TCHAR* c_str = env->GetStringUTFChars(js, 0);
_tprintf(_T("Rückgabewert der Methode 'strTest': %s\n\n"), c_str);
// Programmende
_tprintf(_T("The end .. to be continued..."));
jvm->DestroyJavaVM();
return 0;
}