vxcall/toy-arms

Writing C struct to process memory

Closed this issue · 3 comments

gardc commented

Hi. I'm trying to write a custom C struct to another process' memory, but it seems the write method only writes the size of size_of::<LPCVOID>() as SIZE_T regardless of value T's size.

Write method:

#[cfg(feature = "external")]
    pub fn write<T>(&self, base_address: usize, value: &mut T) -> Result<(), ToyArmsExternalError> {
        unsafe {
            let ok = WriteProcessMemory(self.process_handle, base_address as LPVOID, value as *mut T as LPCVOID, size_of::<LPCVOID>() as SIZE_T, null_mut::<SIZE_T>());
            if ok == FALSE {
                println!("{}", GetLastError());
                return Err(ToyArmsExternalError::WriteProcessMemoryFailed);
            }
        }
        Ok(())
    }

Would this be correct, or am I using the library wrong?
Cheers

Hi! Thank you for the report. Recently I couldn't afford to review and improve my code. Especially, the external feature has been untouched for long time.

it seems the write method only writes the size of size_of::() as SIZE_T regardless of value T's size.

I think you're correct. I'll check correct it today.
Sorry for the inconvenience!

I rectified the write method simply as following, I think that would do. Thanks for telling me that.
I will publish this one in next release on creates.io in a few hours.

pub fn write<T>(&self, base_address: usize, value: &mut T) -> Result<(), ToyArmsExternalError> {
        unsafe {
            let ok = WriteProcessMemory(
                self.process_handle,
                base_address as LPVOID,
                value as *mut T as LPCVOID,
                // -> size_of::<T>()
                size_of::<T>() as SIZE_T,
                null_mut::<SIZE_T>(),
            );
            if ok == FALSE {
                println!("{}", GetLastError());
                return Err(ToyArmsExternalError::WriteProcessMemoryFailed);
            }
        }
        Ok(())
    }

Furthermore, this update will slightly change some other stuffs as well as the write method, such as:

  • the Process struct which used to be called MemoryEx.
  • detect_keydown! macro which used to be the detect_keydown function.

Also I'll elaborate the examples on README.

gardc commented

Wow that was quick, thanks! =D