gpt4 book ai didi

c# - 什么等同于 Java 中的 Marshal.ReadIntPtr(IntPtr) (C#)?

转载 作者:行者123 更新时间:2023-11-30 07:31:57 26 4
gpt4 key购买 nike

什么等同于 Java 中的 Marshal.ReadIntPtr(IntPtr) (C#)?

最佳答案

看看下面的类

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

关于c# - 什么等同于 Java 中的 Marshal.ReadIntPtr(IntPtr) (C#)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6929833/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com