我有以下问题。我写了一个Application,它创建了一个Java虚拟机。如果我在Visual Studio中启动此程序,则可以正常工作。但是,如果我在Visual Studio之外启动它,则该程序将无法正常工作,并且我将导致ntdll.dll崩溃。

这是我的代码:

    int result = 0;

LoadRuntimeLibrary(libPath);



// Load the JVM library
g_jniLibrary = LoadLibrary(libPath);

if (g_jniLibrary == NULL) {

    info->Error("Could not load libary: ");
    return -1;
}

// Grab the create VM function address
JNI_createJavaVM createJavaVM = (JNI_createJavaVM)GetProcAddress(g_jniLibrary, "JNI_CreateJavaVM");
if (createJavaVM == NULL) {
    info->Error("ERROR: Could not find JNI_CreateJavaVM function");
    return -1;
}

// Count the vm args
int numVMArgs = -1;
while (vmArgs[++numVMArgs] != NULL) {}

// Add the options for exit and abort hooks
int numHooks = 0;

JavaVMOption* options = (JavaVMOption*)malloc((numVMArgs + numHooks) * sizeof(JavaVMOption));

for (int i = 0; i < numVMArgs; i++){
    options[i].optionString = vmArgs[i];
    options[i].extraInfo = 0;
}


// Setup hook pointers

options[numVMArgs].optionString = "abort";
options[numVMArgs].extraInfo = (void*)&AbortHook;
options[numVMArgs + 1].optionString = "exit";
options[numVMArgs + 1].extraInfo = (void*)&ExitHook;

JavaVMInitArgs init_args;
memset(&init_args, 0, sizeof(init_args));

init_args.version = JNI_VERSION_1_8;
init_args.options = options;
init_args.nOptions = numVMArgs + numHooks;
init_args.ignoreUnrecognized = JNI_FALSE;



result = createJavaVM(&jvm, &env, &init_args); // here is the crash





 env = GetJNIEnv(false);


Init(env);



result = RunMainClass(env, mainCls, argc, javaargs);

jvm->DestroyJavaVM();
FreeLibrary(g_jniLibrary);

return result;

希望您有任何想法,可能出什么问题了

最佳答案

您正在越界访问options数组。它仅包含numVMArgs元素,因为numHooks为零。

当您这样做时,这当然会导致不确定的行为

options[numVMArgs].optionString = "abort";
options[numVMArgs].extraInfo = (void*)&AbortHook;
options[numVMArgs + 1].optionString = "exit";
options[numVMArgs + 1].extraInfo = (void*)&ExitHook;

因为索引numVMArgsnumVMArgs + 1超出范围。

07-28 09:45