Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read a byte from Intptr

Tags:

c#

I have a situation where I have an Intptr, and I have a structure of my own which consists of 4 bytes. I want to read 4 bytes from the Intptr into this structure of mine. But i want to fill the 4 bytes in the structure individually. something like

mystruct obj = new mystruct().

obj.byte2 = "read fist byte from Myintptr"
obj.byte1 = "read next byte from MyIntptr"

is this possible ?

one of the ways could be that i read the whole thing into a byte array first using Marshal.copy(), and then do the second step of copying into obj. But im curious i there is another way

like image 249
mishal153 Avatar asked Jan 24 '26 04:01

mishal153


1 Answers

Are you looking for something like this?

obj.byte1 = System.Runtime.InteropServices.Marshal.ReadByte(ip, 0);
obj.byte2 = System.Runtime.InteropServices.Marshal.ReadByte(ip, 1);
like image 72
BlueMonkMN Avatar answered Jan 25 '26 17:01

BlueMonkMN



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!