3#include <QLoggingCategory>
4#include <RabbitCommonTools.h>
9#include "HookWindows.h"
10#include "RabbitCommonTools.h"
12static Q_LOGGING_CATEGORY(log,
"Client.Hook.Windows")
15 :
CHook(pParaClient, parent),
20CHookWindows::~CHookWindows()
22 qDebug(log) <<
"CHookWindows::~CHookWindows()";
26LRESULT CALLBACK CHookWindows::keyboardHookProc(INT code, WPARAM wparam, LPARAM lparam)
28 if (code == HC_ACTION)
30 KBDLLHOOKSTRUCT* hook =
reinterpret_cast<KBDLLHOOKSTRUCT*
>(lparam);
36 Qt::KeyboardModifiers keyMdf = Qt::NoModifier;
52 key = Qt::Key_Super_L;
53 keyMdf = Qt::MetaModifier;
58 key = Qt::Key_Super_R;
59 keyMdf = Qt::MetaModifier;
74 key = Qt::Key_Control;
90 CFrmViewer* self = qobject_cast<CFrmViewer*>(QApplication::focusWidget());
91 WId foreground_window =
reinterpret_cast<WId
>(GetForegroundWindow());
92 if (self && self->parentWidget()->window()->winId() == foreground_window)
94 if(wparam == WM_KEYDOWN || wparam == WM_SYSKEYDOWN)
95 emit self->sigKeyPressEvent(
new QKeyEvent(QKeyEvent::KeyPress, key, keyMdf));
97 if(wparam == WM_KEYUP || wparam == WM_SYSKEYUP)
98 emit self->sigKeyReleaseEvent(
new QKeyEvent(QKeyEvent::KeyRelease, key, Qt::NoModifier));
101 qDebug(log) <<
"process vkCode:" << hook->vkCode
102 <<
"scanCode:" << hook->scanCode
103 <<
"flags:" << hook->flags;
119 return CallNextHookEx(
nullptr, code, wparam, lparam);
123int CHookWindows::RegisterKeyboard()
125 if(!RabbitCommon::CTools::Instance()->HasAdministratorPrivilege()
126 && m_pParameterClient->GetPromptAdministratorPrivilege())
130 QMessageBox::Warning, tr(
"Warning"),
131 tr(
"The programe is not administrator privilege.\n"
132 "Don't disable system shortcuts(eg: Ctrl+Alt+del).\n"
133 "Restart program by administrator?"),
134 QMessageBox::Yes | QMessageBox::No);
135 msg.setCheckBox(
new QCheckBox(tr(
"Always shown"), &msg));
136 msg.checkBox()->setCheckable(
true);
137 msg.checkBox()->setChecked(
138 m_pParameterClient->GetPromptAdministratorPrivilege());
140 if(QMessageBox::Yes == nRet) {
141 RabbitCommon::CTools::Instance()->StartWithAdministratorPrivilege(
true);
143 if(m_pParameterClient->GetPromptAdministratorPrivilege()
144 != msg.checkBox()->isChecked()) {
145 m_pParameterClient->SetPromptAdministratorPrivilege(
146 msg.checkBox()->isChecked());
151 UnRegisterKeyboard();
152 DisableTaskManager(
true);
153 m_hKeyboard = SetWindowsHookExW(WH_KEYBOARD_LL, keyboardHookProc,
nullptr, 0);
154 if(NULL == m_hKeyboard) {
155 qCritical(log) <<
"SetWindowsHookExW error:" << GetLastError();
161int CHookWindows::UnRegisterKeyboard()
165 UnhookWindowsHookEx(m_hKeyboard);
166 m_hKeyboard =
nullptr;
168 DisableTaskManager(
false);
173bool EnableDebugPrivileges() {
174 HANDLE hToken = NULL;
175 LUID sedebugnameValue;
176 TOKEN_PRIVILEGES tkp;
179 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) {
180 qDebug(log) <<
"OpenProcessToken failed. Error:" << GetLastError();
186 if (!LookupPrivilegeValue(NULL, SE_RESTORE_NAME, &sedebugnameValue)) {
187 qDebug(log) <<
"LookupPrivilegeValue (SE_DEBUG_NAME) failed. Error:" << GetLastError();
192 tkp.PrivilegeCount = 1;
193 tkp.Privileges[0].Luid = sedebugnameValue;
194 tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
197 if (!AdjustTokenPrivileges(hToken, FALSE, &tkp,
sizeof(tkp), NULL, NULL)) {
198 qDebug(log) <<
"AdjustTokenPrivileges failed. Error:" << GetLastError();
203 if (GetLastError() == ERROR_NOT_ALL_ASSIGNED) {
204 qDebug(log) <<
"The token does not have the specified privileges.";
207 qDebug(log) <<
"Debug privileges enabled successfully.";
216#define TASKMANAGERSystem "HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System"
217#define TASKMANAGERExplorer "HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer"
218void CHookWindows::DisableTaskManager(
bool flag)
221 int value = flag ? 0x00000001 : 0x00000000;
222 QSettings settings(TASKMANAGERSystem, QSettings::NativeFormat);
223 settings.setValue(
"DisableTaskMgr", value);
224 settings.setValue(
"DisableChangePassword", value);
225 settings.setValue(
"DisableLockWorkstation", value);
226 settings.setValue(
"DisableSwitchUserOption", value);
228 QSettings settings2(TASKMANAGERExplorer, QSettings::NativeFormat);
229 settings2.setValue(
"NoLogOff", value);
A widget which displays output image from a CConnectDesktop and sends input keypresses and mouse acti...
The class is the HOOK abstract class.
The parameters of client.