如何获取我的可执行文件的运行状态 [英] how to get the running status of my executable

查看:56
本文介绍了如何获取我的可执行文件的运行状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

其实我有两个可执行文件。如果第一个被打破,一些指示将显示在第二个可执行文件中,如红色按钮,如果第一个正在运行,它会在第二个exe中显示绿色按钮。如何才能获得我的可执行文件的运行状态。

Actually i have two executable. if first one is breaked, some indication is to be shown in the second executable like red button and if first one is running, it shows green button in the second exe. how it will be possible to get the running status of my executable.

推荐答案

如果你的程序是first.exe,你可以自由修改,你可以使用互斥。你可以在你的first.exe中创建一个命名互斥锁,并在你的second.exe中定期检查这个互斥锁。



否则,你需要使用 CreateToolhelp32Snapshot API。这是一个小例子。



If first.exe is your program that you have developed and can freely modify, you could use Mutex. You would create a Named Mutex in your first.exe and check periodically for this mutex in your second.exe.

Otherwise, you need to use CreateToolhelp32Snapshot API. Here is a small example.

// Take a snapshot of all processes in the system.
HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if(hProcessSnap == INVALID_HANDLE_VALUE)
  return FALSE;

// Set the size of the structure before using it.
PROCESSENTRY32 pe32;
pe32.dwSize = sizeof(PROCESSENTRY32);

// Retrieve information about the first process, and exit if unsuccessful
if(!Process32First(hProcessSnap, &pe32))
{
  CloseHandle(hProcessSnap);// Must clean up the snapshot object!
  return FALSE;
}
BOOL found = FALSE;

// Now walk the snapshot of processes
do
{
  if(!_tcsicmp(pe32.szExeFile, _T("FIRST.EXE")))
  {
    version = _T("MD3");
    found = TRUE;
    break;
  }
} while(Process32Next(hProcessSnap, &pe32));
CloseHandle(hProcessSnap);
return found;


这篇关于如何获取我的可执行文件的运行状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆