[英]get pointer of byte array in jna
我在c#中有以下代碼,並且在使用JNA的java中需要類似的功能:
IntPtr pImage = SerializeByteArrayToIntPtr(imageData);
public static IntPtr SerializeByteArrayToIntPtr(byte[] arr)
{
IntPtr ptr = IntPtr.Zero;
if (arr != null && arr.Length > 0)
{
ptr = Marshal.AllocHGlobal(arr.Length);
Marshal.Copy(arr, 0, ptr, arr.Length);
}
return ptr;
}
你想使用記憶
這樣使用它:
// allocate sufficient native memory to hold the java array
Pointer ptr = new Memory(arr.length);
// Copy the java array's contents to the native memory
ptr.write(0, arr, 0, arr.length);
請注意,只要將使用內存的本機代碼需要它,您就需要保留對Memory對象的強引用(否則,Memory對象將在收集垃圾時回收本機內存)。
如果您需要更多地控制本機內存的生命周期,那么請在libc中映射malloc()和free(),然后使用它們。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.