You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
3.1 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include "modules/desktop_capture/win/desktop.h"
  11. #include <vector>
  12. #include "rtc_base/logging.h"
  13. namespace webrtc {
  14. Desktop::Desktop(HDESK desktop, bool own) : desktop_(desktop), own_(own) {}
  15. Desktop::~Desktop() {
  16. if (own_ && desktop_ != NULL) {
  17. if (!::CloseDesktop(desktop_)) {
  18. RTC_LOG(LS_ERROR) << "Failed to close the owned desktop handle: "
  19. << GetLastError();
  20. }
  21. }
  22. }
  23. bool Desktop::GetName(std::wstring* desktop_name_out) const {
  24. if (desktop_ == NULL)
  25. return false;
  26. DWORD length = 0;
  27. int rv = GetUserObjectInformationW(desktop_, UOI_NAME, NULL, 0, &length);
  28. if (rv || GetLastError() != ERROR_INSUFFICIENT_BUFFER)
  29. abort();
  30. length /= sizeof(WCHAR);
  31. std::vector<WCHAR> buffer(length);
  32. if (!GetUserObjectInformationW(desktop_, UOI_NAME, &buffer[0],
  33. length * sizeof(WCHAR), &length)) {
  34. RTC_LOG(LS_ERROR) << "Failed to query the desktop name: " << GetLastError();
  35. return false;
  36. }
  37. desktop_name_out->assign(&buffer[0], length / sizeof(WCHAR));
  38. return true;
  39. }
  40. bool Desktop::IsSame(const Desktop& other) const {
  41. std::wstring name;
  42. if (!GetName(&name))
  43. return false;
  44. std::wstring other_name;
  45. if (!other.GetName(&other_name))
  46. return false;
  47. return name == other_name;
  48. }
  49. bool Desktop::SetThreadDesktop() const {
  50. if (!::SetThreadDesktop(desktop_)) {
  51. RTC_LOG(LS_ERROR) << "Failed to assign the desktop to the current thread: "
  52. << GetLastError();
  53. return false;
  54. }
  55. return true;
  56. }
  57. Desktop* Desktop::GetDesktop(const WCHAR* desktop_name) {
  58. ACCESS_MASK desired_access = DESKTOP_CREATEMENU | DESKTOP_CREATEWINDOW |
  59. DESKTOP_ENUMERATE | DESKTOP_HOOKCONTROL |
  60. DESKTOP_WRITEOBJECTS | DESKTOP_READOBJECTS |
  61. DESKTOP_SWITCHDESKTOP | GENERIC_WRITE;
  62. HDESK desktop = OpenDesktopW(desktop_name, 0, FALSE, desired_access);
  63. if (desktop == NULL) {
  64. RTC_LOG(LS_ERROR) << "Failed to open the desktop '" << desktop_name
  65. << "': " << GetLastError();
  66. return NULL;
  67. }
  68. return new Desktop(desktop, true);
  69. }
  70. Desktop* Desktop::GetInputDesktop() {
  71. HDESK desktop = OpenInputDesktop(
  72. 0, FALSE, GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE);
  73. if (desktop == NULL)
  74. return NULL;
  75. return new Desktop(desktop, true);
  76. }
  77. Desktop* Desktop::GetThreadDesktop() {
  78. HDESK desktop = ::GetThreadDesktop(GetCurrentThreadId());
  79. if (desktop == NULL) {
  80. RTC_LOG(LS_ERROR)
  81. << "Failed to retrieve the handle of the desktop assigned to "
  82. "the current thread: "
  83. << GetLastError();
  84. return NULL;
  85. }
  86. return new Desktop(desktop, false);
  87. }
  88. } // namespace webrtc