简体   繁体   English

如何确定.exe是否在c ++中运行?

[英]How do I find out if a .exe is running in c++?

如何在给定进程名称的Windows上运行可执行文件,例如program.exe?

The C++ standard library has no such support. C ++标准库没有这样的支持。 You need an operating system API to do this. 您需要操作系统API才能执行此操作。 If this is Windows then you'd use CreateToolhelp32Snapshot(), followed by Process32First and Process32Next to iterate the running processes. 如果这是Windows,那么您将使用CreateToolhelp32Snapshot(),然后使用Process32First和Process32Next来迭代正在运行的进程。 Beware of the inevitable race condition, the process could have exited by the time you found it. 要注意不可避免的竞争条件,这个过程可能会在你找到它的时候退出。

I just created one using Hans suggestion. 我刚用汉斯的建议创建了一个。 Works like a champ! 像冠军一样工作!

Oh and here is the basic code. 哦,这是基本代码。

Please you will have to add CStrings sAppPath and sAppName. 请您必须添加CStrings sAppPath和sAppName。

StartProcess is a small function that uses CreateProcess and returns the PID(not used here). StartProcess是一个小函数,它使用CreateProcess并返回PID(这里不使用)。 You will need to replace it. 你需要更换它。

This is not a complete program, just the code to find if the program is running using Hans suggestion. 这不是一个完整的程序,只是用于查找程序是否使用Hans建议运行的代码。 A fun test is to set the path to c:\\windows\\ and the app to notepad.exe and set it for 10 seconds. 一个有趣的测试是将路径设置为c:\\ windows \\,将应用程序设置为notepad.exe并将其设置为10秒。

#include <tlhelp32.h>
PROCESSENTRY32 pe32 = {0}; 
HANDLE    hSnap;
int       iDone;
int       iTime = 60;
bool      bProcessFound;

while(true)    // go forever
{
    hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
    pe32.dwSize = sizeof(PROCESSENTRY32); 
    Process32First(hSnap,&pe32);     // Can throw away, never an actual app

    bProcessFound = false;   //init values
    iDone = 1;

    while(iDone)    // go until out of Processes
    {
        iDone = Process32Next(hSnap,&pe32);
        if (strcmp(pe32.szExeFile,sAppName) == 0)    // Did we find our process?
        {
            bProcessFound = true;
            iDone = 0;
        }
    }

    if(!bProcessFound)    // if we didn't find it running...
    {
        startProcess(sAppPath+sAppName,"");             // start it
    }
    Sleep(iTime*1000);    // delay x amount of seconds.
}

Assumptions: since you mention '.exe', you want this for some flavor of Windows. 假设:既然你提到了'.exe',那么你想要一些Windows的味道。 You want to write a program in C++ to determine whether a program with a particular executable name is running (regardless of the language used to implement the target program). 您希望用C ++编写程序来确定具有特定可执行文件名的程序是否正在运行(无论用于实现目标程序的语言如何)。

Enumerate the running processes using either the Toolhelp API or the process status API. 使用Toolhelp API或进程状态API枚举正在运行的进程。 Compare the name of the executable for each running process to the one you're looking for (and be aware that there may be more than one process with that executable name). 将每个正在运行的进程的可执行文件的名称与您正在查找的进程的名称进行比较(并注意可能有多个具有该可执行文件名的进程)。

hProcessInfo = OpenProcess( PROCESS_ALL_ACCESS, FALSE, pe32.th32ProcessID );

            do{
                if(strcmp(pe32.szExeFile,"process.exe") == 0)
                {
                    processfound = true;
                    break;
                }
}while( Process32Next( hProcessSnap, &pe32 ) );

If you don't want to get process detail from code just press Ctrl+Alt+Del and check process list. 如果您不想从代码中获取流程详细信息,只需按Ctrl + Alt + Del并检查流程列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM