Windows下通过进程名称获取进程pid

 例如,通过devenv.exe找到进程id号为14376

Windows下通过进程名称获取进程pid_第1张图片

 在msdn查到api接口为:

DWORD GetProcessId(
  HANDLE Process
);

 需要传入一个HANDLE,那问题又来了,怎样获得这个HANDLE呢?

网上找了很久的答案,最后在stack overflow找到了答案

https://stackoverflow.com/questions/865152/how-can-i-get-a-process-handle-by-its-name-in-c

原问题:

I'm trying to get the process handle of, say example.exe, so I can call TerminateProcess 
on it. How can I do this? Notice, it doesn't have a window so FindWindow won't work.

 回答:

#include 
#include 
#include 

int main( int, char *[] )
{
    PROCESSENTRY32 entry;
    entry.dwSize = sizeof(PROCESSENTRY32);

    HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);

    if (Process32First(snapshot, &entry) == TRUE)
    {
        while (Process32Next(snapshot, &entry) == TRUE)
        {
            if (stricmp(entry.szExeFile, "target.exe") == 0)
            {  
                HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, entry.th32ProcessID);

                // Do stuff..

                CloseHandle(hProcess);
            }
        }
    }

    CloseHandle(snapshot);

    return 0;
}

Also, if you'd like to use PROCESS_ALL_ACCESS in OpenProcess, you could try this:

#include 
#include 
#include 

void EnableDebugPriv()
{
    HANDLE hToken;
    LUID luid;
    TOKEN_PRIVILEGES tkp;

    OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken);

    LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &luid);

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

    AdjustTokenPrivileges(hToken, false, &tkp, sizeof(tkp), NULL, NULL);

    CloseHandle(hToken); 
}

int main( int, char *[] )
{
    EnableDebugPriv();

    PROCESSENTRY32 entry;
    entry.dwSize = sizeof(PROCESSENTRY32);

    HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);

    if (Process32First(snapshot, &entry) == TRUE)
    {
        while (Process32Next(snapshot, &entry) == TRUE)
        {
            if (stricmp(entry.szExeFile, "target.exe") == 0)
            {  
                HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, entry.th32ProcessID);

                // Do stuff..

                CloseHandle(hProcess);
            }
        }
    }

    CloseHandle(snapshot);

    return 0;
}

stricmp(entry.szExeFile, "target.exe")这句在我运行时报错了,

错误	C2440	“”: 无法从“WCHAR [260]”转换为“std::string”	

修改一下工程属性就行了:属性-高级-字符集-使用多字节字符集(原本是“使用 Unicode 字符集”)

这样就可以编译通过了

最终源文件如下:

#include 
#include 
#include 
#include 
#include < Windows.h>
#include 
#include 


DWORD qureyProcessId(std::string name) {
    DWORD pid;
    PROCESSENTRY32 entry;
    entry.dwSize = sizeof(PROCESSENTRY32);

    HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);

    if (Process32First(snapshot, &entry) == TRUE)
    {
        while (Process32Next(snapshot, &entry) == TRUE)
        {
            if (std::string(entry.szExeFile) == name) {
                HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, entry.th32ProcessID);
                pid = GetProcessId(hProcess);
                //std::cout << "pid = " << pid << std::endl;
                // Do stuff..
                CloseHandle(hProcess);
            }
        }
    }
    CloseHandle(snapshot);
    return pid;
}
int main() {
    auto pid = qureyProcessId("devenv.exe");
    std::cout << "pid of devenv.exe: " << pid << std::endl;
    return 0;
}

看看运行结果

Windows下通过进程名称获取进程pid_第2张图片

 

你可能感兴趣的:(C++,Windows,API)