| 13 | namespace OpenKneeboard::WindowCaptureControl { |
| 14 | |
| 15 | Handles InstallHooks(HWND hwnd) { |
| 16 | DWORD processID {}; |
| 17 | Handles ret {}; |
| 18 | |
| 19 | auto threadID = GetWindowThreadProcessId(hwnd, &processID); |
| 20 | if (processID == GetCurrentProcessId()) { |
| 21 | dprint("Cowardly refusing to move my own mouse cursor."); |
| 22 | return {}; |
| 23 | } |
| 24 | if (!threadID) { |
| 25 | dprint("Failed to find thread for window."); |
| 26 | return {}; |
| 27 | } |
| 28 | |
| 29 | const auto hookPath = (RuntimeFiles::GetInstallationDirectory() |
| 30 | / RuntimeFiles::WINDOW_CAPTURE_HOOK_DLL) |
| 31 | .wstring(); |
| 32 | |
| 33 | dprint(L"Loading hook library: {}", hookPath); |
| 34 | |
| 35 | ret.mLibrary.reset(LoadLibraryW(hookPath.c_str())); |
| 36 | if (!ret.mLibrary) { |
| 37 | dprint("Failed to load hook library: {}", GetLastError()); |
| 38 | } |
| 39 | |
| 40 | auto msgProc |
| 41 | = GetProcAddress(ret.mLibrary.get(), "GetMsgProc_WindowCaptureHook"); |
| 42 | if (!msgProc) { |
| 43 | dprint("Failed to find msgProc hook address: {}", GetLastError()); |
| 44 | } |
| 45 | auto wndProc |
| 46 | = GetProcAddress(ret.mLibrary.get(), "CallWndProc_WindowCaptureHook"); |
| 47 | if (!msgProc) { |
| 48 | dprint("Failed to find wndProc hook address: {}", GetLastError()); |
| 49 | } |
| 50 | |
| 51 | ret.mMessageHook.reset(SetWindowsHookExW( |
| 52 | WH_GETMESSAGE, |
| 53 | reinterpret_cast<HOOKPROC>(msgProc), |
| 54 | ret.mLibrary.get(), |
| 55 | threadID)); |
| 56 | if (!ret.mMessageHook) { |
| 57 | dprint("Failed to set WH_GETMESSAGE: {}", GetLastError()); |
| 58 | } |
| 59 | ret.mWindowProcHook.reset(SetWindowsHookExW( |
| 60 | WH_CALLWNDPROC, |
| 61 | reinterpret_cast<HOOKPROC>(wndProc), |
| 62 | ret.mLibrary.get(), |
| 63 | threadID)); |
| 64 | if (!ret.mWindowProcHook) { |
| 65 | dprint("Failed to set WH_CALLWNDPROC: {}", GetLastError()); |
| 66 | } |
| 67 | return ret; |
| 68 | } |
| 69 | |
| 70 | bool Handles::IsValid() const { |
| 71 | return mLibrary && mMessageHook && mWindowProcHook; |
no test coverage detected