如何从Java调用Microsoft.Z3.dll中的Z3_get_version()

时间:2011-11-01 00:31:15

标签: java scala dll java-native-interface z3

Microsoft.Z3.dll在文件属性中描述为Z3托管DLL。

Java可以加载dll。它使用System.loadLibrary或System.load来实现,具体取决于程序员的首选项。

如果Java设计者也创建了DLL,则可以使用javah来定义导入/导出声明。不幸的是,那不是我的情况。该DLL已由Microsoft创建为托管C#DLL。

我需要一些帮助来获取C#声明,例如在Microsoft RISE Z3 managed API找到的声明并对Java包/类进行原型设计以使调用成功。 (我确信DLL已加载)。

为方便起见,Microsoft在Microsoft.Z3.h的第03042行定义了特定呼叫。任何示例代码都将非常感谢!

我从服务器获取的错误是:

java.lang.UnsatisfiedLinkError: Microsoft.Z3.GetVersion(
    LMicrosoft/Z3$IntPtr;
    LMicrosoft/Z3$IntPtr;
    LMicrosoft/Z3$IntPtr;
    LMicrosoft/Z3$IntPtr;)V
at Microsoft.Z3.GetVersion(Native Method)
at Microsoft.Z3.z3VersionString(Z3.java:81)
at DatabaseXml.XmlTest(DatabaseXml.java:66)
at DatabaseXml.doGet(DatabaseXml.java:124)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

1 个答案:

答案 0 :(得分:1)

我原本希望DLL的托管版本与Java具有一些本机兼容性。它似乎不是这样。因此,答案是生成一个新的DLL,实现与Sheng Liang's "The Java™ Native Interface:Programmer’s Guide and Specification"中描述的JNI兼容的调用协议。在David Caldwell's website: InOnIt

上找到了一些有用的教程

大多数C源代码都是从Scala ^ Z3中提取的,最终版本是:

#include "stdafx.h"
#include <jni.h>
#include "z3.h"
// #include "z3_api.h" (z3.h automatically includes z3_api.h)
#include "HelloWorld.h"

#ifdef __cplusplus
extern "C" {
#endif
    JNIEXPORT void JNICALL Java_example_jni_HelloWorld_getVersion (
        JNIEnv * env, 
        jclass cls, 
        jobject major, 
        jobject minor, 
        jobject buildNumber, 
        jobject revisionNumber
    ) {

        unsigned int cmaj, cmin, bn, rv;
        jclass ipc;
        jfieldID fid;

        Z3_get_version(&cmaj, &cmin, &bn, &rv);

        ipc = (env)->GetObjectClass(major);
        fid = (env)->GetFieldID(ipc, "value", "I");
        (env)->SetIntField(major, fid, (jint)cmaj);
        ipc = (env)->GetObjectClass(minor);
        fid = (env)->GetFieldID(ipc, "value", "I");
        (env)->SetIntField(minor, fid, (jint)cmin);
        ipc = (env)->GetObjectClass(buildNumber);
        fid = (env)->GetFieldID(ipc, "value", "I");
        (env)->SetIntField(buildNumber, fid, (jint)bn);
        ipc = (env)->GetObjectClass(revisionNumber);
        fid = (env)->GetFieldID(ipc, "value", "I");
        (env)->SetIntField(revisionNumber, fid, (jint)rv);
    }
#ifdef __cplusplus
}
#endif

头文件来自多个源:jni.h及其依赖项随JDK一起提供,并驻留在include和include \ win32目录中。 z3.h和z3_api来自Microsoft RISE Z3,安装在C:\ Program Files(x86)\ Microsoft Research \ Z3-3.2 \ include。 Microsoft RISE还提供z3.lib,它位于C:\ Program Files(x86)\ Microsoft Research \ Z3-3.2 \ bin;您需要将上述代码链接到名为Z3GetVersion_Release.dll的Win32 DLL项目中。

HelloWorld.h由javah生成(有关示例,请参阅InOnIt)。要生成HelloWorld.h,需要在名为HelloWorld.java的文件中使用以下Java类。

package example.jni;

public class HelloWorld {

    private static final String LIB_SEPARATOR = "\\";
    private static final String LIB_NAME = "Z3GetVersion_Release";
    private static final String LIB_EXT = ".dll";

    /** Placeholder class to ease JNI interaction. */
    public static class IntPtr {
        public int value;
    }

    // this is just to force class loading, and therefore library loading.
    public static void init() { }

    static {
        String curDir = System.getProperty("user.dir");
        try {
            System.load(curDir + LIB_SEPARATOR + LIB_NAME + LIB_EXT);
        } catch (UnsatisfiedLinkError e) {
            System.out.println("Library could not be found in directory:" + curDir );
        } catch (SecurityException e) {
            System.out.println("Security permissions prevented loading library from directory:" + curDir );
        }
    }


    /*private static void getVersion(IntPtr major, IntPtr minor, IntPtr buildNumber, IntPtr revisionNumber)
    {
        major.value=0;
        minor.value=0;
        buildNumber.value=0;
        revisionNumber.value=0;
    }*/   
    private static native void getVersion(IntPtr major, IntPtr minor, IntPtr buildNumber, IntPtr revisionNumber);

    public static String z3VersionString() {
        IntPtr major = new IntPtr();
        IntPtr minor = new IntPtr();
        IntPtr buildNumber = new IntPtr();
        IntPtr revisionNumber = new IntPtr();
        getVersion(major, minor, buildNumber, revisionNumber);
        return "Z3 " + major.value + "." + minor.value + " (build " + buildNumber.value + ", rev. " + revisionNumber.value + ")";
    }

    public static void main(String[] args) {
        System.out.println(z3VersionString());
    }
}

示例代码假设您已将所有dll从C:\ Program Files(x86)\ Microsoft Research \ Z3-3.2 \ bin复制到您的工作目录,并且您已将java.exe指向正确的类路径,其中您的已编译的java课程位于。如果一切顺利,运行命令“java example.jni.HelloWorld”将输出响应:Z3 3.2(build 0,rev.0)