简体   繁体   English

如何查看Windows上是否还有其他进程正在运行?

[英]How do I see if another process is running on windows?

I have a VC++ console app and I need to check to see if another process is running. 我有一个VC ++控制台应用程序,我需要检查是否正在运行另一个进程。 I don't have the window title, all I have is the executable name. 我没有窗口标题,我只有可执行文件名称。 How do I get the process handle / PID for it? 如何获取进程句柄/ PID? Can I enumerate the processes running with this .exe ? 我可以枚举使用此.exe运行的进程吗?

Use the CreateToolhelp32Snapshot Function 使用CreateToolhelp32Snapshot功能

hSnapShot = FCreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);

Followed by Process32First and Process32Next . 随后是Process32FirstProcess32Next

You will get a PROCESSENTRY32 struct as follows with an szExeFile member. 您将使用szExeFile成员获得如下的PROCESSENTRY32结构。

PROCESSENTRY32W    processInfo;
processInfo.szExeFile

Make sure to first acquire the privilege SeDebugPrivilege before enumerating, that way you will get all processes across all sessions and users. 确保在枚举之前首先获得SeDebugPrivilege权限,这样您将获得所有会话和用户的所有进程。

To acquire the privilege so you get all sessions: 要获得权限,您将获得所有会话:

acquirePrivilegeByName(SE_DEBUG_NAME);// SeDebugPrivilege

Where acquirePrivilegeByName is defined as: 其中acquirePrivilegeByName定义为:

BOOL acquirePrivilegeByName(
                            const TCHAR     *szPrivilegeName)
{
    HANDLE          htoken;
    TOKEN_PRIVILEGES    tkp;
    DWORD           dwerr;

    //---------------- adjust process token privileges to grant privilege
    if (szPrivilegeName == NULL)
    {
        SetLastError(ERROR_INVALID_PARAMETER);
        return FALSE;
    }

    if (!LookupPrivilegeValue(NULL, szPrivilegeName, &(tkp.Privileges[0].Luid)))
        return FALSE;

    tkp.PrivilegeCount = 1;
    tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;

    if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &htoken))
        return FALSE;

    if (!AdjustTokenPrivileges(htoken, FALSE, &tkp, 0, NULL, NULL) ||
        GetLastError() != ERROR_SUCCESS)    // may equal ERROR_NOT_ALL_ASSIGNED
    {
        dwerr = GetLastError();
        CloseHandle(htoken);
        SetLastError(dwerr);
        return FALSE;
    }

    CloseHandle(htoken);
    SetLastError(ERROR_SUCCESS);

    return TRUE;
} //acquirePrivilegeByName()

If you need the full process image name you can use QueryFullProcessImageName , but the szExeFile member may be enough for your needs. 如果需要完整的进程映像名称,可以使用QueryFullProcessImageName ,但szExeFile成员可能足以满足您的需要。

You can use EnumProcesses to enumerate the processes on a system. 您可以使用EnumProcesses枚举系统上的进程。

You'll need to use OpenProcess to get a process handle, then QueryFullProcessImageName to get the processes executable. 您需要使用OpenProcess来获取进程句柄,然后使用QueryFullProcessImageName来获取进程可执行文件。

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

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