C++:验证 Windows 应用程序是否打开? [英] C++: Verify a Windows application is open?
本文介绍了C++:验证 Windows 应用程序是否打开?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 C++ 程序,它涉及打开一个应用程序(比如calculator.exe).我需要能够进行测试以确保此应用程序已打开.这是我的意思的一个例子.
I have a C++ program that involves opening an application (let's say calculator.exe). I need to be able to test to make sure this application is open. Here's an example of what I mean.
#include <iostream.h>
int main()
{
system("start calculator");
if (something)
cout << "Calculator is running.\n";
cin.get();
return 0;
}
我需要为 something
输入什么来测试计算器是否打开?
What would I need to put in for something
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屋!
查看全文