WDK:通过name.exe获取processId

发布于 2024-09-14 01:51:51 字数 404 浏览 7 评论 0原文

我正在 Windows Filtering Platform 中开发一个驱动程序,我需要另一个进程的进程 ID 来完成我需要做的事情。我只知道该进程的文件名(name.exe)。

在 win32 中,我可以使用函数 CreateToolhelp32Snapshot 来获取所有进程的列表,并且我可以在那里搜索 PID。 ( http://msdn.microsoft.com/en- us/library/ms684834(VS.85).aspx

不幸的是在内核模式下这个东西不可用。有人知道如何通过内核空间仅知道二进制名称来获取 processID 吗?

I'm developing a driver in Windows Filtering Platform and I need the process ID of another process to do what I need to do. I know only the file name of that process (name.exe).

In win32 I could use the function CreateToolhelp32Snapshot to get the list of all processes and I could search the PID there. ( http://msdn.microsoft.com/en-us/library/ms684834(VS.85).aspx )

Unfortunately in kernel mode this stuff is not available. Anyone know how can I obtain the processID knowing only the binary name, by kernel space?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回忆躺在深渊里 2024-09-21 01:51:51

根据时间的不同,您似乎可以使用您自己的 CreateProcessNotifyEx() 处理程序调用 PsSetCreateProcessNotifyRoutineEx()。然后,您的 CreateProcessNotifyEx() 将收到一个指向 PS_CREATE_NOTIFY_INFO 的指针。此结构中包含字段 ImageFileName 和位 FileOpenNameAvailable

程序名称将位于 ImageFileName 指向的 Unicode 字符串中。如果FileOpenNameAvailable,则该字符串将包含二进制文件的完全限定路径。否则,预计只能找到模块名称,可能没有扩展名。

Depending on the timing, it seems that you could call PsSetCreateProcessNotifyRoutineEx() with your own handler for CreateProcessNotifyEx(). Your CreateProcessNotifyEx() will then receive a pointer to a PS_CREATE_NOTIFY_INFO. In this struct is the field ImageFileName and also the bit FileOpenNameAvailable.

The program name will be in the Unicode string pointed to by ImageFileName. If FileOpenNameAvailable, then that string will contain the fully-qualified path to the binary. Otherwise, expect to find only the module name, possibly without the extension.

沩ん囻菔务 2024-09-21 01:51:51

可以使用进程快照、遍历进程、对比进程名的方法直接获取进程。

DWORD GetProcessIDByName(LPCTSTR szProcessName)
{
    STARTUPINFO st;
    PROCESS_INFORMATION pi;
    PROCESSENTRY32 ps;
    HANDLE hSnapshot;
    DWORD dwPID = 0;
    ZeroMemory(&st, sizeof(STARTUPINFO));
    ZeroMemory(&pi, sizeof(PROCESS_INFORMATION));
    st.cb = sizeof(STARTUPINFO);
    ZeroMemory(&ps, sizeof(PROCESSENTRY32));
    ps.dwSize = sizeof(PROCESSENTRY32);
    hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    if (hSnapshot == INVALID_HANDLE_VALUE)
    {
        return dwPID;
    }
    if (!Process32First(hSnapshot, &ps))
    {
        return dwPID;
    }
    do
    {
        if (lstrcmpi(ps.szExeFile, szProcessName) == 0)     {
            dwPID = ps.th32ProcessID;
        }
    } while (Process32Next(hSnapshot, &ps));
     
    CloseHandle(hSnapshot);
    return dwPID;
}

然后在主程序中加入如下代码:

DWORD pId = GetProcessIDByName("xxx.exe");

You can use the method of taking a process snapshot, traversing the process and comparing the process name to directly obtain the process.

DWORD GetProcessIDByName(LPCTSTR szProcessName)
{
    STARTUPINFO st;
    PROCESS_INFORMATION pi;
    PROCESSENTRY32 ps;
    HANDLE hSnapshot;
    DWORD dwPID = 0;
    ZeroMemory(&st, sizeof(STARTUPINFO));
    ZeroMemory(&pi, sizeof(PROCESS_INFORMATION));
    st.cb = sizeof(STARTUPINFO);
    ZeroMemory(&ps, sizeof(PROCESSENTRY32));
    ps.dwSize = sizeof(PROCESSENTRY32);
    hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    if (hSnapshot == INVALID_HANDLE_VALUE)
    {
        return dwPID;
    }
    if (!Process32First(hSnapshot, &ps))
    {
        return dwPID;
    }
    do
    {
        if (lstrcmpi(ps.szExeFile, szProcessName) == 0)     {
            dwPID = ps.th32ProcessID;
        }
    } while (Process32Next(hSnapshot, &ps));
     
    CloseHandle(hSnapshot);
    return dwPID;
}

Then get the following code into the main program:

DWORD pId = GetProcessIDByName("xxx.exe");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文