void signalHandler( int signum )
{
cout << "Interrupt signal (" << signum << ") received.\n";
// cleanup and close up stuff here
// terminate program
exit(signum);
}
int main ()
{
// register signal SIGINT and signal handler
signal(SIGINT, signalHandler);
while(1){
cout << "Going to sleep...." << endl;
sleep(1);
}
return 0;
}
This is a example when the console window is printing " Going to sleep " if you press "Ctrl + C" the process will go to signalHandler(). So i mean that when i end this process in the task manager, my app will go to signalHandler() before exit. Could you show me a solution
But there is a crude way to do this by hooking NtTerminateProcess() which will allow you to modify the function to prevent your process to be closed. But this is very Dangerous and Un needed. Unless you are developing a Security Application there, is no need for this. Maximum you can do is by using ACLS & DACLS.
// Start the child process p1.exe. Make sure p1.exe is in the
// same folder as current application. Otherwise write the full path in first argument.
if(!CreateProcess(L".\\p1.exe", NULL, NULL, NULL, FALSE, 0, NULL, NULL, &sj, &pj))
{
printf( "Hello CreateProcess failed (%d)\n", GetLastError() );
getch();
return;
}
// Start child process p2.exe. Make sure p2.exe is in the
// same folder as current application. Otherwise write the full path in first argument.
if(!CreateProcess(L".\\p2.exe", NULL, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
{
printf( "CreateProcess2 failed (%d)\n", GetLastError() );
getch();
return;
}
// Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
CloseHandle( pj.hProcess );
CloseHandle( pj.hThread );
printf( "Close p1 & p2" );
getch();
}
this my code using second process. But I mean, when process 1 running after time task manager end it, so how to it know being ended by itself.
Only use win32 API
not use .net framework
No, there is no way to know when YOUR PROCESS is being terminated if task manager uses TerminateProcess() API (this is done in case of "force kill option").