加载DLL库错误126 - 尝试创建JVM

时间:2016-05-02 09:16:39

标签: c winapi jvm dll-dependency

我正在尝试使用.exe来运行我的Java应用程序。我有以下代码:

Labyrinth.c

#include <windows.h>
#include <stdio.h>
#include <jni.h>

#define MAIN_CLASS "game/main/Game"

__declspec(dllexport) __stdcall int run(){
    JNIEnv*         env;
    JavaVM*         jvm;
    JavaVMInitArgs  vmargs;
    JavaVMOption    options[1];
    jint            rc;
    jclass          class;
    jmethodID       mainID;

    vmargs.version = 0x00010002;
    options[0].optionString = "-Djava.class.path=.";
    vmargs.options = options;
    vmargs.nOptions = 1;
    rc = JNI_CreateJavaVM(&jvm, (void**) &env, &vmargs);
    if(rc < 0){
        printf("Failed creating JVM");
        return 1;
    }
    class = (*env)->FindClass(env, MAIN_CLASS);
    if(class == 0){
        printf("Failed finding the main class");
        return 1;
    }
    mainID = (*env)->GetStaticMethodID(env, class, "main", "([Ljava/lang/String;)V");
    if(mainID == 0){
        printf("Failed finding the main method");
        return 1;
    }
    (*env)->CallStaticVoidMethod(env, class, mainID, 0);
    (*jvm)->DestroyJavaVM(jvm);
    return 0;
}

然后编译为OpenLabyrinth.dll

我有一个程序试图运行dll

Start.c

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <direct.h>

typedef int (__stdcall* function)();

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow){
    HINSTANCE hGetProcIDDLL = LoadLibrary("OpenLabyrinth.dll");
    if(!hGetProcIDDLL){
        printf("Couldn't find the library: %d", GetLastError());
        return 1;
    }
    function run = (function) GetProcAddress(hGetProcIDDLL, "run");
    if(!run){
        printf("Couldn't find the function: %d", GetLastError());
        return 1;
    }
    run();
    return 0;
}

后来编译成Labyrinth.exe

运行我的应用程序时,我得到LoadLibrary错误代码126.我试图谷歌错误126,发现我的.dll需要依赖。

Process Monitor检查我发现我的程序执行的每个操作都是SUCCESS,但是它返回了代码1。

然而,当我使用Dependency Walker检查时,我显示了很多丢失的文件。它们都是API-MS-WIN-CORE-somethingEXT-MS-WIN-something

导致错误的原因是什么?

1 个答案:

答案 0 :(得分:1)

我只是遇到了同样的问题。 Dependency Walker没有帮助。我在Process Monitor的帮助下解决了该问题,但我不得不将其输出与DLL实际加载正常的情况(在另一台计算机上)进行比较。通过比较LoadImage操作,我可以看到由于缺少依赖项vcruntime140.dll,LoadLibrary失败了。

但是等等!加载jvm.dll后,我遇到另一个问题,试图找到主类。同样的技术使我看到我在失败的系统上缺少了msvcp140.dll。

我添加了vcruntime140.dll和msvcp140.dll,现在一切正常。

对不起,应该提到这是使用OpenJDK 11.0.2。

相关问题