|
How to execute some code when process terminated normal or abnormal?
Answer:
WinAPI (Win32) specific only.
If you close the window from Taskmanager's "End Task", then you get the WM_CLOSE notification.
The WM_CLOSE message is sent as a signal that a window or an application should terminate.
BUT
If you click on "End Process" from task manager, then there's no way to handle it, in .NET or Win32.
You better hope that the end user clicks on Taskmanager's "End Task" and not on "End Process" .
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_CLOSE:
MessageBox(NULL, "closed the window from Taskmanager!", "abnormal exit" , MB_OK);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
modified on Friday, April 30, 2010 11:01 AM
|
|
|
|
|
Hey, I know how to execute some code when application closing in normal case. I'm not novice, and know how Windows messages system is working. The question is more advanced. What if for example, something go wrong, application freezing - and user terminates it using task manager, terminates it dirty? I need to do some code here.
Probably this is impossible, but look at the answer Michael Godfroid gave me above. This seems to be what I look for. So I'm going to investigate this.
|
|
|
|
|
Tried it.
Doesn't seem to work. I made a console app added a line to pause execution then terminated it from the
task manager. I didn't get the message.
|
|
|
|
|
Did you tried this in debugger? I guess with debugging it will not work.
Try to make infinite loop instead. I mean try following:
In main() make infinite loop.
In signal handler need to do something to identify we here, probably type to console (but this might not work).
Run application without debugging. It's important, cause things like this might behave different when debugger attached.
I didnt tried it myself yet, going to check this on monday.
|
|
|
|
|
Btw, here is from doc.
Note
SIGINT is not supported for any Win32 application. When a CTRL+C interrupt occurs, Win32 operating systems generate a new thread to specifically handle that interrupt. This can cause a single-thread application such as one in UNIX to become multithreaded, resulting in unexpected behavior.
So terminating from Ctrl+C will not work. Try to kill process from task manager.
|
|
|
|
|
I used this code in release mode and killed it from the task manager, while running it from the command line so as to catch the text. - It never came.
Not quite sure what SIGINT has to do with the price of chewing-gum in Czechoslovakia? We're not trying to interrupt the process, we're just terminating it.
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <tchar.h>
void SignalHandler(int signal)
{
printf("Application aborting...\n");
}
int main()
{
typedef void (*SignalHandlerPointer)(int);
SignalHandlerPointer previousHandler;
previousHandler = signal(SIGABRT, SignalHandler);
getch();
abort();
}
However, now that I can't find the page that I lifted the above code from I can't help but get the feeling that this particular approach is an exercise that won't succeed. I reckon that the signal is only thrown when the _application_ calls abort(), and not when windows calls abort(theApp).
I wonder if you'd have to hook TerminateProcess or something similar. I've a feeling that such a solution will require a fairly hard-core answer. Good-luck
|
|
|
|
|
For console:
CTRL_CLOSE_EVENT or the Console process termination message.
If you close the console from Taskmanager's "End Task", then you get the CTRL_CLOSE_EVENT notification.
The CTRL_CLOSE_EVENT message is sent as a signal that a console application should terminate.
BUT
If you click on "End Process" from task manager, then there's no way to handle it, in .NET or Win32.
The code below displays the message "Signal to quit was received" once you click on "End Task" from task manager.
<code></code>
#define STRICT 1
#include <windows.h>
#include <ctime>
#include <iostream>
using namespace std;
bool GlobalQuit = false;
BOOL WINAPI CtrlHandler(DWORD fdwCtrlType) {
switch (fdwCtrlType) {
case CTRL_C_EVENT:
Beep(1000, 1000);
case CTRL_CLOSE_EVENT:
case CTRL_BREAK_EVENT:
case CTRL_LOGOFF_EVENT:
case CTRL_SHUTDOWN_EVENT:
system("Color 1A");
cout << "Signal to quit was received\n";
GlobalQuit = true;
return TRUE;
default:
return FALSE;
}
}
int main(int argc, char *argv[], char *envp[]) {
struct tm *today;
time_t ltime;
char *p;
SetConsoleCtrlHandler(CtrlHandler, TRUE);
while (!GlobalQuit) {
time(<ime);
today = localtime(<ime);
p = asctime(today);
Sleep(2000);
}
return 0;
}
modified on Friday, April 30, 2010 12:01 PM
|
|
|
|
|
The OP Doesn't want to terminate a process, he wants to INTERCEPT process termination in order to do cleanup. Please stop confusing the issue.
|
|
|
|
|
That will only work if the app has a message loop, and is still pumping messages
|
|
|
|
|
When Task Manager uses TerminateProcess() to end a process, there is no notification. Also, there are no notifications generated for WH_CBT hook procedures.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
|
|
|
|
|
Can't really be done folks. Task manager sends a WM_CLOSE or CTRL_CLOSE_EVENT first, but only if you terminate from the applications tab. You'll be able to intercept that one with the SetConsoleCtrlHandler function. Otherwise task manager will fire a TerminateProcess, which cannot be intercepted ever.
Read it from the master himself here.[^]
|
|
|
|
|
Hi,
The task manager uses two methods of terminating a process depending on which tab has been selected. The tab labled 'Applications' will send a WM_CLOSE message to the window when 'End Task' has been selected which can easily be intercepted and handled.
The task manager tab labled 'Processes' utilizes TerminateProcess[^] which cannot be intercepted/blocked by the target process.
Some useful documentation from Microsoft Windows Internals Fourth Edition[^]:
Processes, Threads, and Jobs[^]
Best Wishes,
-David Delaune
|
|
|
|
|
Hook SSDT ,but I think it is pretty difficlt
|
|
|
|
|
You could try and hook the TerminateProcess API.
But API hooking is not at all possible/recommended these days.
|
|
|
|
|
how do i check whether my submenu item is checked
|
|
|
|
|
<pre>
MENUITEMINFO info;
info.cbSize = sizeof (MENUITEMINFO); // must fill up this field
info.fMask = MIIM_STATE; // get the state of the menu item
VERIFY(submenu->GetMenuItemInfo(ID_HELP_TEST, &info));
if( info.fState == MF_CHECKED))
//
else
//
</pre>
This code will help.
|
|
|
|
|
|
If you check if it is checked then it is checked since you check it.
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> Sometimes you just have to hate coding to do it well. <
|
|
|
|
|
|
I am having following code sample which uses fstream object to Read some buffered value and write the buffer at certain location in the another .cpp file. The code works perfectly i.e. It inserts the desired buffer at desired location. But if I try to open the output file to verify the logic, it opens like binary file(like dll) and not like normal CPP file in MSDEV. However it opens normaly in notepad. I am expecting it to open like normal CPP file in MSDEV.
fstream objFileStream(strFileName,(ios::out|ios::in|ios::nocreate),filebuf::sh_read);
while(objFileStream.getline(szBuffer,iCount,'\n')&&(bMoveToNextID==false))
{
strBuffer=szBuffer;
if((strBuffer.Find(strID))!=-1)
{
_tcscpy(szBuffer,strInsertText);
objFileStream.write(szBuffer,strInsertText.GetLength());
objFileStream.write("\n",3);
bMoveToNextID=true;
}
}
objFileStream.close();
modified on Friday, April 30, 2010 2:17 AM
|
|
|
|
|
varsha_vm wrote: fstream objFileStream(strFileName,(ios::out|ios::in|ios::nocreate|ios::binary),filebuf::sh_read);
You are using ios::binary, so of course the file is going to be opened in binary mode. Remove it to open in ASCII mode.
-Saurabh
|
|
|
|
|
Thanks for reply
But even if I remove the ios::binary, I am getting the same result. I don't have ios::binary in my original code. This is something I have tried when I was getting the undesired result. I have edited my original code sample.
|
|
|
|
|
varsha_vm wrote: objFileStream.write("\n",3);
This looks suspicious. Why 3?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
|
|
|
|
|
David
Thanks for pointing out the error. I have added it for another new line, but removing it now. Program is working fine.
Thanks again.
|
|
|
|
|
Hi! I have a problem about release build
I'm using Visual Studio 2005. The project is worked on MFC
When I build the project what I working in debug mode, It builds done successfully.
but in release mode, Output window shows next
1>Compiling resources...
1>Linking...
1>Generating code
and then.. it doesn't pass.
It seems like be stoped.
After 20 min ,I just canceld build.
It has been built well before.
I just added some files(.h .cpp) and resorces(.bmp), not special code
and it happened.
one more thing,
if I set 'Off' at configuration 'Warnning Level', it builds succesffully.
What's involved in that?
Do you have any idea about that?
please help me
|
|
|
|