VC++如何根据进程名获取进程ID

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/liubing8609/article/details/82078055

VC++如何根据进程名获取进程ID

#include <Tlhelp32.h>

UINT GetProcessIdByName(LPCTSTR pszExeFile)

{

    UINT nProcessID = 0;

    PROCESSENTRY32 pe = {sizeof(PROCESSENTRY32)};

 

    HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0);

    if (hSnapshot != INVALID_HANDLE_VALUE)

    {

        if (Process32First(hSnapshot, &pe))

        {

            while (Process32Next(hSnapshot, &pe))

            {

                if (lstrcmpi(pszExeFile, pe.szExeFile) == 0)

                {

                    nProcessID = pe.th32ProcessID;

                    break;

                }

            }

        }

        CloseHandle(hSnapshot);

    }

    return nProcessID;

}

 

使用例子:

UINT id = GetProcessIdByName(_T("ctfom.exe"))

猜你喜欢

转载自blog.csdn.net/liubing8609/article/details/82078055