对于F#程序员来说,修改特定内存地址空间的值并不是一件很常见的事,和C#一样,F#代码由F#编译器编译之后生产托管模块,但是F#中并没有提供C#中的unsafe功能,那么如何在F#中修改给定内存地址空间的值呢,下面的这些代码就可实现此要求:
//Memory Address,you may get this address from some other operations //not sure if this is readable safe address let p = 0x002100000n //Pointer to the Address let address : nativeptr<int32> = Microsoft.FSharp.NativeInterop.NativePtr.ofNativeInt(p) //set the value which is stored in the memory with given address and offset to 0xA let setV = Microsoft.FSharp.NativeInterop.NativePtr.set(address) 0 0xA //get the changed value let result = Microsoft.FSharp.NativeInterop.NativePtr.read(address)
这里的p为一nativeint类型数据,当然,在实际程序中我们不会明显的给出一个地址,也不方便给出,这里只是做个例子:)
主要用到的function都是Microsoft.FSharp.NativeInterop.NativePtr命名空间的。有想法的话,可以尝试一下~:)