Commits any local module changes back to the target process.
| 6 | // Commits any local module changes back to the target process. |
| 7 | // |
| 8 | bool module_view::commit() const |
| 9 | { |
| 10 | bool result = false; |
| 11 | |
| 12 | // Try to open the process. |
| 13 | // |
| 14 | HANDLE process_handle = OpenProcess( PROCESS_VM_WRITE | PROCESS_VM_OPERATION, FALSE, process_id ); |
| 15 | if ( !process_handle ) |
| 16 | return false; |
| 17 | |
| 18 | // Get RWX permissions. |
| 19 | // |
| 20 | DWORD new_protect = PAGE_EXECUTE_READWRITE; |
| 21 | DWORD old_protect; |
| 22 | if ( !VirtualProtectEx( process_handle, ( LPVOID )module_base, module_size, new_protect, &old_protect ) ) |
| 23 | goto cleanup; |
| 24 | |
| 25 | // Write the memory. |
| 26 | // |
| 27 | SIZE_T num_written; |
| 28 | if ( WriteProcessMemory( process_handle, ( LPVOID )module_base, local_module.cdata(), local_module.size(), &num_written ) && num_written == module_size ) |
| 29 | result = true; |
| 30 | |
| 31 | // Restore old memory permissions. |
| 32 | // |
| 33 | if ( !VirtualProtectEx( process_handle, ( LPVOID )module_base, module_size, old_protect, &new_protect ) ) |
| 34 | result = false; |
| 35 | |
| 36 | // On function exit, close the handle. |
| 37 | // |
| 38 | cleanup: |
| 39 | CloseHandle( process_handle ); |
| 40 | return result; |
| 41 | } |
| 42 | |
| 43 | // Fetches any remote module changes back to the local module buffer. |
| 44 | // |