//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "UnitMain.h"
#include "DllFuncs.h"
#include "yb_base.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
#pragma link "hwhpdrv.lib"
TFormMain *FormMain;
TSharedMemory AppMem(MYAPPMARK, 4096); //定义共享的内存,这个内存是真正存在的
//---------------------------------------------------------------------------
__fastcall TFormMain::TFormMain(TComponent* Owner)
: TForm(Owner)
{
AppMem.AppInfo->hMainWnd = Application->Handle;
AppMem.AppInfo->hMainForm = Handle;
WriteStartupMessage();
PostMessage(Handle, WM_USERCMD, UC_INITWIN, 0);
MyHook = new THookedProcs;
MyHook->InitFuncs();
}
//---------------------------------------------------------------------------
__fastcall TFormMain::~TFormMain()
{
MyHook->UninitFuncs();
delete MyHook;
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::FormCloseQuery(TObject *Sender, bool &CanClose)
{
WriteShutdownMessage();
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::WndProc(Messages::TMessage &Message)
{
if(Message.Msg == WM_USERCMD)
{
if(Message.WParam == UC_SHOWWIN)
{
Show();
AppMem.ActiveAppWnd();
}
else if(Message.WParam == UC_INITWIN)
{
Hide();
Left = (Screen->Width - Width) / 2;
Top = (Screen->Height - Height) / 2;
}
else if(Message.WParam == UC_WINHOOK)
{
WinHookMessage(Message.LParam);
}
}
TForm::WndProc(Message);
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::BnExitClick(TObject *Sender)
{
Close();
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::BnHideClick(TObject *Sender)
{
Hide();
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::CreateParams(Controls::TCreateParams &Params)
{
TForm::CreateParams(Params);
Params.Style = WS_OVERLAPPED|WS_DLGFRAME|WS_CAPTION|WS_SYSMENU;
Params.ExStyle |= WS_EX_TOPMOST;
Params.X = Screen->Width - 8;
Params.Y = Screen->Height - 8;
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::WinHookMessage(long Param)
{
HWND hWnd = (HWND) Param;
char szWinCaption[256];
char szWinClass[256];
AnsiString s;
DateTimeX x;
s += AnsiString().sprintf("%04d-%02d-%02d %02d:%02d:%02d ", x.Year, x.Month, x.Day, x.Hour, x.Minute, x.Second);
if(!GetWindowText(hWnd, szWinCaption, 256))
*szWinCaption = 0;
if(!GetClassName(hWnd,szWinClass,256))
*szWinClass = 0;
s += "\"" + AnsiString(szWinClass) + "\" -> \"" + AnsiString(szWinCaption) + "\"";
if(!ValidAppCheck(hWnd, szWinClass,szWinCaption))
s += " (关闭禁用程序)";
s += "\r\n";
TBinFileFuncs::WriteLogFileInfo(TRelPath().Extension("sys").c_str(), s.c_str()); //与 .exe 同名的 .sys 文件
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::WriteStartupMessage(void)
{
AnsiString s;
DateTimeX x = StartupTime;
TSysInfo si;
s += "========== Copyright (C) Victor Chen ===== Email: victor@cppfans.com ==========\r\n";
s += AnsiString().sprintf("%04d-%02d-%02d %02d:%02d:%02d - ", x.Year, x.Month, x.Day, x.Hour, x.Minute, x.Second);
s += AnsiString().sprintf("[System Startup - %s %d.%02d.%04d]", si.OS->OSName, si.OS->MajorVer, si.OS->MinorVer, si.OS->BuildNum);
s += "\r\n";
TBinFileFuncs::WriteLogFileInfo(TRelPath().Extension("sys").c_str(), s.c_str()); //与 .exe 同名的 .sys 文件
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::WriteShutdownMessage(void)
{
AnsiString s;
DateTimeX x;
TimeX t; t.S000 = (x - StartupTime) % (24*60*60);
int d = (x - StartupTime) / (24*60*60);
s += AnsiString().sprintf("%04d-%02d-%02d %02d:%02d:%02d - ", x.Year, x.Month, x.Day, x.Hour, x.Minute, x.Second);
s += AnsiString().sprintf("[System Shutdown - %d days, %d hrs, %d mins, %d secs]", d, t.Hour, t.Minute, t.Second);
s += "\r\n";
TBinFileFuncs::WriteLogFileInfo(TRelPath().Extension("sys").c_str(), s.c_str()); //与 .exe 同名的 .sys 文件
}
//---------------------------------------------------------------------------
bool __fastcall TFormMain::ValidAppCheck(HWND hwnd, char *cls, char *cap)
{
TBinFile f;
f.FileName = TRelPath().Extension("cfg"); //与 .exe 同名的 .cfg 文件
f.OpenMode = TBinFile::omRead; //准备读文件
if(f.Exists) //如果文件存在
{
try
{
f.Active = true; //打开文件
char aLine[2048];
while(fgets(aLine,2000,f)) //读一行文本 (标准 C 函数)
{
if(strnicmp(aLine,"CLASS=",6)==0) //用类名识别
{
TBinFileFuncs::DeleteSpaces(aLine+6,1,1); //去除空格
if(stricmp(aLine+6,cls)==0)
{
PostMessage(hwnd, WM_CLOSE, 0, 0); //关闭程序
return false;
}
}
else if(strnicmp(aLine,"CAPTION=",8)==0) //用标题识别
{
TBinFileFuncs::DeleteSpaces(aLine+8,1,1); //去除空格
if(stricmp(aLine+8,cap)==0)
{
PostMessage(hwnd, WM_CLOSE, 0, 0); //关闭程序
return false;
}
}
}
}
catch(Exception &e)
{
//忽略错误提示, 也可以在此处把错误信息添加到一个错误信息文件
}
}
return true;
}
//---------------------------------------------------------------------------