| 124 | |
| 125 | |
| 126 | void SetBreakpoint(HANDLE hProcess, LPVOID entryPoint, bool set, BYTE *data) { |
| 127 | DWORD protection; |
| 128 | |
| 129 | // Give ourself write access to the region. |
| 130 | if (VirtualProtectEx(hProcess, entryPoint, 1, PAGE_EXECUTE_READWRITE, &protection)) { |
| 131 | BYTE buffer[1]; |
| 132 | |
| 133 | if (set) { |
| 134 | SIZE_T numBytesRead; |
| 135 | ReadProcessMemory(hProcess, entryPoint, data, 1, &numBytesRead); |
| 136 | |
| 137 | // Write the int 3 instruction. |
| 138 | buffer[0] = 0xCC; |
| 139 | } else { |
| 140 | // Restore the original byte. |
| 141 | buffer[0] = data[0]; |
| 142 | } |
| 143 | |
| 144 | SIZE_T numBytesWritten; |
| 145 | WriteProcessMemory(hProcess, entryPoint, buffer, 1, &numBytesWritten); |
| 146 | |
| 147 | // Restore the original protections. |
| 148 | VirtualProtectEx(hProcess, entryPoint, 1, protection, &protection); |
| 149 | |
| 150 | // Flush the cache so we know that our new code gets executed. |
| 151 | FlushInstructionCache(hProcess, entryPoint, 1); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | enum MessageType { |
| 156 | MessageType_Info = 0, |