| 48 | } |
| 49 | |
| 50 | void CtrlRoutine::findAddress() { |
| 51 | if (this->address == NULL) { |
| 52 | /* |
| 53 | Why set a refrence to "this"? Well because it's impossible set a non static class method as ctrl handler of SetConsoleCtrlHandler. |
| 54 | So set a refrence to "this" and then use this refrence "CtrlRoutine::current_routine" for future needs. |
| 55 | */ |
| 56 | CtrlRoutine::current_routine = &*this; |
| 57 | |
| 58 | this->found_address_event = CreateEvent(NULL, true, false, NULL); |
| 59 | |
| 60 | if (this->found_address_event == NULL) { |
| 61 | throw system_error(error_code(GetLastError(), system_category()), "ctrl-routine:findAddress:CreateEvent"); |
| 62 | } |
| 63 | |
| 64 | if (!SetConsoleCtrlHandler((PHANDLER_ROUTINE)CtrlRoutine::customConsoleCtrlHandler, true)) { |
| 65 | this->removeCustomConsoleCtrlHandler(); |
| 66 | this->closeFoundAddressEvent(); |
| 67 | throw system_error(error_code(GetLastError(), system_category()), "ctrl-routine:findAddress:SetConsoleCtrlHandlert"); |
| 68 | } |
| 69 | |
| 70 | if (!GenerateConsoleCtrlEvent(this->event_type, 0)) { |
| 71 | this->removeCustomConsoleCtrlHandler(); |
| 72 | this->closeFoundAddressEvent(); |
| 73 | throw system_error(error_code(GetLastError(), system_category()), "ctrl-routine:findAddress:GenerateConsoleCtrlEvent"); |
| 74 | } |
| 75 | |
| 76 | DWORD dwWaitResult = WaitForSingleObject(this->found_address_event, INFINITE); |
| 77 | if (dwWaitResult == WAIT_FAILED) { |
| 78 | this->removeCustomConsoleCtrlHandler(); |
| 79 | this->closeFoundAddressEvent(); |
| 80 | throw system_error(error_code(GetLastError(), system_category()), "ctrl-routine:findAddress:WaitForSingleObject"); |
| 81 | } |
| 82 | |
| 83 | if (this->address == NULL) { |
| 84 | this->removeCustomConsoleCtrlHandler(); |
| 85 | this->closeFoundAddressEvent(); |
| 86 | throw runtime_error(string("ctrl-routine:findAddress:checkAddressIsNotNull")); |
| 87 | } |
| 88 | |
| 89 | this->removeCustomConsoleCtrlHandler(); |
| 90 | this->closeFoundAddressEvent(); |
| 91 | } |
| 92 | // TODO: remove; |
| 93 | //std::cout << this->event_type << " : " << this->address << "\n"; |
| 94 | } |
| 95 | |
| 96 | void CtrlRoutine::removeCustomConsoleCtrlHandler(void) { |
| 97 | if (!SetConsoleCtrlHandler((PHANDLER_ROUTINE)CtrlRoutine::customConsoleCtrlHandler, false)) { |
no test coverage detected