c++ - 运行 Windows 程序并检测它何时以 C++ 结束

标签 c++ windows winapi process

假设我运行了一个应用程序,一段时间后这个应用程序将被用户关闭。是否有可能找出程序何时退出?我可以在运行该应用程序时获取它的进程 ID 吗?

最佳答案

这是来自 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();
}

关于c++ - 运行 Windows 程序并检测它何时以 C++ 结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1846385/

相关文章:

c++ - 可以同时容纳Foo *和std::shared_ptr <Foo>的类型

C++如何获取文件最近更改的时间戳?

windows - 使用批处理文件从不同时区获取本地时间

c++ - 如何将屏幕截图写入 PNG?

windows - 如何通过 MS-DOS 命令打印当前目录名称

c++ - 有什么理由更喜欢 memset/ZeroMemory 而不是 WinAPI 结构的值初始化?

c++ - 基于函数重载的程序

c++ - Google Colab中的CHECK(call)函数获取错误

c++ - 不再需要在子类化后恢复 GWLP_USERDATA

CreateProcess 没有按应有的方式运行应用程序(nc.exe)?