wrapped_window_proc.cc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "base/win/wrapped_window_proc.h"
  5. #include "base/atomicops.h"
  6. #include "base/check.h"
  7. #include "base/notreached.h"
  8. #include "base/strings/string_util.h"
  9. namespace {
  10. base::win::WinProcExceptionFilter s_exception_filter = nullptr;
  11. HMODULE GetModuleFromWndProc(WNDPROC window_proc) {
  12. HMODULE instance = nullptr;
  13. // Converting a pointer-to-function to a void* is undefined behavior, but
  14. // Windows (and POSIX) APIs require it to work.
  15. void* address = reinterpret_cast<void*>(window_proc);
  16. if (!::GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
  17. GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
  18. static_cast<char*>(address), &instance)) {
  19. NOTREACHED();
  20. }
  21. return instance;
  22. }
  23. } // namespace.
  24. namespace base {
  25. namespace win {
  26. WinProcExceptionFilter SetWinProcExceptionFilter(
  27. WinProcExceptionFilter filter) {
  28. subtle::AtomicWord rv = subtle::NoBarrier_AtomicExchange(
  29. reinterpret_cast<subtle::AtomicWord*>(&s_exception_filter),
  30. reinterpret_cast<subtle::AtomicWord>(filter));
  31. return reinterpret_cast<WinProcExceptionFilter>(rv);
  32. }
  33. int CallExceptionFilter(EXCEPTION_POINTERS* info) {
  34. return s_exception_filter ? s_exception_filter(info)
  35. : EXCEPTION_CONTINUE_SEARCH;
  36. }
  37. BASE_EXPORT void InitializeWindowClass(const wchar_t* class_name,
  38. WNDPROC window_proc,
  39. UINT style,
  40. int class_extra,
  41. int window_extra,
  42. HCURSOR cursor,
  43. HBRUSH background,
  44. const wchar_t* menu_name,
  45. HICON large_icon,
  46. HICON small_icon,
  47. WNDCLASSEX* class_out) {
  48. class_out->cbSize = sizeof(WNDCLASSEX);
  49. class_out->style = style;
  50. class_out->lpfnWndProc = window_proc;
  51. class_out->cbClsExtra = class_extra;
  52. class_out->cbWndExtra = window_extra;
  53. // RegisterClassEx uses a handle of the module containing the window procedure
  54. // to distinguish identically named classes registered in different modules.
  55. class_out->hInstance = GetModuleFromWndProc(window_proc);
  56. class_out->hIcon = large_icon;
  57. class_out->hCursor = cursor;
  58. class_out->hbrBackground = background;
  59. class_out->lpszMenuName = menu_name;
  60. class_out->lpszClassName = class_name;
  61. class_out->hIconSm = small_icon;
  62. // Check if |window_proc| is valid.
  63. DCHECK(class_out->hInstance != nullptr);
  64. }
  65. } // namespace win
  66. } // namespace base