// OicqKiller.cpp : Defines the class behaviors for the application.
//
#include "stdafx.h"
#include "OicqKiller.h"
#include "OicqKillerDlg.h"
#include <tlhelp32.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//////////////////////////////////////////////////////////////////////////
// 全局变量
CMutex gMutex;
COicqKillerApp theApp;
/////////////////////////////////////////////////////////////////////////////
// COicqKillerApp
BEGIN_MESSAGE_MAP(COicqKillerApp, CWinApp)
//{{AFX_MSG_MAP(COicqKillerApp)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG
ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// COicqKillerApp construction
COicqKillerApp::COicqKillerApp()
{
// 初始化参数
m_hExitApp = CreateEvent(NULL, TRUE, FALSE, NULL);
m_ThreadKillOicq = NULL;
m_pParm = new PARMSTRUCT;
memset(m_pParm, 0, sizeof(PARMSTRUCT));
m_pParm->bAutoRun = FALSE; // 默认情况:不会开机自动运行
m_pParm->bHiden = FALSE; // 默认情况:运行时会显示界面
m_pParm->bKill = FALSE; // 默认情况:不阻止QQ运行
m_pParm->lKeyValue = KY_DEFAULT; // 默认情况:采用的是默认热键Ctrl + *
m_pParm->lWaitSeconds = WS_DEFAULT; // 默认情况:每5秒检测一次
}
/////////////////////////////////////////////////////////////////////////////
// COicqKillerApp initialization
BOOL COicqKillerApp::InitInstance()
{
AfxEnableControlContainer();
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need.
#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif
// 一、初始化,启动所有功能
//////////////////////////////////////////////////////////////////////////
HKEY hKey = NULL;
long lErrorCode = -1;
// 0.设置开机自动运行
hKey = NULL;
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SoftWare\\Microsoft\\Windows\\CurrentVersion\\Run", NULL, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS)
{
char szPath[MAX_PATH] = {0};
GetModuleFileName(NULL, szPath, MAX_PATH);
CString scValue;
scValue.Format("%s", szPath);
RegSetValueEx(hKey, "OicqKiller.exe", 0, REG_SZ, (BYTE*)szPath, scValue.GetLength() + 1);
RegCloseKey(hKey);
}
else
{
AfxMessageBox("设置开机自动运行出错");
}
// 1.从注册表读取参数
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SoftWare\\OicqKiller", 0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS)
{// 成功
DWORD dwType;
DWORD dwBytes = 64;
BYTE szValue[4] = {0};
lErrorCode = RegQueryValueEx(hKey, "bAutoRun", 0, &dwType, szValue, &dwBytes);
m_pParm->bAutoRun = szValue[0] + szValue[1] * 0x100;
RegCloseKey(hKey);
}
else
{
m_pParm->bAutoRun = TRUE; // 默认情况
}
// 1.2 读取Hiden
hKey = NULL;
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SoftWare\\OicqKiller", 0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS)
{// 成功
DWORD dwType;
DWORD dwBytes = 64;
BYTE szValue[4] = {0};
lErrorCode = RegQueryValueEx(hKey, "bHiden", 0, &dwType, szValue, &dwBytes);
m_pParm->bHiden = szValue[0] + szValue[1] * 0x100;
RegCloseKey(hKey);
}
else
{
m_pParm->bHiden = TRUE; // 默认情况
}
// 1.3 读取Kill
hKey = NULL;
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SoftWare\\OicqKiller", 0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS)
{// 成功
DWORD dwType;
DWORD dwBytes = 64;
BYTE szValue[4] = {0};
lErrorCode = RegQueryValueEx(hKey, "bKill", 0, &dwType, szValue, &dwBytes);
m_pParm->bKill = szValue[0] + szValue[1] * 0x100;
RegCloseKey(hKey);
}
else
{
m_pParm->bKill = TRUE; // 默认情况
}
// 1.4 读取KeyValue
hKey = NULL;
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SoftWare\\OicqKiller", 0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS)
{// 成功
DWORD dwType;
DWORD dwBytes = 64;
BYTE szValue[4] = {0};
lErrorCode = RegQueryValueEx(hKey, "lKeyValue", 0, &dwType, szValue, &dwBytes);
m_pParm->lKeyValue = MAKELONG(MAKEWORD(szValue[0], szValue[1]), MAKEWORD(szValue[2], szValue[3]));
RegCloseKey(hKey);
}
else
{
m_pParm->lKeyValue = KY_DEFAULT; // 默认情况
}
// 1.5 读取WaitSeconds
hKey = NULL;
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SoftWare\\OicqKiller", 0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS)
{// 成功
DWORD dwType;
DWORD dwBytes = 64;
BYTE szValue[4] = {0};
lErrorCode = RegQueryValueEx(hKey, "lWaitSeconds", 0, &dwType, szValue, &dwBytes);
m_pParm->lWaitSeconds = MAKELONG(MAKEWORD(szValue[0], szValue[1]), MAKEWORD(szValue[2], szValue[3]));
RegCloseKey(hKey);
}
else
{
m_pParm->lWaitSeconds = WS_DEFAULT; // 默认情况
}
//////////////////////////////////////////////////////////////////////////
// 2.开启封杀线程
if (m_ThreadKillOicq == NULL)
{
m_ThreadKillOicq = AfxBeginThread(KillQicqProc, m_pParm);
if (m_ThreadKillOicq == NULL)
{// 2.1 开启线程失败
AfxMessageBox("开启QQ封杀线程失败,请重启软件");
return FALSE;
}
else
{// 2.2 开启线程成功
m_ThreadKillOicq->m_bAutoDelete = FALSE;
}
}
// 3.显示方式
COicqKillerDlg dlg;
dlg.Create(IDD_OICQKILLER_DIALOG, theApp.m_pMainWnd);
if (m_pParm->bHiden)
{// 隐藏
dlg.ShowWindow(SW_HIDE);
}
else
{// 显示
dlg.ShowWindow(SW_SHOW);
}
dlg.RunModalLoop();
return FALSE;
}
// 二、封杀线程体
UINT COicqKillerApp::KillQicqProc(LPVOID lpParm)
{
PPARMSTRUCTpParm = (PPARMSTRUCT)lpParm;
HANDLE hSnapShot = NULL;
while (1)
{
// 0.获取最新运行参数
gMutex.Lock();
LONG lWaitSeconds = pParm->lWaitSeconds;
BOOL bKill = pParm->bKill;
gMutex.Unlock();
if (WaitForSingleObject(theApp.m_hExitApp, lWaitSeconds) == WAIT_OBJECT_0)
{// 1.退出信号激活,退出线程
break;
}
if (!bKill)
{// 不阻止QQ运行
continue;
}
// 2.检查系统中是否有QQ进程
hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnapShot == NULL)
{
continue;
}
long nProcess= 0;
PROCESSENTRY32 prosInfo;
prosInfo.dwSize = sizeof(prosInfo);
BOOL bStatus = Process32First(hSnapShot, &prosInfo);
while (bStatus)
{
CString scTmp = prosInfo.szExeFile;
scTmp.MakeUpper();
if ( scTmp == "QQ.EXE")
{// 3.1 找到QQ进程
// 3.1.1 中止QQ进程
DWORD dwProcessID = prosInfo.th32ProcessID;
HANDLE hProcess = ::OpenProcess(PROCESS_TERMINATE, FALSE, dwProcessID);
::TerminateProcess(hProcess, 0);
CloseHandle(hProcess);
}
bStatus = Process32Next(hSnapShot, &prosInfo);
++nProcess;
}
}
return 1;
}
// 三、退出程序:释放资源
int COicqKillerApp::ExitInstance()
{
// 1.发出退出信号
if (m_ThreadKillOicq != NULL)
{
SetEvent(m_hExitApp);
}
// 2.关闭句柄
m_ThreadKillOicq = NULL;
CloseHandle(m_hExitApp);
// 3.清空缓存
delete m_pParm;
m_pParm = NULL;
return CWinApp::ExitInstance();
}