2

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?

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
apilat
  • 1,370
  • 8
  • 17
  • Error code 126 [translates to](https://msdn.microsoft.com/en-us/library/windows/desktop/ms681382.aspx): *"**ERROR_MOD_NOT_FOUND**: The specified module could not be found."* Since there is only one `LoadLibrary`-call in your code, I'd assume it's that. And if your process' exit code is 1 then either that call or the call to `GetProcAddress` failed. You might want to consider using a debugger. – IInspectable May 02 '16 at 09:43
  • 1
    You are missing dependencies. Have you provided the dependencies states by the developer of the library in its documentation? – David Heffernan May 02 '16 at 10:49

1 Answers1

2

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.

theothertom
  • 141
  • 2
  • 3