Running a Windows program and detecting when it ends in C ++

Suppose I run an application, after some time this application will be closed by the user. Can I find out when the program will be released? Can I get the process ID when running this application?

+3
source share
7 answers

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();
}
+9
source

CreateProcess WaitForSingleObject - : CreateProcess, WFSO, . ... . , - - . ?

- , , . SendMessage (HWND_BROADCAST HWND_TOPMOST), SendMessage , , .... , .... DEADLOCK.

, , , , SendMessageTimeout, SendMessage (, DDE, - ). , SendMessage, , , , API SetSysColors.

: (a) Wait , (b) - Wait PeekMessage , , , (c) MsgWaitForMultipleObjects API WaitForSingleObject.

+2
+1
+1

, , . , PID, :

 HANDLE h = OpenProcess(SYNCHRONIZE, TRUE, pid);
 WaitForSingleObject(h, INFINITE );

: https://gist.github.com/rdp/6b5fc8993089ee12b44d ( , , ).

+1

google msdn, :

CreateProcess GetExitCodeProcess. , .

, . , .

0

, Windows (?) Runtime Library. . C > .

, _ spawnlp (_P_WAIT, cmdname, arg0, ..., argn, 0) ( " spawn l ist, p ath " ), cmdname PATH . : " argn NULL, ".

.

#include <stdio.h>
#include <process.h>
int main() {
    puts("waiting");
    _spawnlp(_P_WAIT, "mspaint.exe", "mspaint.exe", 0);
    puts("returned");
}
0

Source: https://habr.com/ru/post/1724730/


All Articles