gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 02:14:00 29 4
gpt4 key购买 nike

我的程序中有多个 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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com