2017-09-15 112 views
0

我正在尝试使用jna-4.4.0在Java内部包装C#dll(libxert.dll)。 我在调用方法“xert_alloc”时遇到异常。 请帮忙。JNA:无效的内存访问

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(); 
} 

例外

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) 

我认为,这不是一个重复的问题,因为我尝试了我在堆栈溢出中找到的答案中提到的所有选项, 但没有解决我的问题。

+0

“我想这是中提到的所有选项” - 哪些? –

+1

看起来不像'IntByReference'。 'Mem'是'指针'和'MemLength','size_t'。 –

+0

@David Heffernan,我已经改变到下面的公共内存MemLength =新内存(5); public Pointer Mem = MemLength.getPointer(0);'现在我得到异常''main“java.lang.IndexOutOfBoundsException:界限超出可用空间:size = 5,offset = 8 \t at com.sun.jna .Memory.boundsCheck(Memory.java:220) \t at com.sun.jna.Memory.getPointer(Memory.java:540)'请指教 – Chandrahasan

回答

-1

更改内存大小设置为8,如:

Public Memory MemLength = new Memory(8);