有没有办法来验证一个程序是从一个C ++程序打开它试图打开它(Windows)? [英] Is there a way to verify a program is open from a C++ program that tries to open it (Windows)?
本文介绍了有没有办法来验证一个程序是从一个C ++程序打开它试图打开它(Windows)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个C ++程序涉及打开一个程序(我们说calculator.exe)。我需要能够测试,以确保这个程序是开放的。这里是我的意思的一个例子。
I have a C++ program that involves opening a program (let's say calculator.exe). I need to be able to test to make sure this program is open. Here's an example of what I mean.
#include <iostream.h>
int main()
{
system("start calculator");
if (xxxxx)
cout << "Calculator is running.\n";
cin.get();
return 0;
}
我需要输入什么来测试计算器是否打开?
What would I need to put in for xxxxx that tests whether the calculator is open?
推荐答案
您可以通过进程名称找到它,或者您可以通过窗口标题找到它。
You can find it by process name, or you can find it by window title.
#include <iostream>
#include <windows.h>
#include <TlHelp32.h>
DWORD find_by_process_name(const wchar_t* process_name)
{
DWORD pid = 0;
HANDLE hndl = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS | TH32CS_SNAPMODULE, 0);
if (hndl)
{
PROCESSENTRY32 process = { sizeof(PROCESSENTRY32) };
Process32First(hndl, &process);
do
{
if (_wcsicmp(process.szExeFile, process_name) == 0)
{
pid = process.th32ProcessID;
break;
}
} while (Process32Next(hndl, &process));
CloseHandle(hndl);
}
return pid;
}
int main()
{
ShellExecuteA(0, "open", "calc.exe", 0, 0, SW_SHOWNORMAL);
if (find_by_process_name(L"calc.exe"))
std::cout << "calculator is running\n";
return 0;
}
这篇关于有没有办法来验证一个程序是从一个C ++程序打开它试图打开它(Windows)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文