This program searches and lists the IDs of all processes except this process in the system, corresponding file specifiers, priority, CPU share, number of threads, related process information, etc. by calling several API functions in kernel32.dll. Related information and abort the selected process.
When this program is running, an icon will be added in the system tray area, it will not appear in the task list that appears when pressing Ctrl+Alt+Del, nor will the task button be displayed on the taskbar, and it will be automatically hidden when inactive or minimized. It will not run repeatedly. If the program is already running, it will only activate the running program when it wants to run it.
This program avoids repeated running methods of programs is quite unique. Because after I tried some methods on the Internet, I found that when the program was activated from the minimized state, the window could not be minimized when clicking the window minimize button. So the author adopts the method of sending and processing custom messages. When the program is running, first enumerate the windows in the system. If you find that the program is running, send a custom message to the program window and then end. The running program displays a window after receiving the custom message.
//Project file PRocviewpro.dpr
program procviewpro;
uses
Forms, windows, messages, main in 'procview.pas' {Form1};
{$R *.RES}
{
//This is automatic by the system
Begin
application.Initialize;
Application.Title :='System process monitoring';
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
}
var
myhwnd:hwnd;
Begin
myhwnd := FindWindow(nil, 'system process monitoring'); // Find window
If myhwnd=0 then // No found, continue running
Begin
Application.Initialize;
Application.Title :='System process monitoring';
Application.CreateForm(TForm1, Form1);
Application.Run;
end
else //Discover the window, send the mouse click system tray area message to activate the window
postmessage(myhwnd,WM_SYSTRAYMSG,0,wm_lbuttondown);
{
//The disadvantage of the following method is: if the window was originally in a minimized state, clicking the window minimize button after activation will not minimize the window.
showwindow(myhwnd,sw_restore);
FlashWindow(MYHWND,TRUE);
}
end.
{
//The following is a method to use global atoms to avoid repeated running of programs
const
atomstr='procview';
var
atom:integer;
Begin
if globalfindatom(atomstr)=0 then
Begin
atom:=globaladdatom(atomstr);
with application do
Begin
Initialize;
Title := 'System Process Monitoring';
CreateForm(TForm1, Form1);
Run;
end;
globaldeleteatom(atom);
end;
end.
}
//Unit file procview.pas
unit procview;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, TLHelp32, Buttons, ComCtrls, ExtCtrls, ShellAPI, MyFlag;
const
PROCESS_TERMINATE=0;
SYSTRAY_ID=1;
WM_SYSTRAYMSG=WM_USER+100;
type
TForm1 = class(TForm)
lvSysProc: TListView;
lblSysProc: TLabel;
lblAboutProc: TLabel;
lvAboutProc: TListView;
lblCountSysProc: TLabel;
lblCountAboutProc: TLabel;
Panel1: TPanel;
btnDetermine: TButton;
btnRefresh: TButton;
lblOthers: TLabel;
lblEmail: TLabel;
MyFlag1: TMyFlag;
procedure btnRefreshClick(Sender: T Object );
procedure btnDetermineClick(Sender: T Object );
procedure lvSysProcClick(Sender: T Object );
procedure FormCreate(Sender: T Object );
procedure AppOnMinimize(Sender:T Object );
procedure FormClose(Sender: T Object ; var Action: TCloseAction);
procedure FormDeactivate(Sender: T Object );
procedure lblEmailClick(Sender: T Object );
procedure FormResize(Sender: T Object );
Private
{ Private declarations }
fshandle:thandle;
FormOldHeight,FormOldWidth:Integer;
procedure SysTray OnClick (var message:TMessage);message WM_SYSTRAYMSG;
public
{ Public declarations }
end;
var
Form1: TForm1;
idid: dWord;
fp32:tprocessentry32;
fm32:tmoduleentry32;
SysTrayIcon:TNotifyIconData;
Implementation
{$R *.DFM}
function RegisterServiceProcess(dwProcessID,dwType:integer):integer;stdcall;external 'KERNEL32.DLL';
procedure TForm1.btnRefreshClick(Sender: T Object );
var
clp:bool;
newitem1:Tlistitem;
MyIcon:TIcon;
IconIndex:word;
ProcFile : array[0..MAX_PATH] of char;
Begin
MyIcon:=TIcon.create;
lvSysProc.Items.clear;
lvSysProc.SmallImages.clear;
fshandle:=CreateToolhelp32Snapshot(th32cs_snapprocess,0);
fp32.dwsize:=sizeof(fp32);
clp:=process32first(fshandle,fp32);
IconIndex:=0;
while integer(clp)<>0 do
Begin
if fp32.th32processid<>getcurrentprocessid then
Begin
newitem1:=lvSysProc.items.add;
{
newitem1.caption:=fp32.szexefile;
MyIcon.Handle:=ExtractIcon(Form1.Handle,fp32.szexefile,0);
}
StrCopy(ProcFile,fp32.szExeFile);
newitem1.caption:=ProcFile;
MyIcon.Handle:=ExtractAssociatedIcon(HINSTANCE,ProcFile,IconIndex);
if MyIcon.Handle<>0 then
Begin
with lvSysProc do
Begin
NewItem1.ImageIndex:=smallimages.addicon(MyIcon);
end;
end;
with newitem1.subitems do
Begin
add(IntToHex(fp32.th32processid,4));
Add(IntToHex(fp32.th32ParentProcessID,4));
Add(IntToHex(fp32.pcPriClassBase,4));
Add(IntToHex(fp32.cntUsage,4));
Add(IntToStr(fp32.cntThreads));
end;
end;
clp:=process32next(fshandle,fp32);
end;
closehandle(fshandle);
lblCountSysProc.caption:=IntToStr(lvSysProc.items.count);
MyIcon.Free;
end;
procedure TForm1.btnDetermineClick(Sender: T Object );
var
processhndle:thandle;
Begin
with lvSysProc do
Begin
if selected=nil then
Begin
messagebox(form1.handle,'Please select the process to be terminated first!','Operation prompt',MB_OK+MB_ICONINFORMATION);
end
else
Begin
if messagebox(form1.handle,pchar('terminate'+itemfocused.caption+'?')
,'Terminate process',mb_yesno+MB_ICONWARNING+MB_DEFBUTTON2)=mryes then
Begin
idid:=strtoint('$'+itemfocused.subitems[0]);
processhndle:=openprocess(PROCESS_TERMINATE,bool(0),idid);
if integer(terminateprocess(processshndle,0))=0 then
messagebox(form1.handle,pchar('cannot terminate'+itemfocused.caption+'!')
,'Operation failed',mb_ok+MB_IC ONERROR )
else
Begin
Selected.Delete;
lvAboutProc.Items.Clear;
lblCountSysProc.caption:=inttostr(lvSysProc.items.count);
lblCountAboutProc.caption:='';
end
end;
end;
end;
end;
procedure TForm1.lvSysProcClick(Sender: T Object );
var
newitem2:Tlistitem;
clp:bool;
Begin
if lvSysProc.selected<>nil then
Begin
idid:=strtoint('$'+lvSysProc.itemfocused.subitems[0]);
lvAboutProc.items.Clear;
fshandle:=CreateToolhelp32Snapshot(th32cs_snapmodule,idid);
fm32.dwsize:=sizeof(fm32);
clp:=Module32First(fshandle,fm32);
while integer(clp)<>0 do
Begin
newitem2:=lvAboutProc.Items.add;
with newitem2 do
Begin
caption:=fm32.szeexepath;
with newitem2.subitems do
Begin
add(IntToHex(fm32.th32moduleid,4));
add(IntToHex(fm32.GlblcntUsage,4));
add(IntToHex(fm32.proccntUsage,4));
end;
end;
clp:=Module32Next(fshandle,fm32);
end;
closehandle(fshandle);
lblCountAboutProc.Caption:=IntToStr(lvAboutProc.items.count);
end
end;
procedure TForm1.FormCreate(Sender: T Object );
Begin
with application do
Begin
showwindow(handle,SW_HIDE); //Hide the task button on the taskbar
OnMinimize:=AppOnMinimize; //It is automatically hidden when minimized
OnDeactivate:=FormDeactivate; // automatically hide when inactive
OnActivate:=btnRefreshClick;
end;
RegisterServiceProcess(GetcurrentProcessID,1); //Register the program as a system service program to avoid appearing in the task list
with SysTrayIcon do
Begin
cbSize:=sizeof(SysTrayIcon);
wnd:=Handle;
uID:=SYSTRAY_ID;
uFlags:=NIF_ICON OR NIF_MESSAGE OR NIF_Tip;
uCallBackMessage:=WM_SYSTRAYMSG;
hIcon:=Application.Icon.Handle;
szTip:='System process monitoring';
end;
Shell_NotifyIcon(NIM_ADD,@SysTrayIcon); //Add the program icon to the system tray area
with lvSysProc do
Begin
SmallImages:=TImageList.CreateSize(16,16);
SmallImages.ShareImages:=True;
end;
FormOldWidth:=self.Width;
FormOldHeight:=self.Height;
end;
//It is automatically hidden when minimized
procedure Tform1.AppOnMinimize(Sender:T Object );
Begin
ShowWindow(application.handle,SW_HIDE);
end;
//Response the mouse to click on the system tray area icon
procedure tform1.SysTray OnClick (var message:TMessage);
Begin
with message do
Begin
if (lparam=wm_lbuttondown) or (lparam=wm_rbuttondown) then
Begin
application.restore;
SetForegroundWindow(Handle);
showwindow(application.handle,SW_HIDE);
end;
end;
end;
procedure TForm1.FormClose(Sender: T Object ; var Action: TCloseAction);
Begin
Shell_NotifyIcon(NIM_DELETE,@SysTrayIcon); //Cancel the system tray area icon
RegisterServiceProcess(GetcurrentProcessID,0); //Cancel the registration of the system service program
lvSysProc.SmallImages.Free;
end;
//It will automatically hide when inactive
procedure TForm1.FormDeactivate(Sender: T Object );
Begin
application.minimize;
end;
procedure TForm1.lblEmailClick(Sender: T Object );
Begin
if ShellExecute(Handle,'Open',Pchar('Mailto:[email protected]'),nil,nil,SW_SHOW)<33 then
MessageBox(form1.Handle,'Cannot start the email software!','I'm sorry',MB_ICONINFORMATION+MB_OK);
end;
//Adjust the position of each component when the form size changes
procedure TForm1.FormResize(Sender: T Object );
Begin
with panel1 do top:=top+self.Height-FormOldHeight;
with lvSysProc do
Begin
width:=width+self.Width-FormOldWidth;
end;
with lvAboutProc do
Begin
height:=height+self.Height-FormOldHeight;
width:=width+self.Width-FormOldWidth;
end;
FormOldWidth:=self.Width;
FormOldHeight:=self.Height;
end;
end.
The above programs can be compiled and run normally in Delphi 2, Windows 95 Chinese version, Delphi 5, and Windows 97 Chinese version. If you have any questions, please email to:[email protected] to discuss with me.
postscript:
In the above code, RegisterServiceProcess() is an unpublished API function that only exists in win 9x.
After learning masm32, I rewritten and improved this program with masm32
Interested friends can download the latest version:
http://www.hcny.gov.cn/netres/download/procview.rar