2017-06-21 1 views
0

J'ai un .dll généré par Jet excelsior que j'essaye de tirer des classes de leur dll d'invocation générée. Je suivais leur script d'exemple à l'origine fait dans c. Malgré des heures de recherches et de dépannage, je n'arrive pas à faire fonctionner l'appel LoadLibrary. Je cours la communauté visual studio 2017 en faisant un projet C++ vide. Voici mon appel et le peu d'informations de débogage que j'ai pu obtenir. Toutes les solutions ou des conseils de débogage est le bienvenu que je suis en train de devenir désespérée, aussi des conseils sur mon c l'utilisation de ++ est la bienvenue car je suis encore novice:C++ LoadLibrary() provoque la fermeture du programme

1. HINSTANCE test = LoadLibrary(L"C:\\Full Path\\myDll.dll"); 
2. //Code exits before reaching this point. 
3. int er = (int)GetLastError(); 

Oui, j'ai essayé de déplacer mon dll à mon répertoire de projet et juste appeler le nom du fichier. Malheureusement, je ne peux pas voir ce que le getlasterror retourne parce que le code se bloque. Lors du débogage, avant que la ligne 1 ne soit exécutée. Son information étendue est:
Name: test Value: 0xcccccccc{unused=???} unused <Unable to read memory>
Je suppose que ce n'est pas pertinent car il n'a pas été réellement exécuté?
Voici le code complet si elle se trouve être pertinent (Il échoue au fond Au début de main():

#include "stdafx.h" 
#include <string> 
#include <windows.h> 
#include <iostream> 
#include <C:\Program Files (x86)\Java\jdk1.8.0_131\include\jni.h> 

HINSTANCE loadDll(LPCWSTR name) 
{ 
    HINSTANCE hDll = LoadLibrary(name); 
    int thing = (int)GetLastError(); 
    if (!hDll) { 
     thing = (int)GetLastError(); 
     printf("Unable to load %s\n", name); 
     exit(1); 
    } 

    printf("%s loaded\n", name); 

    return hDll; 
} 

typedef jint(JNICALL * JNI_GetDefaultJavaVMInitArgs_func) (void *args); 
typedef jint(JNICALL * JNI_CreateJavaVM_func) (JavaVM **pvm, void **penv, void *args); 

/* 
* Initialize JET run-time. 
*/ 
void initJavaRT(HINSTANCE myDllHandle, JavaVM** pjvm, JNIEnv** penv) 
{ 
    JavaVMInitArgs args; 
    JNI_GetDefaultJavaVMInitArgs_func JNI_Init_Args = (jint(JNICALL *) (void *args)) GetProcAddress(myDllHandle, "JNI_GetDefaultJavaVMInitArgs"); 
    JNI_CreateJavaVM_func JNI_Create_VM = (jint(JNICALL *) (JavaVM **pvm, void **penv, void *args)) GetProcAddress(myDllHandle, "JNI_CreateJavaVM"); 

    if (!JNI_Init_Args) { 
     std::cerr << "!JNI_Init_Args\n"; 
     printf("%s doesn't contain public JNI_GetDefaultJavaVMInitArgs\n", dllName); 
     exit(1); 
    } 

    if (!JNI_Create_VM) { 
     printf("%s doesn't contain public JNI_CreateJavaVM\n", dllName); 
     exit(1); 
    } 

    memset(&args, 0, sizeof(args)); 

    /*args.version = JNI_VERSION_1_2; 
    if (JNI_GetDefaultJavaVMInitArgs_func(&args) != JNI_OK) { 
     printf("JNI_GetDefaultJavaVMInitArgs() failed with result %d\n", JNI_GetDefaultJavaVMInitArgs_func(&args)); 
     exit(1); 
    }*/ 

    /* 
    * NOTE: no JVM is actually created 
    * this call to JNI_CreateJavaVM is intended for JET RT initialization 
    */ 
    /*if (JNI_CreateJavaVM_func(pjvm, (void **)penv, &args) != JNI_OK) { 
     printf("JNI_CreateJavaVM() failed with result %d\n", JNI_CreateJavaVM_func(pjvm, (void **)penv, &args)); 
     exit(1); 
    }*/ 

    printf("JET RT initialized\n"); 
    fflush(stdout); 
} 


/* 
* Look for class. 
*/ 
jclass lookForClass(JNIEnv* env, char* name) 
{ 
    jclass clazz = env->FindClass(name); 

    if (!clazz) { 
     printf("Unable to find class %s\n", name); 
     exit(1); 
    } 

    printf("Class %s found\n", name); 
    fflush(stdout); 

    return clazz; 
} 


/* 
* Create an object and invoke the "ifoo" instance method 
*/ 
void invokeInstanceMethod(JNIEnv* env, jclass myClassInDll) 
{ 
    jmethodID MID_init, MID_ifoo; 
    jobject obj; 

    MID_init = env->GetMethodID(myClassInDll, "<init>", "()V"); 
    if (!MID_init) { 
     printf("Error: MyClassInDll.<init>() not found\n"); 
     return; 
    } 

    obj = env->NewObject(myClassInDll, MID_init); 
    if (!obj) { 
     printf("Error: failed to allocate an object\n"); 
     return; 
    } 

    MID_ifoo = env->GetMethodID(myClassInDll, "ifoo", "()V"); 

    if (!MID_ifoo) { 
     printf("Error: MyClassInDll.ifoo() not found\n"); 
     return; 
    } 

    env->CallVoidMethod(obj, MID_ifoo); 
} 



/* 
* Invoke the "foo" static method 
*/ 
void invokeStaticMethod(JNIEnv* env, jclass myClassInDll) 
{ 
    jmethodID MID_foo; 

    MID_foo = env->GetStaticMethodID(myClassInDll, "parse", "()V"); 
    if (!MID_foo) { 
     printf("\nError: MyClassInDll.foo() not found\n"); 
     return; 
    } 

    env->CallStaticVoidMethod(myClassInDll, MID_foo); 
} 


void finalizeJavaRT(JavaVM* jvm) 
{ 
    jvm->DestroyJavaVM(); 
} 


int main() 
{ 
    //Actually just trouble shooting code, not used in full program 
    HINSTANCE test = LoadLibrary(L"C:\\Full Path\\stgNativeProject.dll"); 
    int er = (int)GetLastError(); 

    //Actual program code 
    HINSTANCE myDllHandle; 
    JNIEnv *env; 
    JavaVM *jvm; 
    jclass myClassInDll; 

    /* 
    * First of all, load required component. 
    * By the time of JET initialization, all components should be loaded. 
    */ 
    std::cerr << dllName << "\n"; 
    myDllHandle = loadDll(dllName); 

    /* 
    * Initialize JET run-time. 
    * The handle of loaded component is used to retrieve Invocation API. 
    */ 
    initJavaRT(myDllHandle, &jvm, &env); 

    /* 
    * Look for class. 
    */ 
    myClassInDll = lookForClass(env, "MyClassInDll"); 

    /* 
    * Create an object and invoke instance method. 
    */ 
    invokeInstanceMethod(env, myClassInDll); 

    /* 
    * Invoke static method. 
    */ 
    invokeStaticMethod(env, myClassInDll); 

    /* 
    * Finalize JET run-time. 
    */ 
    finalizeJavaRT(jvm); 

    return 0; 
} 
+0

Si vous allez à downvote si rapidement, au moins me donner un moyen d'améliorer p bail et merci. –

+0

0xcccccccc = mémoire de pile non initialisée: https://stackoverflow.com/a/127404/487892 – drescherjm

+0

LoadLibrary n'est-elle pas censée prendre en charge cette fonction lorsqu'elle est appelée? Figuré c'était juste parce qu'il n'avait pas encore été exécuté. –

Répondre

0

Je teste un utilitaire appelé Jet La solution à mon problème est relativement simple. Excelsior pour précompiler mon code java pour un usage natif Cet utilitaire crée un fichier .dll à utiliser dans les projets C++ ou C. Le problème que je rencontrais est qu'il est nécessaire que la DLL d'invocation soit compressée avec Jet Pack avec le moteur d'exécution Jet. Vérifiez les docs dessus here. L'appel d'échantillon Jet dlls/cmain sera le plus utile pour vous si vous avez téléchargé Jet