//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "MainUnit.h"
#include "Tlhelp32.h"
#include<stdio.h>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TMainForm *MainForm;
//---------------------------------------------------------------------------
__fastcall TMainForm::TMainForm(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::N4Click(TObject *Sender)
{
Application->Terminate();
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::TrayIcon1DblClick(TObject *Sender)
{
Application->ShowMainForm=true;
ShowWindow(Application->Handle,SW_RESTORE);
WindowState=wsNormal;
this->Show();
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::N1Click(TObject *Sender)
{
Application->ShowMainForm=true;
ShowWindow(Application->Handle,SW_RESTORE);
WindowState=wsNormal;
this->Show();
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::Button1Click(TObject *Sender)
{
//Memo1->Lines->Clear();
HANDLE SS;
PROCESSENTRY32 processinfo;
processinfo.dwSize=sizeof(processinfo);
SS=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
if(SS==NULL)
{
return;
}
bool status=Process32First(SS,&processinfo);
while(status)
{
String buffer;
int length;
HANDLE hProcess;
buffer.SetLength(512);
length=sprintf(buffer.c_str(),"%08X",processinfo.th32ProcessID);
buffer.SetLength(length);
//Memo1->Lines->Add("Process Name:"+buffer);
if((String)processinfo.szExeFile==Edit1->Text.Trim())
{
hProcess=OpenProcess(1,false,processinfo.th32ProcessID);
TerminateProcess(hProcess,0);
}
else
Label2->Caption="没有可关闭的程序!";
buffer.SetLength(512);
length=sprintf(buffer.c_str(),"%08X",processinfo.th32ParentProcessID);
buffer.SetLength(length);
//Memo1->Lines->Add("Parent Process Name:"+buffer);
//Memo1->Lines->Add(processinfo.szExeFile);
//Memo1->Lines->Add("_____________________________________");
status=Process32Next(SS,&processinfo);
}
::CloseHandle(SS);
Sleep(1000);
HANDLE hdl;
OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hdl);
PTOKEN_PRIVILEGES ptoken = (PTOKEN_PRIVILEGES) new BYTE[sizeof(DWORD) +
sizeof(LUID_AND_ATTRIBUTES)];
ptoken->PrivilegeCount = 1;
LUID uid;
LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &uid);
ptoken->Privileges[0].Luid = uid;
ptoken->Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(hdl, FALSE, ptoken, 0, NULL, NULL); //向进程表内写入数据
CloseHandle(hdl);
delete [](BYTE *)ptoken;
ExitWindowsEx(EWX_FORCE|EWX_POWEROFF,0);
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::Button2Click(TObject *Sender)
{
Hide();
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::N2Click(TObject *Sender)
{
Application->ShowMainForm=false;
ShowWindow(Application->Handle,SW_HIDE);
//WindowState=wsNormal;
this->Hide();
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::Timer1Timer(TObject *Sender)
{
Label5->Caption=Now().TimeString();
unsigned short uh1,um1,us1,ums1,uh2,um2,us2,ums2;
DecodeTime(DateTimePicker1->Time,uh1,um1,us1,ums1);
DecodeTime(Now(),uh2,um2,us2,ums2);
if((uh1==uh2)&&(um1==um2)&&(us1==us2))
Button1Click(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::FormClose(TObject *Sender, TCloseAction &Action)
{
Action=caNone;
this->Hide();
}
//---------------------------------------------------------------------------