2017-09-15 3 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) 

スタックオーバーフローで見つかった回答に記載されているすべてのオプションを試したため、重複した問題が発生しました。 が問題を解決していません。以下のような8へ

+0

は「私が言及されているすべてのオプションを試してみましたか」? –

+1

私には 'IntByReference'のようには見えません。 'Mem'は' size_t'の 'Pointer'と' MemLength'です。 –

+0

@David Heffernan、私は 'public memory MemLength = new Memory(5)以下に変更しました。 public Pointer Mem = MemLength.getPointer(0); '現在、私は例外" main "以下になります。java.lang.IndexOutOfBoundsException:境界が利用可能なスペースを超えています:サイズ= 5、オフセット= 8 \t com.sun.jna .Memory.boundsCheck(Memory.java:220) \t at com.sun.jna.Memory.getPointer(Memory.java:540) 'お願いします。 – Chandrahasan

答えて

-1

変更メモリサイズ: - もの

Public Memory MemLength = new Memory(8);