apilat
apilat

Reputation: 1510

Load DLL Library Error 126 - Trying to create a JVM

I am trying to make an .exe for running my Java application. I have the following code:

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;
}

which is then compiled to OpenLabyrinth.dll

And I have a program trying to to run the 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;
}

later compiled into Labyrinth.exe

When running my application i get the LoadLibrary error code 126. I tried to google error 126 and found out that my .dll needs dependencies.

Checking it with Process Monitor I found out that every operation performed by my program was SUCCESS, but it returned with code 1.

However when I checked it using Dependency Walker i showed a lot of missing files. They were all either API-MS-WIN-CORE-something or EXT-MS-WIN-something.

What should be causing the error?

Upvotes: 2

Views: 2497

Answers (1)

theothertom
theothertom

Reputation: 191

I just ran into the same problem. Dependency Walker didn't help. I solved the problem with the help of Process Monitor, but I had to compare it's output to a case (on a different machine) where the DLL actually loaded OK. From comparing LoadImage operations, I could see that LoadLibrary was failing because of a missing dependency vcruntime140.dll.

But wait there's more! After I got jvm.dll loaded, I ran into another problem trying to find the main class. Same technique led me to see I was missing msvcp140.dll on the system that failed.

I added vcruntime140.dll and msvcp140.dll and now all is well.

Sorry, should have mentioned this was using OpenJDK 11.0.2.

Upvotes: 2

Related Questions