java - JNA/DLL 调用后 System.out.println() 停止工作?

标签 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。

感谢您的浏览!如果有任何见解,我将不胜感激!

最佳答案

您可以通过调用轻松重现该内容

System.out.close();
System.err.close();

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

关于java - JNA/DLL 调用后 System.out.println() 停止工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49199661/

相关文章:

java - 避免加载一个传递依赖的模块

java - 为什么 JScrollPane 中的这个列表没有左对齐?

java - hibernate 多对多: A collection with cascade all-delete-orphan

c# - 枚举程序集的所有已安装版本(在 GAC 中)

java - JNA ByteBuffer statvfs

java - 使用扫描仪读取文件时出现InputMismatchException

c++ LoadLibrary() 导致程序退出

c# - 从 C# 类库导出函数

java - JNA 内存泄漏 - 如何修复?

java - JNA 如何将结构从 Java 传递到 C++ 方法?