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

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

 在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 <cstdio>
#include <windows.h>
#include <tlhelp32.h>

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 <cstdio>
#include <windows.h>
#include <tlhelp32.h>

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	“<function-style-cast>”: 无法从“WCHAR [260]”转换为“std::string”	

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

这样就可以编译通过了

最终源文件如下:

#include <iostream>
#include <string>
#include <ctime>
#include <thread>
#include < Windows.h>
#include <processthreadsapi.h>
#include <tlhelp32.h>


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;
}

看看运行结果

 

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要根据Windows应用程序名称进程PID获取应用程序窗口标题,您可以使用Java的JNA(Java Native Access)库。JNA允许Java程序直接调用本地代码,因此您可以使用Windows API函数获取窗口标题。 以下是一个示例代码,它演示如何使用JNA库调用Windows API函数获取窗口标题: ``` import com.sun.jna.Native; import com.sun.jna.Pointer; import com.sun.jna.platform.win32.User32; import com.sun.jna.platform.win32.WinDef; import com.sun.jna.platform.win32.WinUser; public class WindowTitleGetter { public static void main(String[] args) { // 根据应用程序名称获取窗口标题 String appName = "notepad.exe"; String windowTitle = getWindowTitleByAppName(appName); System.out.println("Window title of " + appName + ": " + windowTitle); // 根据进程PID获取窗口标题 int pid = 1234; windowTitle = getWindowTitleByProcessId(pid); System.out.println("Window title of process " + pid + ": " + windowTitle); } private static String getWindowTitleByAppName(String appName) { final User32 user32 = User32.INSTANCE; WinDef.HWND hwnd = user32.FindWindow(null, appName); if (hwnd == null) { return null; } char[] buffer = new char[1024]; user32.GetWindowText(hwnd, buffer, buffer.length); return Native.toString(buffer); } private static String getWindowTitleByProcessId(int pid) { final User32 user32 = User32.INSTANCE; final WinDef.HWND[] hwnd = {null}; user32.EnumWindows(new WinUser.WNDENUMPROC() { public boolean callback(WinDef.HWND hWnd, Pointer arg1) { int[] pidArray = {0}; User32.INSTANCE.GetWindowThreadProcessId(hWnd, pidArray); if (pidArray[0] == pid) { hwnd[0] = hWnd; return false; } return true; } }, null); if (hwnd[0] == null) { return null; } char[] buffer = new char[1024]; user32.GetWindowText(hwnd[0], buffer, buffer.length); return Native.toString(buffer); } } ``` 在上面的示例中,getWindowTitleByAppName方法使用FindWindow函数根据应用程序名称查找窗口句柄,然后使用GetWindowText函数获取窗口标题。getWindowTitleByProcessId方法使用EnumWindows函数枚举所有窗口,找到与指定PID对应的窗口句柄,然后使用GetWindowText函数获取窗口标题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值