The MASM Forum

General => The Campus => Topic started by: mabdelouahab on October 24, 2014, 08:48:31 AM

Title: Process End
Post by: mabdelouahab on October 24, 2014, 08:48:31 AM
Hi there guys
For special reasons, I've been away from the Forum
But I came back quickly
And now I have a question

how to i can trace the event of (Current Process End or Terminate),If possible, and if it exists, like WM_CLOSE or WM_DESTROY in window
I mean, in any type of programs, Console - Win32App ...

Any idea please

+ I'm sorry for my English
Title: Re: Process End
Post by: TouEnMasm on October 24, 2014, 05:44:46 PM
a debugger is the perfect tool to do that (windbg can read the dll code).
Title: Re: Process End
Post by: mabdelouahab on October 24, 2014, 07:32:42 PM
hi ToutEnMasm;
To clarify, I want a way to impose a program that invoke  'ProcEndp'  when it ends

include masm32rt.inc
;.............................
.code
ProcEndp PROC
;.............................
ret
ProcEndp endp

start:   
;.............................
end start



How can I impose that the procedure 'ProcEndp' will be invoked  when the Process is closed by windows  or when it is terminated
I want to make the Event END related by Invoke  'ProcEndp'
Title: Re: Process End
Post by: mabdelouahab on October 24, 2014, 07:40:49 PM
Is there a particular EVENT when the expiration or termination of the program
Title: Re: Process End
Post by: dedndave on October 24, 2014, 11:13:35 PM
for GUI programs, there is a system of messages

http://masm32.com/board/index.php?topic=1491.msg15340#msg15340 (http://masm32.com/board/index.php?topic=1491.msg15340#msg15340)

for console mode programs, you can use SetConsoleCtrlHandler to set an event handler

http://msdn.microsoft.com/en-us/library/windows/desktop/ms686016%28v=vs.85%29.aspx (http://msdn.microsoft.com/en-us/library/windows/desktop/ms686016%28v=vs.85%29.aspx)

you could also use GetConsoleWindow to get a handle and subclass WndProc - then use the GUI methods

http://msdn.microsoft.com/en-us/library/windows/desktop/ms683175%28v=vs.85%29.aspx (http://msdn.microsoft.com/en-us/library/windows/desktop/ms683175%28v=vs.85%29.aspx)
Title: Re: Process End
Post by: MichaelW on October 25, 2014, 06:03:16 PM
Or you can try the CRT  atexit (http://msdn.microsoft.com/en-us/library/tze57ck3.aspx) or  _onexit (http://msdn.microsoft.com/en-us/library/zk17ww08.aspx) functions. You'll need to run this code from a command prompt to see the output from the procedures.

;===============================================================================
include \masm32\include\masm32rt.inc
;===============================================================================
.data
.code
;=============================================================================== 
OnExit1 proc C
    printf("OnExit1\n")
    ret
OnExit1 endp
OnExit2 proc C
    printf("OnExit2\n")
    ret   
OnExit2 endp
AtExit1 proc C
    printf("AtExit1\n")
    ret
AtExit1 endp
AtExit2 proc C
    printf("AtExit2\n")
    ret
AtExit2 endp
;=============================================================================== 
start:
;===============================================================================
    ; _onexit returns a pointer to the function for success, or zero for error.
    ; atexit returns zero for success, or non-zero for error.
    invoke crt__onexit, OnExit1
    printf("%d\n",eax)
    invoke crt__onexit, OnExit2
    printf("%d\n",eax)
    invoke crt_atexit, AtExit1
    printf("%d\n",eax)
    invoke crt_atexit, AtExit2
    printf("%d\n",eax)   
    inkey
    exit
end start
;===============================================================================


And I get no output if I terminate the app from Task Manager.
Title: Re: Process End
Post by: Magnum on October 25, 2014, 07:06:30 PM
Welcome back to the forum. :-)
Title: Re: Process End
Post by: sinsi on October 25, 2014, 07:22:47 PM
You control your program, so in your message loop exit you call EndProc, then ExitProcess.
If someone else uses TerminateProcess (http://msdn.microsoft.com/en-us/library/windows/desktop/ms686714(v=vs.85).aspx), you don't get any notifications
QuoteA process cannot prevent itself from being terminated.
Title: Re: Process End
Post by: mabdelouahab on October 26, 2014, 05:23:16 AM
I thank you all
I thank you MichaelW, That's exactly what I 'm looking for, I will try if it works