forked from codebytere/node-mac-permissions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpcutil.cpp
More file actions
191 lines (169 loc) · 5.87 KB
/
Copy pathpcutil.cpp
File metadata and controls
191 lines (169 loc) · 5.87 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <napi.h>
#include <windows.h>
#include <ShlObj.h>
#include <Shlwapi.h>
#include <string.h>
#pragma comment(lib, "shlwapi.lib")
static bool GetFileVersion(const wchar_t *file_path, WORD *major_version, WORD *minor_version, WORD *build_number, WORD *revision_number)
{
DWORD handle, len;
UINT buf_len;
LPTSTR buf_data;
VS_FIXEDFILEINFO *file_info;
len = GetFileVersionInfoSizeW(file_path, &handle);
if (0 == len)
return false;
buf_data = (LPTSTR)malloc(len);
if (!buf_data)
return false;
if (!GetFileVersionInfoW(file_path, handle, len, buf_data))
{
free(buf_data);
return false;
}
if (VerQueryValueW(buf_data, L"\\", (LPVOID *)&file_info, (PUINT)&buf_len))
{
*major_version = HIWORD(file_info->dwFileVersionMS);
*minor_version = LOWORD(file_info->dwFileVersionMS);
*build_number = HIWORD(file_info->dwFileVersionLS);
*revision_number = LOWORD(file_info->dwFileVersionLS);
free(buf_data);
return true;
}
free(buf_data);
return false;
}
static int GetNTDLLVersion()
{
static int ret = 0;
if (ret == 0)
{
wchar_t buf_dll_name[MAX_PATH] = {0};
HRESULT hr = ::SHGetFolderPathW(NULL, CSIDL_SYSTEM, NULL, SHGFP_TYPE_CURRENT, buf_dll_name);
if (SUCCEEDED(hr) && ::PathAppendW(buf_dll_name, L"ntdll.dll"))
{
WORD major_version, minor_version, build_number, revision_number;
GetFileVersion(buf_dll_name, &major_version, &minor_version, &build_number, &revision_number);
ret = major_version * 100 + minor_version;
}
}
return ret;
}
//遍历windows窗口
struct CaptureTargetInfo
{
HWND id = 0;
std::wstring title;
RECT rc{0, 0, 0, 0};
};
typedef std::vector<CaptureTargetInfo> CaptureTargetInfoList;
static BOOL CALLBACK WindowsEnumerationHandler(HWND hwnd, LPARAM param)
{
CaptureTargetInfoList *list =
reinterpret_cast<CaptureTargetInfoList *>(param);
// Skip windows that are invisible, minimized, have no title, or are owned,
// unless they have the app window style set.
HWND owner = GetWindow(hwnd, GW_OWNER);
LONG exstyle = GetWindowLong(hwnd, GWL_EXSTYLE);
if (IsIconic(hwnd) || !IsWindowVisible(hwnd) ||
(owner && !(exstyle & WS_EX_APPWINDOW)) || (exstyle & WS_EX_LAYERED))
{
return TRUE;
}
int len = GetWindowTextLength(hwnd);
if (len == 0)
{
return TRUE;
}
// Skip the Program Manager window and the Start button.
const size_t kClassLength = 256;
WCHAR class_name[kClassLength];
int class_name_length = GetClassNameW(hwnd, class_name, kClassLength);
// Skip Program Manager window and the Start button. This is the same logic
// that's used in Win32WindowPicker in libjingle. Consider filtering other
// windows as well (e.g. toolbars).
if (wcscmp(class_name, L"Progman") == 0 || wcscmp(class_name, L"Button") == 0)
return TRUE;
if (GetNTDLLVersion() >= 602 &&
(wcscmp(class_name, L"ApplicationFrameWindow") == 0 ||
wcscmp(class_name, L"Windows.UI.Core.CoreWindow") == 0))
{
return TRUE;
}
CaptureTargetInfo window;
window.id = hwnd;
const size_t kTitleLength = 500;
WCHAR window_title[kTitleLength];
// Truncate the title if it's longer than kTitleLength.
GetWindowTextW(hwnd, window_title, kTitleLength);
window.title = window_title;
// Skip windows when we failed to convert the title or it is empty.
if (window.title.empty())
return TRUE;
list->push_back(window);
return TRUE;
}
static bool GetCaptureWindowList(CaptureTargetInfoList *windows)
{
CaptureTargetInfoList result;
LPARAM param = reinterpret_cast<LPARAM>(&result);
if (!EnumWindows(&WindowsEnumerationHandler, param))
return false;
for (auto e : result)
windows->push_back(e);
return true;
}
static std::string wstring2string(std::wstring wstr)
{
std::string result;
//获取缓冲区大小,并申请空间,缓冲区大小事按字节计算的
int len = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.size(), NULL, 0, NULL, NULL);
char *buffer = new char[len + 1];
//宽字节编码转换成多字节编码
WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.size(), buffer, len, NULL, NULL);
buffer[len] = '\0';
//删除缓冲区并返回值
result.append(buffer);
delete[] buffer;
return result;
}
Napi::Array enumerateWindows(const Napi::CallbackInfo &info)
{
Napi::Env env = info.Env();
Napi::Array ar = Napi::Array::New(env);
CaptureTargetInfoList windows;
/// for desktop
windows.push_back({0, L"Desktop", RECT{0, 0, 0, 0}});
/// for monitors
EnumDisplayMonitors(
nullptr, nullptr, [](HMONITOR hmon, HDC, LPRECT pRC, LPARAM lparam) {
auto &monitors = *reinterpret_cast<CaptureTargetInfoList *>(lparam);
wchar_t buf[100] = {0};
swprintf_s(buf, L"Monitor:(%d,%d,%d,%d)", pRC->left, pRC->top, pRC->right, pRC->bottom);
monitors.push_back({(HWND)hmon, buf, *pRC});
return TRUE;
},
reinterpret_cast<LPARAM>(&windows));
/// for windows
GetCaptureWindowList(&windows);
uint32_t i = 0;
for (auto w : windows)
{
Napi::Object obj = Napi::Object::New(env);
obj.Set("id", reinterpret_cast<int32_t>(w.id));
obj.Set("title", wstring2string(w.title).c_str());
obj.Set("left", w.rc.left);
obj.Set("top", w.rc.top);
obj.Set("right", w.rc.right);
obj.Set("bottom", w.rc.bottom);
ar[i++] = obj;
}
return ar;
}
// Initializes all functions exposed to JS
Napi::Object Init(Napi::Env env, Napi::Object exports) {
exports.Set(Napi::String::New(env, "enumerateWindows"),
Napi::Function::New(env, enumerateWindows));
return exports;
}
NODE_API_MODULE(pcutil, Init)