Memory Editing, Write Float.

DavidTiger

New member
Joined
Jun 23, 2012
Messages
1
Programming Experience
3-5
Hey, Having problems writing a float value to a memory pointer.

Here's what I have to write Int values:

[DllImport("kernel32.dll")]        private static extern bool VirtualProtectEx(IntPtr hProcess, IntPtr lpAddress, UIntPtr dwSize, uint flNewProtect, out uint lpflOldProtect);
        [DllImport("kernel32.dll")]
        private static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, UIntPtr nSize, out IntPtr lpNumberOfBytesWritten);
  
        public class MemoryEditingTools
        {
            public bool errorFlag;
            public byte[] errorMemBytes;
            public IntPtr errorMemPointer = IntPtr.Zero;
            public Label infoDisplay;
            public IntPtr tempProcessHandle = IntPtr.Zero;


            public byte[] ReadMem(IntPtr address, uint size)
            {
                byte[] lpBuffer = new byte[size];
                uint lpNumberOfBytesRead = 0;
                try
                {
                    MainForm.ReadProcessMemory(this.tempProcessHandle, address, lpBuffer, size, ref lpNumberOfBytesRead);
                }
                catch
                {
                    this.errorFlag = true;
                }
                if (lpNumberOfBytesRead != size)
                {
                    this.errorFlag = true;
                    return lpBuffer;
                }
                this.errorFlag = false;
                return lpBuffer;
            }
            
            public bool WriteToMem(IntPtr address, uint size, byte[] value)
            {
                try
                {
                    IntPtr ptr;
                    uint num;
                    MainForm.VirtualProtectEx(this.tempProcessHandle, address, (UIntPtr)4, 0x40, out num);
                    MainForm.WriteProcessMemory(this.tempProcessHandle, address, BitConverter.GetBytes(BitConverter.ToSingle(value, 0)), (UIntPtr)size, out ptr);
                    MainForm.VirtualProtectEx(this.tempProcessHandle, address, (UIntPtr)4, num, out num);
                    this.errorFlag = false;
                }
                catch
                {
                    this.errorFlag = true;
                    this.errorMemBytes = value;
                    this.errorMemPointer = address;
                }
                return this.errorFlag;
            }
        }


Which then is used like so, to write my int value.

this.memTools.WriteToMem(ptr3, 4, BitConverter.GetBytes(1));


How can I manage to write a float / single value to a pointer address?

Cheers, Dave
 

Latest posts

Top Bottom