Automatic resurrection abnormal termination process

xiaoxiao2021-03-05  35

Automatic resurrection abnormal termination process 01-6-26 at 10:09 am: 05

Problem

Microsoft's Windows operating system has been deeply in the field, and Windows-based applications are endless. But people have more or less have such an experience, and the application running on Windows will terminate. At this time, it is generally re-started. At this point, if the computer is unattended, the abnormal termination process may affect normal work. When developing the GPS Global Satellite Positioning System Control Center program, the author encounters the abnormal termination of the control center program. This article describes a method that can automatically resurrect this abnormal term.

Realization principle

Typically, a process for one running instance of an application is called, which can also contain multiple possible paths in one process, each execution path called a thread, and one process contains at least one main thread. The main thread is responsible for performing the startup code. Alternatively, a process can create a number of sub-processes. When the process is created, the system automatically generates the main thread and then the main thread can create more threads. Therefore, we can write a program that created, promoting the child process, and monitors the running of the process, and then recreate and prompt the child process immediately when it appears anomally.

Main function

1. Create a child process

Bool CreateProcess (LPCTSTR LPApplicationName,

LPTSTR LPCOMMANDLINE,

LPSecurity_attributes LPPROCESSATTRIBUTES,

LPSecurity_attributes LPTHREADATIADATTRIBUTES,

Bool binherithandles,

DWORD DWCREATIONFLAGS,

LPVOID LPENVIRONMENT,

LPCTSTR LPCURRENTDIRECTORY,

LPStartupinfo LPstartupinfo,

LPPROCESS_INFORMATION LPPROCESSINFORMATION);

Parameter Description:

LPApplicationName: The name of the executable of the new process will be used, including the extension;

LPCommandline: The command line of the new process, if lpapplicationname is null, then its first parameter is the name of the executable file that the new process will use, can not include the extension, the system is default .exe file;

LPRocessAttributes and LPTHREADATTRIBUTES: Safety attributes specified for process objects and thread objects, respectively;

BINHERITHANDLES: Specifies whether the process inherits the handle of its parent process;

DWCREATIONFLAGS: Specifies the logo of the new process generation method, and can be connected with a logical operator or in different ways;

lpenvironment: Points a memory that contains the environment block string that will be used by the new process, generally null;

LPCurrentDirectory: Sets the current drive and working directory of the sub-process, when null, the child process inherits the current drive and work directory of the Parent process;

lpstartupinfo: Points to the StartupInfo structure, the general child process uses the default value, but puts all members in this structure into 0, and sets CB as a structure size;

LPPROCESSINFORMATION: This parameter points to the LPPROCESS_INFORMATION structure, CreateProcess Fill in the relevant sub-process before returning, the parent process is using the information to monitor the child process.

2. Sub process termination detection function

GetExitcodeProcess (Handle HProcess, LPDWORD LPEXITCODE); where HProcess is a process handle, LPEXITCode is an exit code when the process terminates. If a process is not terminated, the return value of LPEXITCODE is STILL_ACTIVE, otherwise it returns other values.

Program implementation

In this paper, the specific implementation steps of this method are introduced in this paper.

1. Create a new project

Place a MEMO component and an OK button component on the form FORM1, change the CAPTION attribute of the OK button component to createProcess. Place a TIMER component, set the interval value of the Timer component to 1000, that is, check whether the process is terminated every second.

2. Description process and variables

PROCEDURE ESTABLISHPROCESS;

PiProcinfogps: Process_information;

3. Write the implementation code of the EstablishProcess process

PROCEDURE ESTABLISHPROCESS;

VAR

Sistartupinfo: startupinfo;

Saprocess, Sathread: Security_Attribute;

Fsuccess: boolean;

Begin

Fsuccess: = FALSE;

ZeromeMory (@ sistartupinfo, sizeof (sistartupinfo);

Sistartupinfo.cb: = sizeof (sistartupinfo);

Saprocess.nlength: = sizeof (saprocess);

Saprocess.lpsecurityDescriptor: = pchar (nil);

Saprocess.binherithandle: = true;

SATHREAD.NLENGTH: = SIZEOF (Sathread);

Sathread.lpsecurityDescriptor: = pchar (nil);

Sathread.binherithandle: = true;

Fsuccess: = CreateProcess (Pchar (NIL), 'C: / SR350 / SR350BUFF', @ saprocess, @ Sathread, False, Create_Default_ERROR_MODE, PCHAR (NIL), PCHAR (NIL), SISTARTUPINFO, PIPROCINFOGPS

IF (not fsuccess) THEN

Form1.Memo1.Lines.Add ('Create Process SR350Buff Fail.')

Else

Form1.Memo1.Lines.Add ('Create Process SR350Buff Success.')

END;

4. Call the process in the onClick event of the CreateProcess button

ESTABLISHPROCESS;

5. Write code for Timer1 Ontimer events

Procedure TFORM1.TIMER1TIMER (Sender: TOBJECT);

VAR

DWEXITCODE: DWORD;

FPROCESSEXIT: BOOLEAN;

Begin

DWEXITCODE: = 0;

FPROCESSEXIT: = FALSE;

FProcessexit: = getExitcodeProcess

(piprocinfogps.hprocess, dwexitcode);

IF (fProcessexit and (dwexitcode <>

STILL_ACTIVE)) THEN

Begin

Memo1.Lines.Add ('sr350buff.exe process termination ");

CloseHandle (piprocinfogps.hthread);

CloseHandle (Piprocinfogps.hprocess); EstablishProcess;

END;

END;

6. Compile the link to run the program

After running the program, you can see that C: /SR350/sr350buff.exe is started, and if the SR350Buff.exe process is killed, SR350Buff.exe automatically starts. Note: This article sets the executable file name c: /SR350/sr350buff.exe, so the C: disk / sr350 directory requires an SR350BUFF.EXE file.

转载请注明原文地址:https://www.9cbs.com/read-32440.html

New Post(0)