2011-08-03 6 views

答えて

4

次のクラスで、そのクラスへの関心の

sun.misc.Unsafe 

方法を見てみましょうは、以下のとおりです。

public native long getAddress(long address); 
public native void putAddress(long address, long value); 
public native long allocateMemory(long size); 
public native long reallocateMemory(long l, long l1); 
public native void setMemory(long l, long l1, byte b); 
public native void copyMemory(long l, long l1, long l2); 

は、ここではその使用例です:

import java.lang.reflect.Field; 
import sun.misc.Unsafe; 
public class Direct { 

     public static void main(String... args) { 
      Unsafe unsafe = null; 

      try { 
       Field field = sun.misc.Unsafe.class.getDeclaredField("theUnsafe"); 
       field.setAccessible(true); 
       unsafe = (sun.misc.Unsafe) field.get(null); 
      } catch (Exception e) { 
       throw new AssertionError(e); 
      } 

      long value = 12345; 
      byte size = 1; 
      long allocateMemory = unsafe.allocateMemory(size); 
      unsafe.putAddress(allocateMemory, value); 
      long readValue = unsafe.getAddress(allocateMemory); 
      System.out.println("read value : " + readValue); 
     } 
    } 
+0

を試してください:struct Test { char string1; // "test" // 4バイト int値1; // 23 // 4バイト char * label; // * label == "str" // 4 bytes };この構造体へのポインタが "p"(com.sun.jna.Pointer)の場合、Marshal.ReadIntPtr(IntPtr)に相当するのはp.getPointer(8)です。 –

関連する問題