在JNA / DLL调用后,System.out.println()是否停止工作?

时间:2018-03-09 18:04:54

标签: java dll jna system.out

我的程序中有多个System.out.println()调用。我的问题是,在通过JNA库调用DLL之后(它可以在不返回错误代码或抛出异常的情况下工作),后续调用println()而不执行任何打印!我知道这些语句正在执行,因为我在NetBeans中逐步执行它们。

不幸的是,我对DLL背后的C代码一无所知,我猜你无法复制这个,除非你注册qimaging.com并下载QCam SDK。我只是想知道是否有人遇到过与此System.out.println()类似的行为,即它直到某个点都有效,然后即使它执行也会停止打印。

这是我的主要测试类:

package hwi.scope;

import com.sun.jna.ptr.IntByReference;
import hwi.scope.qcam.QCamDriverX64;
import java.io.File;

/**
* QCamTest class tests some functions of the QCam driver library.
* @author rnagel
*/
public class QCamTest
{
    private static QCamDriverX64 driver;

    // Main test method:
    public static void main() throws Exception
    {
        // Set path to easily find DLL in the /dll folder:
        File f = new File("dll");        
        System.setProperty("jna.library.path", f.getCanonicalPath());        

        // Use JNA to load the driver library:
        driver = QCamDriverX64.INSTANCE;

        // Load camera driver from the library:
        loadQCamDriver();

        // Print out the driver version:
        printQCamVersion();
    }

    // Load camera driver method:
    public static void loadQCamDriver()
    {
        System.out.println("Loading QCam driver..."); // Executes and prints to console
        int error = driver.QCam_LoadDriver();        
        System.out.println("Done loading driver."); // Executes, but doesn't print to console
    }

    // Print camera driver version:
    public static void printQCamVersion()
    {
        // Obtain driver version as a combination of 'major' and 'minor' increments:
        IntByReference major = new IntByReference(), minor = new IntByReference();
        int error = driver.QCam_Version(major, minor);

        // At this point, I've verified that I have a obtained a valid version.
        System.out.println("QCam v." + major.getValue() + "." + minor.getValue()); // Executes, but doesn't print to console   
    }
}

这是我用来封装DLL的QCamDriverX64类:

package hwi.scope.qcam;

import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.ptr.IntByReference;

/**
 * QCamDriverX64 wraps the 64-bit version of the QCam driver DLL.
 * @author rnagel
 */
public interface QCamDriverX64 extends Library {

    // Make the library name publicly accessible:
    public static final String DLL_NAME = "QCamDriverx64";

    // Load an instance of the library using JNA:
    public static final QCamDriverX64 INSTANCE = (QCamDriverX64) Native.loadLibrary(DLL_NAME, QCamDriverX64.class);

    // Load the QCam driver:
    int QCam_LoadDriver();

    // Obtain QCam driver version # (in major and minor increments)
    int QCam_Version (IntByReference major, IntByReference minor);
}

我使用的是NetBeans 8.2和JDK 1.8.0_121。

谢谢你看看!我很欣赏任何见解!

1 个答案:

答案 0 :(得分:2)

您可以通过调用

轻松复制该内容
System.out.close();
System.err.close();

我假设DLL中的本机代码在这方面做了一些事情。通过实际进行上述调用可能会很友好。在这种情况下,您可以将System.outSystem.err保存到变量中,使用System.setOut()System.setErr()设置一些虚拟流,并将所有内容恢复为DLL调用之前的状态。如果本机代码关闭了底层文件句柄,那将无法提供帮助,唯一的选择是在DLL的提供程序中提交错误报告。