0

我使用 JNI 对 Java 类进行 C 调用。其中一个 Java 类加载一个 DLL,并使用 SWIG 生成的代码调用该 DLL。

问题是:当我在 C 中调用调用 DLL 的 Java 类时,我什么也得不到,它不工作。

不幸的是,我看不到背后的错误。

我该如何解决这个问题?谢谢!

这是我使用的代码:

 /*
 * java method that loads the DLL and that is called from C
 * in the function "createInstance"(see below)
*/
public void loadIsystem(){
    System.load("c:\\_MksProj\\TestHarness\\lib\\IConnectJNI.dll");
    //System.loadLibrary(IConnectJNI);
    ConnectionMgr connection = new ConnectionMgr();
}

/*
 * the ConnectionMgr constructor
 * makes a call to the SWIG created wrapper "connectJNI"
 * and constructs the object. The most recently DLL is used.
 * here the call gets broken
 */
  public ConnectionMgr() {
      this(connectJNI.new_ConnectionMgr__SWIG_0(), true);
      //System.out.println("connectJNI.new_ConnectionMgr__SWIG_0()");
  }


  /*C code that calls the Java method(the env var is already instantiated)
  *the call gets broken when the "connection" instance is created:
  *ConnectionMgr connection = new ConnectionMgr();
  *in the loadSystem method
  */
  jobject createInstance(JNIEnv* env, char * className, char * contsructorSignature, ...)
  {
    jclass myclz = (*env)->FindClass(env, className);
    jmethodID cons = (*env)->GetMethodID(env, myclz, "<init>", contsructorSignature);
    jobject scObject = (*env)->NewObject(env, myclz, cons);

    printf("class: %d\n", myclz);
    printf("constructor: %d\n", cons);
    printf("instance: %d\n", scObject);

    jmethodID scMethod1 = (*env)->GetMethodID(env, myclz, "loadIsystem", "()V");
    (*env)->CallVoidMethod(env, scObject, scMethod1);

    return scObject;
  }

可能与这个问题有关: 从 Matlab 调用 Java

4

0 回答 0