按名称获取 C 中的进程 ID [英] Get a process ID in C by name

查看:34
本文介绍了按名称获取 C 中的进程 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过进程名称(例如,notepad.exe)获取进程 ID,但 StackOverflow 上的先前解决方案似乎无法正常工作.这是我尝试过的:

I'm trying to get a process ID by a process name (for example, notepad.exe), but previous solutions on Stack Overflow don't seem to work properly. Here is what I've tried:

DWORD FindProcessId(const char *processname)
{
    HANDLE hProcessSnap;
    PROCESSENTRY32 pe32;
    DWORD result = NULL;

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

    // Retrieve information about the first process,
    // and exit if unsuccessful
    if (!Process32First(hProcessSnap, &pe32))
    {
        CloseHandle(hProcessSnap);          // Clean the snapshot object
        return(FALSE);
    }

    do
    {
        if (0 == _stricmp(processname, pe32.szExeFile))
        {
            result = pe32.th32ProcessID;
            break;
        }
    } while (Process32Next(hProcessSnap, &pe32));

    CloseHandle(hProcessSnap);

    return result;
}

我传入notepad.exe"并确认它正在我的系统上运行,并且该应用程序以具有所需正确权限的管理员身份运行.高程是这样完成的:

I pass in "notepad.exe" and confirm that it is running on my system, and that the application is running as Administrator with the correct privileges required. Elevation is done like this:

        if (GetModuleFileName(NULL, szPath, ARRAYSIZE(szPath)))
        {
            // Launch itself as administrator.
            sei.lpVerb = TEXT("runas");
            sei.lpFile = szPath;
            sei.hwnd = NULL;
            sei.nShow = SW_NORMAL;

            if (!ShellExecuteEx(&sei))
            {
                MessageBox(NULL, TEXT("The program needs to be elevated to work properly."), APP_TITLE, MB_OK);
                return -1;
            }
        }
        return 0;

它永远找不到进程 ID - 每次都返回 Null.

It never finds the process ID - returns Null every time.

这里使用的是 C,而不是 C++.

This is using C, not C++.

推荐答案

解决方案是在获取进程快照后简单地设置pe32.dwSize.在此处完成固定代码:

The solution is to simply set the pe32.dwSize after getting the process snapshot. Complete fixed code here:

DWORD FindProcessId(const char *processname)
{
    HANDLE hProcessSnap;
    PROCESSENTRY32 pe32;
    DWORD result = NULL;

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

    pe32.dwSize = sizeof(PROCESSENTRY32); // <----- IMPORTANT

    // Retrieve information about the first process,
    // and exit if unsuccessful
    if (!Process32First(hProcessSnap, &pe32))
    {
        CloseHandle(hProcessSnap);          // clean the snapshot object
        printf("!!! Failed to gather information on system processes! \n");
        return(NULL);
    }

    do
    {
        printf("Checking process %ls\n", pe32.szExeFile);
        if (0 == strcmp(processname, pe32.szExeFile))
        {
            result = pe32.th32ProcessID;
            break;
        }
    } while (Process32Next(hProcessSnap, &pe32));

    CloseHandle(hProcessSnap);

    return result;
}

这篇关于按名称获取 C 中的进程 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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