#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "windows.h"
#include
#include
HHOOK keyHook = NULL;
LRESULT CALLBACK keyProc(int nCode,WPARAM wParam,LPARAM lParam );
DWORD GetProcessIDFromName(LPCSTR szName)
{
DWORD id = 0; // 进程ID
PROCESSENTRY32 pe; // 进程信息
pe.dwSize = sizeof(PROCESSENTRY32);
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); // 获取系统进程列表
if(Process32First(hSnapshot, &pe)) // 返回系统中第一个进程的信息
{
do
{
if(0 == _stricmp(pe.szExeFile, szName)) // 不区分大小写比较
{
id = pe.th32ProcessID;
break;
}
}while(Process32Next(hSnapshot, &pe)); // 下一个进程
}
CloseHandle(hSnapshot); // 删除快照
return id;
}
void initHook()
{
keyHook = SetWindowsHookEx(WH_KEYBOARD_LL, keyProc, GetModuleHandle(NULL), 0);
}
void unHook()
{
UnhookWindowsHookEx(keyHook);
}
//钩子用的函数
LRESULT CALLBACK keyProc(int nCode,WPARAM wParam,LPARAM lParam )
{
//在WH_KEYBOARD_LL模式下lParam 是指向KBDLLHOOKSTRUCT类型地址
KBDLLHOOKSTRUCT *pkbhs = (KBDLLHOOKSTRUCT *) lParam;
//如果nCode等于HC_ACTION则处理该消息,如果小于0,则钩子子程就必须将该消息传递给 CallNextHookEx
if(nCode == HC_ACTION){
if(pkbhs->vkCode == VK_ESCAPE && GetAsyncKeyState(VK_CONTROL)& 0x8000 && GetAsyncKeyState(VK_SHIFT)&0x8000){
qDebug() << "Ctrl+Shift+Esc";
return 1;
}else if(pkbhs->vkCode == VK_ESCAPE && GetAsyncKeyState(VK_CONTROL) & 0x8000){
qDebug() << "Ctrl+Esc";
return 1;
}else if(pkbhs->vkCode == VK_TAB && pkbhs->flags & LLKHF_ALTDOWN){
qDebug() << "Alt+Tab";
return 1;
}else if(pkbhs->vkCode == VK_ESCAPE && pkbhs->flags &LLKHF_ALTDOWN){
qDebug() << "Alt+Esc";
return 1;
}else if(pkbhs->vkCode == VK_LWIN || pkbhs->vkCode == VK_RWIN){
qDebug() << "LWIN/RWIN";
return 1;
}/*else if(pkbhs->vkCode == VK_F4 && pkbhs->flags & LLKHF_ALTDOWN){
qDebug() << "Alt+F4";
}
if(pkbhs->vkCode == VK_F1 && pkbhs->flags &LLKHF_ALTDOWN) {
unHook();
qDebug() << "Alt+F1 back room";
}*/
return 0;//返回1表示截取消息不再传递,返回0表示不作处理,消息继续传递
}
return CallNextHookEx(keyHook, nCode, wParam, lParam);
}
MainWindow::MainWindow(QWidget *parent) :
QWidget(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
SetWindowText((HWND)ui->task->winId(), "Windows 任务管理器");
ui->label->setText(u8"上班期间,禁止看小说");
showFullScreen();
EnableDebugPriv();
lock();
initHook();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::closeEvent(QCloseEvent *event)
{
event->ignore();
}
void MainWindow::keyPressEvent(QKeyEvent *event)
{
if (event->key() == Qt::Key_F && (event->modifiers() & Qt::ControlModifier))
{
unlock();
unHook();
qApp->exit();
}
}
//提升权限
void MainWindow::EnableDebugPriv(void)
{
HANDLE hToken;
LUID sedebugnameValue;
TOKEN_PRIVILEGES tkp;
if (!OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) {
return;
}
if (!LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &sedebugnameValue)) {
CloseHandle(hToken);
return;
}
tkp.PrivilegeCount = 1;
tkp.Privileges[0].Luid = sedebugnameValue;
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
if (!AdjustTokenPrivileges(hToken, FALSE, &tkp, sizeof(tkp), NULL, NULL)) {
CloseHandle(hToken);
return;
}
}
void MainWindow::unlock()
{
THREADENTRY32 th32;
th32.dwSize=sizeof(th32);
HANDLE hThreadSnap=::CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD,0);
if(hThreadSnap==INVALID_HANDLE_VALUE)
{
//AfxMessageBox(_T("CreateToolhelp32Snapshot调用失败!"));
return;
}
unsigned long Pid;
int wpid=GetProcessIDFromName("winlogon.exe");
Pid=wpid;
BOOL b=::Thread32First(hThreadSnap,&th32);
while(b)
{
if(th32.th32OwnerProcessID==Pid)
{
HANDLE oth=OpenThread(THREAD_ALL_ACCESS,FALSE,th32.th32ThreadID);
if(::ResumeThread(oth))
{
qDebug()<
}
else
{
qDebug()<
}
CloseHandle(oth);
break;
}
::Thread32Next(hThreadSnap,&th32);
}
::CloseHandle(hThreadSnap);
}
void MainWindow::lock()
{
THREADENTRY32 th32;
th32.dwSize=sizeof(th32);
HANDLE hThreadSnap=::CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD,0);
if(hThreadSnap==INVALID_HANDLE_VALUE)
{
//AfxMessageBox(_T("CreateToolhelp32Snapshot调用失败!"));
return;
}
unsigned long Pid;
int wpid=GetProcessIDFromName("winlogon.exe");
Pid=wpid;
BOOL b=::Thread32First(hThreadSnap,&th32);
while(b)
{
if(th32.th32OwnerProcessID==Pid)
{
HANDLE oth=OpenThread(THREAD_ALL_ACCESS,FALSE,th32.th32ThreadID);
if(!(::SuspendThread(oth)))
{
//m_List.SetItemText(idx,2,_T("已冻结!"));
qDebug()<
}
else
{
//m_List.SetItemText(idx,2,_T("冻结失败!"));
qDebug()<
}
CloseHandle(oth);
break;
}
::Thread32Next(hThreadSnap,&th32);
}
::CloseHandle(hThreadSnap);
}