File: hooking_win_proc.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,122,156 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (91 lines) | stat: -rw-r--r-- 2,779 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "hooking_win_proc.h"

#include <windows.h>

LRESULT CALLBACK WndProc(HWND window,
                         UINT message,
                         WPARAM w_param,
                         LPARAM l_param) {
  // Injected keystroke via PostThreadMessage won't be dispatched to here.
  // Have to use SendMessage or SendInput to trigger the keyboard hook.
  switch (message) {
    case WM_KEYDOWN:
      break;
    case WM_KEYUP:
      break;
    case WM_CLOSE:
      ::DestroyWindow(window);
      break;
    case WM_DESTROY:
      ::PostQuitMessage(0);
      break;
    default:
      return ::DefWindowProc(window, message, w_param, l_param);
  }
  return 0;
}

int WINAPI WinMain(HINSTANCE instance,
                   HINSTANCE prev_instance,
                   LPSTR cmd_line,
                   int cmd_show) {
  constexpr wchar_t winproc_class_name[] = L"myWindowClass";
  constexpr wchar_t winproc_window_name[] = L"ChromeMitigationTests";

  // The parent process should have set up this named event already.
  HANDLE event = ::OpenEventW(EVENT_MODIFY_STATE, FALSE,
                              hooking_win_proc::g_winproc_event);
  if (event == NULL || event == INVALID_HANDLE_VALUE)
    return 1;

  // Step 1: Registering the Window Class.
  WNDCLASSEX window_class;
  window_class.cbSize = sizeof(WNDCLASSEX);
  window_class.style = 0;
  window_class.lpfnWndProc = WndProc;
  window_class.cbClsExtra = 0;
  window_class.cbWndExtra = 0;
  window_class.hInstance = instance;
  window_class.hIcon = ::LoadIcon(NULL, IDI_APPLICATION);
  window_class.hCursor = ::LoadCursor(NULL, IDC_ARROW);
  window_class.hbrBackground = reinterpret_cast<HBRUSH>(COLOR_WINDOWFRAME);
  window_class.lpszMenuName = NULL;
  window_class.lpszClassName = winproc_class_name;
  window_class.hIconSm = ::LoadIcon(NULL, IDI_APPLICATION);

  if (!::RegisterClassEx(&window_class))
    return 1;

  // Step 2: Create the Window.
  HWND window =
      ::CreateWindowExW(WS_EX_CLIENTEDGE, winproc_class_name,
                        winproc_window_name, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,
                        CW_USEDEFAULT, 240, 120, NULL, NULL, instance, NULL);

  if (window == NULL)
    return 1;

  ::ShowWindow(window, cmd_show);
  ::UpdateWindow(window);

  // Step 3: Signal that WinProc is up and running.
  ::SetEvent(event);

  // Step 4: The Message Loop
  // WM_QUIT results in a 0 value from GetMessageW,
  // breaking the loop.
  MSG message;
  while (::GetMessageW(&message, NULL, 0, 0) > 0) {
    ::TranslateMessage(&message);
    ::DispatchMessageW(&message);
  }

  ::SetEvent(event);
  ::CloseHandle(event);

  return 0;
}