日期:2014-05-20 浏览次数:20812 次
#include<jni.h> #include<windows.h> #include<string.h> #include<stdio.h> #define MAXDIR 256 #define TRUE 1 #define FALSE 0 int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hprevInst, LPSTR lpCmd, int nShow) { typedef int boolean; JNIEnv *env; JavaVM *jvm; jint res; jclass cls; jmethodID mid; jstring jstr; jclass stringClass; jobjectArray args; boolean first_open = TRUE; char dir[MAXDIR] = ""; char szDrive[_MAX_DRIVE]; char szFolder[_MAX_DIR]; char szFileName[_MAX_FNAME]; char szExt[_MAX_EXT]; SetLastError(NO_ERROR); CreateMutex(NULL, FALSE, "RuleEditorInstance"); if (GetLastError() == ERROR_ALREADY_EXISTS) first_open = FALSE; #ifdef JNI_VERSION_1_6 if (first_open) { JavaVMInitArgs vm_args; JavaVMOption options[2]; char jvm_class_path[1024] = "-Djava.class.path="; char lib_path[256] = "-Djava.library.path=\""; GetModuleFileName(NULL,dir,MAXDIR); _splitpath(dir, szDrive, szFolder, szFileName, szExt); strcpy(dir, szDrive); strcat(dir, szFolder); strcat(jvm_class_path, dir); strcat(jvm_class_path, "bin;"); strcat(jvm_class_path, dir); strcat(jvm_class_path, "lib\\org.eclipse.swt.win32.win32.x86_3.4.0.v3448f.jar;"); options[0].optionString = jvm_class_path; strcat(lib_path, dir); strcat(lib_path, "lib\""); options[1].optionString = lib_path; vm_args.version = 0x00010002; vm_args.options = options; vm_args.nOptions = 2; vm_args.ignoreUnrecognized = JNI_TRUE; /* Create the Java VM */ res = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args); if (res < 0) { fprintf(stderr, "Can't create Java VM\n"); exit(1); } cls = (*env)->FindClass(env, "ch/ruleedit/RuleEditor"); if (cls == NULL) { goto destroy; } mid = (*env)->GetStaticMethodID(env, cls, "main", "([Ljava/lang/String;)V"); if (mid == NULL) { goto destroy; } //解析lpCmd,赋值给jstr jstr = (*env)->NewStringUTF(env, ""); if (jstr == NULL) { goto destroy; } stringClass = (*env)->FindClass(env, "java/lang/String"); args = (*env)->NewObjectArray(env, 1, stringClass, jstr); if (args == NULL) { goto destroy; } (*env)->CallStaticVoidMethod(env, cls, mid, args); } else { //解析lpCmd(是个文件名) //怎么发送消息给已经在运行的进程? //进程怎么接收到消息? //进程在收到消息得到文件名后,就可以调用(*env)->CallStaticVoidMethod();了 } #else fprintf(stderr, "JNI Version Not Supported...\n"); exit(1); #endif /* JNI_VERSION_1_2 */ destroy: if ((*env)->ExceptionOccurred(env)) { (*env)->ExceptionDescribe(env); } (*jvm)->DestroyJavaVM(jvm); return 0; }