简体   繁体   English

JNA:无效的内存访问

[英]JNA:Invalid memory access

I am attempting to wrap a C# dll (libxert.dll) inside Java using jna-4.4.0. 我正在尝试使用jna-4.4.0在Java中包装C#dll(libxert.dll)。 I'm get an exception when I call the method "xert_alloc". 调用方法“ xert_alloc”时出现异常。 Please help. 请帮忙。

C# C#

namespace XertLibCsharp
{
    [StructLayout(LayoutKind.Sequential)]
    public struct XertHandle {
        IntPtr Mem;
        IntPtr MemLength;
    }
}

[DllImport("libxert", EntryPoint = "xert_alloc")]
public static extern XertHandle Allocate();

JAVA 爪哇

import com.sun.jna.Structure;
import com.sun.jna.ptr.IntByReference;

public class XertHandle extends Structure {
    public IntByReference Mem;
    public IntByReference MemLength; 

    @Override
    protected List<String> getFieldOrder() {
        return Arrays.asList("Mem", "MemLength");
    } 
}

public interface libxert extends Library { 
    libxert INSTANCE = (libxert) Native.loadLibrary("libxert", libxert.class); 
    XertHandle xert_alloc();
}

public static void main(String args[]) {
        libxert jnaLib = libxert.INSTANCE; 
        jnaLib.xert_alloc(); 
}

The Exception 例外

Exception in thread "main" java.lang.Error: Invalid memory access
    at com.sun.jna.Native.invokePointer(Native Method)
    at com.sun.jna.Function.invokePointer(Function.java:490)
    at com.sun.jna.Function.invoke(Function.java:443)
    at com.sun.jna.Function.invoke(Function.java:354)
    at com.sun.jna.Library$Handler.invoke(Library.java:244)

I believe that this is not a duplicate issue because I tried all the options that are mentioned in the answers I found on stack overflow, but none solve my issue. 我相信这不是重复的问题,因为我尝试了在堆栈溢出时找到的答案中提到的所有选项,但是没有一个可以解决我的问题。

将内存大小更改为8,例如:

Public Memory MemLength = new Memory(8);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM