gpt4 book ai didi

java - JNA 和 DLL 的无效内存访问错误

转载 作者:行者123 更新时间:2023-11-30 01:44:17 25 4
gpt4 key购买 nike

我在使用 JNA 和从 LabVIEW 创建的 DLL 文件时遇到问题。当我不使用这一行时,我可以调用它,第一行:

    FileWriter writer = new FileWriter(FirstPath);
BufferedWriter writing = new BufferedWriter(writer);
writing.write("Here goes my strings");
writing.close();

在此之后,DLL 类如下:

   DLLClass dll = (DLLClass)Native.loadLibrary("DLLFile",DLLClass.class);
dll.myMethodInsideDLLClass(FirstPath,SecondPath,ThirdPath);

看起来它正在尝试访问一些随机的 FirstPath 或者我不知道是什么。它给了我这个错误。

   Exception in thread "AWT-EventQueue-0" java.lang.Error: Invalid memory access
at com.sun.jna.Native.getStringBytes(Native Method)
at com.sun.jna.Native.getString(Native.java:2224)
at com.sun.jna.Pointer.getString(Pointer.java:681)
at com.sun.jna.Function.invokeString(Function.java:667)
at com.sun.jna.Function.invoke(Function.java:434)
at com.sun.jna.Function.invoke(Function.java:361)
at com.sun.jna.Library$Handler.invoke(Library.java:265)
at com.sun.proxy.$Proxy0.myMethodInsideDLLClass(Unknown Source)

我的意思是我到底如何访问我试图写入的同一个文件,然后在 dll 方法中再次调用它?我尝试过,但没有任何效果。有人能帮我吗?我将非常感激!

注意:这是我的 DllClass:

  public interface DLLClass extends Library{

public int myMethodInsideDLLClass(String
FirstPath, String SecondPath, String ThirdPath);
}

extends Libray 来自 jna.jar。

这是我的 FileDll.h 文件中的内容:

#ifdef __cplusplus
extern "C" {
#endif


int32_t __cdecl myMethodInsideDLLClass(
char FirstPath[], char SecondPath[],
char ThirdPath[]);


MgErr __cdecl LVDLLStatus(char *errStr, int errStrLen, void *module);

void __cdecl SetExcursionFreeExecutionSetting(Bool32 value);

#ifdef __cplusplus
} // extern "C"
#endif

#pragma pack(pop)

最佳答案

在您尝试过 Daniel 的建议后,如果它不起作用,请尝试以下操作:

替换这一行:

public interface DLLClass extends Library

用这一行:

public interface DLLClass extends com.sun.jna.win32.StdCallLibrary

我能想到的另一个潜在问题是,您可能使用的是较新版本的 java,但您的 JNA 版本是旧的,或者您的库“DLLFile”版本是旧的,并且它不知道java 字符串的内部表示在最近的 java 版本中发生了变化,以创建时使用的任何编码存储字节,而不是始终使用 UTF16。但这确实是在捕获救命稻草。

关于java - JNA 和 DLL 的无效内存访问错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58733268/

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