|
2 | 2 | #include <Windows.h> |
3 | 3 |
|
4 | 4 | #define JAVA_HOME "D:\\Program Files\\Java\\jre1.8.0_60" |
5 | | - |
6 | 5 | #define JVM_DLL JAVA_HOME "\\bin\\server\\" "jvm.dll" |
7 | | -typedef int (*JNICreateJavaVM)(JavaVM **pvm, void **penv, void *args); |
8 | | -int main() { |
9 | | - JavaVM *pvm = NULL; |
10 | | - JNIEnv *penv = NULL; |
11 | | - JavaVMInitArgs args; |
12 | | - JavaVMOption options[1] = {"-Djava.class.path=.;D:\\Program Files\\Java\\jre1.8.0_60\\lib\\dt.jar;D:\\Program Files\\Java\\jre1.8.0_60\\lib\\tools.jar"}; |
13 | | - args.options = options; |
14 | | - args.nOptions = 1; |
15 | | - args.version = JNI_VERSION_1_6; |
16 | | - args.ignoreUnrecognized = JNI_TRUE; |
17 | | - char *jvmDll = JVM_DLL; |
18 | | - HMODULE hmod = LoadLibraryA("D:\\Program Files\\Java\\jre1.8.0_60\\bin\\server\\jvm.dll"); |
19 | | - JNICreateJavaVM pJNICreateJavaVM = (JNICreateJavaVM)GetProcAddress(hmod, "JNI_CreateJavaVM"); |
20 | | - int status = pJNICreateJavaVM(&pvm, (void**)&penv, (void**)&args); |
21 | | - if (status == JNI_ERR) { |
22 | | - return 0; |
23 | | - } |
24 | | - int num = 2; |
25 | | - jclass intArrCls = penv->FindClass("java/lang/String"); |
26 | | - jobjectArray names = penv->NewObjectArray(num, intArrCls, NULL); |
27 | | - jintArray ages = penv->NewIntArray(num); |
28 | | - jfloatArray heights = penv->NewFloatArray(num); |
29 | 6 |
|
30 | | - for (int idx = 0; idx < num; ++idx) { |
31 | | - const char *pns = "xxx"; |
32 | | - jstring ns = penv->NewStringUTF(pns); |
33 | | - jint intArr[1] = { idx }; |
34 | | - jfloat floatArr[1] = { idx }; |
35 | | - penv->SetIntArrayRegion(ages, idx, 1, intArr); |
36 | | - penv->SetFloatArrayRegion(heights, idx, 1, floatArr); |
37 | | - penv->SetObjectArrayElement(names, idx, ns); |
38 | | - penv->ReleaseStringUTFChars(ns, 0); |
39 | | - } |
| 7 | +#ifdef __cplusplus |
| 8 | +#if __cplusplus |
| 9 | +extern "C" { |
| 10 | +#endif |
| 11 | +#endif |
| 12 | + |
| 13 | + typedef int(*JNICreateJavaVM)(JavaVM **pvm, void **penv, void *args); |
| 14 | + typedef int (*JNIGetCreatedJavaVMs)(JavaVM **vmBuf, jsize bufLen, jsize *nVMs); |
| 15 | + int main() { |
| 16 | + int status = 0; |
| 17 | + JavaVM *pvm = NULL; |
| 18 | + JNIEnv *penv = NULL; |
| 19 | + JavaVMInitArgs args; |
| 20 | + JavaVMOption options[4]; |
| 21 | + options[0].optionString = "-Djava.compiler=NONE"; |
| 22 | + options[1] = { "-Djava.class.path=.;D:\\Program Files\\Java\\jre1.8.0_60\\lib\\dt.jar;D:\\Program Files\\Java\\jre1.8.0_60\\lib\\tools.jar", NULL }; |
| 23 | + options[2].optionString = "-verbose:NONE"; |
| 24 | + options[3].optionString = "-XX:+CreateMinidumpOnCrash"; |
| 25 | + args.options = options; |
| 26 | + args.nOptions = 4; |
| 27 | + args.version = JNI_VERSION_1_8; |
| 28 | + args.ignoreUnrecognized = JNI_TRUE; |
| 29 | + char *jvmDll = JVM_DLL; |
| 30 | + HMODULE hmod = LoadLibraryA("D:\\Program Files\\Java\\jre1.8.0_60\\bin\\server\\jvm.dll"); |
| 31 | + JNICreateJavaVM pJNICreateJavaVM = (JNICreateJavaVM)GetProcAddress(hmod, "JNI_CreateJavaVM"); |
| 32 | + JNIGetCreatedJavaVMs pJNIGetCreateJavaVMs = (JNIGetCreatedJavaVMs)GetProcAddress(hmod, "JNI_GetCreatedJavaVMs"); |
| 33 | + JavaVM *pvms[32] = { 0 }; |
| 34 | + jsize pvmsnum = 0; |
| 35 | + status = pJNIGetCreateJavaVMs(pvms, sizeof(pvms), &pvmsnum); |
| 36 | + status = pJNICreateJavaVM(&pvm, (void**)&penv, (void*)&args); |
| 37 | + if (status == JNI_ERR) { |
| 38 | + return 0; |
| 39 | + } |
| 40 | + int num = 2; |
| 41 | + jclass intArrCls = penv->FindClass("java/lang/String"); |
| 42 | + jobjectArray names = penv->NewObjectArray(num, intArrCls, NULL); |
| 43 | + jintArray ages = penv->NewIntArray(num); |
| 44 | + jfloatArray heights = penv->NewFloatArray(num); |
40 | 45 |
|
41 | | - jobjectArray res = (jobjectArray)Java_com_durongze_jni_CallC_CInterface(penv, NULL, names, ages, heights, num); |
| 46 | + for (int idx = 0; idx < num; ++idx) { |
| 47 | + const char *pns = "xxx"; |
| 48 | + jstring ns = penv->NewStringUTF(pns); |
| 49 | + jint intArr[1] = { idx }; |
| 50 | + jfloat floatArr[1] = { idx }; |
| 51 | + penv->SetIntArrayRegion(ages, idx, 1, intArr); |
| 52 | + penv->SetFloatArrayRegion(heights, idx, 1, floatArr); |
| 53 | + penv->SetObjectArrayElement(names, idx, ns); |
| 54 | + penv->ReleaseStringUTFChars(ns, 0); |
| 55 | + } |
| 56 | + |
| 57 | + jobjectArray res = (jobjectArray)Java_com_durongze_jni_CallC_CInterface(penv, NULL, names, ages, heights, num); |
| 58 | + |
| 59 | + pvm->DestroyJavaVM(); |
| 60 | + } |
42 | 61 |
|
43 | | - pvm->DestroyJavaVM(); |
| 62 | +#ifdef __cplusplus |
| 63 | +#if __cplusplus |
44 | 64 | } |
| 65 | +#endif |
| 66 | +#endif |
0 commit comments