Running a Windows program and detect when it ends with C++
This is a quote from here:
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#include <conio.h>
void _tmain( int argc, TCHAR *argv[] )
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
STARTUPINFO sj;
PROCESS_INFORMATION pj;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
ZeroMemory( &sj, sizeof(sj) );
sj.cb = sizeof(sj);
ZeroMemory( &pj, sizeof(pj) );
// 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;
}
// Wait until child processes exit.
WaitForSingleObject( pi.hProcess, INFINITE );
WaitForSingleObject( pj.hProcess, INFINITE );
// Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
CloseHandle( pj.hProcess );
CloseHandle( pj.hThread );
getch();
}
detect program termination (C, Windows)
1. Win32
The Win32 API contains a way to do this via the SetUnhandledExceptionFilter function, as follows:
LONG myFunc(LPEXCEPTION_POINTERS p)
{
printf("Exception!!!\n");
return EXCEPTION_EXECUTE_HANDLER;
}
int main()
{
SetUnhandledExceptionFilter((LPTOP_LEVEL_EXCEPTION_FILTER)&myFunc);
// generate an exception !
int x = 0;
int y = 1/x;
return 0;
}
2. POSIX/Linux
I usually do this via the signal() function and then handle the SIGSEGV signal appropriately. You can also handle the SIGTERM signal and SIGINT, but not SIGKILL (by design). You can use strace() to get a backtrace to see what caused the signal.
How to detect a process start & end using c# in windows?
To do this without polling requires WMI. This is well supported in .net and you can use the ManagementEventWatcher
class to subscribe to WMI notifications.
This Code Project article illustrates how it is done. Here's an extract showing how straightforward it is.
notePad = new ProcessInfo("notepad.exe");
notePad.Started +=
new Win32Process.ProcessInfo.StartedEventHandler(this.NotepadStarted);
notePad.Terminated +=
new Win32Process.ProcessInfo.TerminatedEventHandler(this.NotepadTerminated);
Note that ProcessInfo
is a class implemented in the code attached to that article.
C: Doing something when the program exits
look into the atexit
API of the C standard library.
Detect Windows or Linux in C, C++
It's generally done like this (more or less):
#ifdef _WIN32
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#define DIV 1048576
#define WIDTH 7
#endif
#ifdef linux
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#endif
int main(int argc, char *argv[])
{
#ifdef _WIN32
MEMORYSTATUSEX statex;
statex.dwLength = sizeof (statex);
GlobalMemoryStatusEx (&statex);
_tprintf (TEXT("There is %*ld %% of memory in use.\n"),
WIDTH, statex.dwMemoryLoad);
#endif
#ifdef linux
char cmd[30];
int flag = 0;
FILE *fp;
char line[130];
int TotalMem, TotalFree, TotalUsed;
flag=0;
memcpy (cmd,"\0",30);
sprintf(cmd,"free -t -m|grep Total");
fp = popen(cmd, "r");
while ( fgets( line, sizeof line, fp))
{
flag++;
sscanf(line,"%*s %d %d %d",&TotalMem, &TotalUsed, &TotalFree);
}
pclose(fp);
if(flag)
printf("TotalMem:%d -- TotalUsed:%d -- TotalFree:%d\n",TotalMem,TotalUsed,TotalFree);
else
printf("not found\n");
#endif
return 0;
}
This way, only code for linux will be compiled while on a linux platform, and only windows code will be compiled on a windows platform.
Related Topics
How Could I Sensibly Overload Placement Operator New
Issue Using Visual Studio 2010 Compiled C++ Dll in Windows 2000
How to Get Vendor Id and Product Id of a Plugged Usb Device on Windows
Breaking Readfile() Blocking - Named Pipe (Windows API)
Constructor with By-Value Parameter & Noexcept
Redirecting Function Output to /Dev/Null
Pass by Reference More Expensive Than Pass by Value
G++ Can't Find Boost Libraries. I Say They'Re in Plain Sight
Why Was the Restriction on the Comma Operator Being in a Constant Expression Removed in C++11
How to Suppress Specific Warnings in G++
Off-The-Shelf C++ Hex Dump Code
Boost Interprocess Mutexes and Checking for Abandonment
Cocreateinstance Returning E_Nointerface Even Though Interface Is Found
Why Default Return Value of Main Is 0 and Not Exit_Success
Thread Safety of Matlab Engine API
What Is the Significance of 'Strongly Happens Before' Compared to '(Simply) Happens Before'