5#include <QLoggingCategory>
6#include <RabbitCommonTools.h>
11#include "HookWindows.h"
12#include "RabbitCommonTools.h"
14static Q_LOGGING_CATEGORY(log,
"Plugin.Hook.Windows")
17 :
CHook(pParaClient, parent),
22CHookWindows::~CHookWindows()
24 qDebug(log) <<
"CHookWindows::~CHookWindows()";
28LRESULT CALLBACK CHookWindows::keyboardHookProc(INT code, WPARAM wparam, LPARAM lparam)
30 if (code == HC_ACTION)
32 KBDLLHOOKSTRUCT* hook =
reinterpret_cast<KBDLLHOOKSTRUCT*
>(lparam);
38 Qt::KeyboardModifiers keyMdf = Qt::NoModifier;
54 key = Qt::Key_Super_L;
55 keyMdf = Qt::MetaModifier;
60 key = Qt::Key_Super_R;
61 keyMdf = Qt::MetaModifier;
69 keyMdf = Qt::KeyboardModifier::AltModifier;
76 key = Qt::Key_Control;
77 keyMdf = Qt::KeyboardModifier::ControlModifier;
94 CFrmViewer* self = qobject_cast<CFrmViewer*>(QApplication::focusWidget());
95 WId foreground_window =
reinterpret_cast<WId
>(GetForegroundWindow());
96 if (self && self->parentWidget()->window()->winId() == foreground_window)
98 if(wparam == WM_KEYDOWN || wparam == WM_SYSKEYDOWN)
100 QApplication::postEvent(self,
new QKeyEvent(QKeyEvent::KeyPress, key, keyMdf));
101 if(wparam == WM_KEYUP || wparam == WM_SYSKEYUP)
103 QApplication::postEvent(self,
new QKeyEvent(QKeyEvent::KeyRelease, key, keyMdf));
125 return CallNextHookEx(
nullptr, code, wparam, lparam);
129int CHookWindows::OnRegisterKeyboard()
132 UnRegisterKeyboard();
133 m_hKeyboard = SetWindowsHookExW(WH_KEYBOARD_LL, keyboardHookProc,
nullptr, 0);
134 if(NULL == m_hKeyboard) {
135 qCritical(log) <<
"SetWindowsHookExW error:" << GetLastError();
141int CHookWindows::OnUnRegisterKeyboard()
145 UnhookWindowsHookEx(m_hKeyboard);
146 m_hKeyboard =
nullptr;
151int CHookWindows::OnDisableDesktopShortcuts()
153 DisableTaskManager(
true);
157int CHookWindows::OnRestoreDesktopShortcuts()
159 DisableTaskManager(
false);
164bool EnableDebugPrivileges() {
165 HANDLE hToken = NULL;
166 LUID sedebugnameValue;
167 TOKEN_PRIVILEGES tkp;
170 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) {
171 qDebug(log) <<
"OpenProcessToken failed. Error:" << GetLastError();
177 if (!LookupPrivilegeValue(NULL, SE_RESTORE_NAME, &sedebugnameValue)) {
178 qDebug(log) <<
"LookupPrivilegeValue (SE_DEBUG_NAME) failed. Error:" << GetLastError();
183 tkp.PrivilegeCount = 1;
184 tkp.Privileges[0].Luid = sedebugnameValue;
185 tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
188 if (!AdjustTokenPrivileges(hToken, FALSE, &tkp,
sizeof(tkp), NULL, NULL)) {
189 qDebug(log) <<
"AdjustTokenPrivileges failed. Error:" << GetLastError();
194 if (GetLastError() == ERROR_NOT_ALL_ASSIGNED) {
195 qDebug(log) <<
"The token does not have the specified privileges.";
198 qDebug(log) <<
"Debug privileges enabled successfully.";
207#define TASKMANAGERSystem "HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System"
208#define TASKMANAGERExplorer "HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer"
209#define WINLOGON "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon"
210void CHookWindows::DisableTaskManager(
bool flag)
213 int value = flag ? 0x00000001 : 0x00000000;
214 QSettings system(TASKMANAGERSystem, QSettings::NativeFormat);
215 system.setValue(
"DisableTaskMgr", value);
216 system.setValue(
"DisableChangePassword", value);
217 system.setValue(
"DisableSwitchUserOption", value);
218 system.setValue(
"DisableCAD", value);
219 system.setValue(
"DisableLockWorkstation", value);
221 QSettings explorer(TASKMANAGERExplorer, QSettings::NativeFormat);
222 explorer.setValue(
"NoLogOff", value);
231bool CHookWindows::DisableWindowsKey()
237 if (!RegisterHotKey(
nullptr, 1, MOD_WIN, VK_LWIN)) {
238 qCritical(log) <<
"注册左 Windows 键拦截失败:" << GetLastError();
242 if (!RegisterHotKey(
nullptr, 2, MOD_WIN, VK_RWIN)) {
243 qCritical(log) <<
"注册右 Windows 键拦截失败:" << GetLastError();
249 LONG result = RegCreateKeyExW(HKEY_CURRENT_USER,
250 L
"Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer",
251 0,
nullptr, 0, KEY_ALL_ACCESS,
nullptr, &hKey,
nullptr);
253 if (result == ERROR_SUCCESS) {
255 result = RegSetValueExW(hKey, L
"NoWinKeys", 0, REG_DWORD,
256 reinterpret_cast<const BYTE*
>(&value),
sizeof(value));
258 if (result == ERROR_SUCCESS) {
259 qDebug(log) <<
"注册表设置成功,Windows 键已禁用";
261 qWarning(log) <<
"注册表设置失败:" << result;
266 qWarning(log) <<
"创建注册表键失败:" << result;
273bool CHookWindows::EnableWindowsKey()
278 UnregisterHotKey(
nullptr, 1);
279 UnregisterHotKey(
nullptr, 2);
283 LONG result = RegOpenKeyExW(HKEY_CURRENT_USER,
284 L
"Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer",
285 0, KEY_ALL_ACCESS, &hKey);
287 if (result == ERROR_SUCCESS) {
288 result = RegDeleteValueW(hKey, L
"NoWinKeys");
289 if (result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND) {
290 qDebug(log) <<
"Windows 键已恢复";
292 qWarning(log) <<
"删除注册表值失败:" << result;
301bool CHookWindows::DisableTaskManager()
305 LONG result = RegCreateKeyExW(HKEY_CURRENT_USER,
306 L
"Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System",
307 0,
nullptr, 0, KEY_ALL_ACCESS,
nullptr, &hKey,
nullptr);
309 if (result == ERROR_SUCCESS) {
311 result = RegSetValueExW(hKey, L
"DisableTaskMgr", 0, REG_DWORD,
312 reinterpret_cast<const BYTE*
>(&value),
sizeof(value));
314 if (result == ERROR_SUCCESS) {
315 qDebug(log) <<
"任务管理器已禁用";
318 qWarning(log) <<
"禁用任务管理器失败:" << result;
326bool CHookWindows::EnableTaskManager()
329 LONG result = RegOpenKeyExW(HKEY_CURRENT_USER,
330 L
"Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System",
331 0, KEY_ALL_ACCESS, &hKey);
333 if (result == ERROR_SUCCESS) {
334 result = RegDeleteValueW(hKey, L
"DisableTaskMgr");
337 if (result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND) {
338 qDebug(log) <<
"任务管理器已恢复";
The class is the HOOK abstract class.
Global parameters of plugins.