无效的内存访问
Invalid memory access
我写了一个必须在Java中使用的DLL:
public interface MyInterface extends Library {
public void GetDllVersion(char[] buffer, int bufferLen);
...
}
MyInterface instance=(MyInterface) Native.loadLibrary(basename,MyInterface.class);
char[] buffer=new char[20];
instance.GetDllVersion(buffer,buffer.length);
对应的C代码是
void __declspec(dllexport) GetDllVersion(char *buffer,int bufferLen) {
...
}
我还打开了jna.debug_load
以查看一些输出。lib按预期加载,但对GetDllVersion
的调用因而失败
Exception in thread "main" java.lang.Error: Invalid memory access
at com.sun.jna.Native.invokeVoid(Native Method)
at com.sun.jna.Function.invoke(Function.java:367)
at com.sun.jna.Function.invoke(Function.java:315)
at com.sun.jna.Library$Handler.invoke(Library.java:212)
at com.sun.proxy.$Proxy0.GetDllVersion(Unknown Source)
at Main.main(Main.java:34)
Dependency Walker显示DLL导出此函数。
如何找出函数调用到底出了什么问题?
您的问题是由于Java 16位字符的大小与C 8位字符的大小不匹配。一种解决方案是将数据作为字节[]传递。
public void GetDllVersion(byte[] buffer, int bufferLen);
和
__declspec(dllexport) void GetDllVersion(byte *buffer,int bufferLen) {
另一个解决方案是在本机端使用宽字符。
__declspec(dllexport) void GetDllVersion(wchar_t *buffer,int bufferLen) {
相关文章:
- 特里树.无法访问内存
- 如何使用 C/C++ 访问内存的内容?
- 指针可以用于访问内存中的任何任意区域吗?
- 分段错误:无法访问内存
- SIGABRT 在线程中访问内存时
- 访问内存以内联循环访问数组
- 如何从C或C 语言级别安全访问内存映射的硬件寄存器
- 在释放了所有作用域内指针之后仍然可访问内存
- 有什么快速访问内存的技巧吗
- CUDA非法访问内存
- C++ 访问内存冲突
- 悬空指针仍在访问内存值
- 当我尝试从结构列表中访问内存时出现错误:" Program received signal SIGSEGV, Segmentation fault."
- 如何访问内存映射的 USB 主控制器寄存器
- 访问内存时出现奇怪的崩溃
- 如何在C++读取块后访问内存中的文件数据
- 使用共享库时仍可访问内存
- 从 C++ 结构中包含的指针数组动态分配和访问内存
- Win32应用程序,可无限制地访问内存
- 强制两个线程直接访问内存中的全局变量