给定进程句柄获取正在运行的进程

发布于 2024-08-02 09:59:46 字数 99 浏览 6 评论 0原文

有人可以告诉我,如果我已经知道句柄,如何使用进程类捕获 C# 中正在运行的进程?

我也不想枚举 getrunning 进程方法。如果可以的话 pInvoke 是可以的。

can someone tell me how i can capture a running process in c# using the process class if i already know the handle?

Id rather not have not have to enumerate the getrunning processes method either. pInvoke is ok if possible.

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

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

发布评论

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

评论(5

月亮是我掰弯的 2024-08-09 09:59:46

在普通的 C# 中,看起来您必须遍历所有这些:

// IntPtr myHandle = ...
Process myProcess = Process.GetProcesses().Single(
    p => p.Id != 0 && p.Handle == myHandle);

如果未找到句柄,上面的示例会故意失败。否则,您当然可以使用 SingleOrDefault。显然,它不喜欢您请求进程 ID 0 的句柄,因此需要额外的条件。

使用 WINAPI,您可以使用 GetProcessId。我在 pinvoke.net 上找不到它,但应该可以:(

[DllImport("kernel32.dll")]
static extern int GetProcessId(IntPtr handle);

签名使用 DWORD,但进程 ID 由 .NET BCL 中的 int 表示)

你有一个句柄,但没有进程 ID,这似乎有点奇怪。通过调用 OpenProcess,它需要一个进程 ID。

In plain C#, it looks like you have to loop through them all:

// IntPtr myHandle = ...
Process myProcess = Process.GetProcesses().Single(
    p => p.Id != 0 && p.Handle == myHandle);

The above example intentionally fails if the handle isn't found. Otherwise, you could of course use SingleOrDefault. Apparently, it doesn't like you requesting the handle of process ID 0, hence the extra condition.

Using the WINAPI, you can use GetProcessId. I couldn't find it on pinvoke.net, but this should do:

[DllImport("kernel32.dll")]
static extern int GetProcessId(IntPtr handle);

(signature uses a DWORD, but process IDs are represented by ints in the .NET BCL)

It seems a bit odd that you'd have a handle, but not a process ID however. Process handles are acquired by calling OpenProcess, which takes a process ID.

独自←快乐 2024-08-09 09:59:46
using System.Diagnostics;

class ProcessHandler {
    public static Process FindProcess( IntPtr yourHandle ) {
        foreach (Process p in Process.GetProcesses()) {
            if (p.Handle == yourHandle) {
                return p;
            }
        }

        return null;
    }
}
using System.Diagnostics;

class ProcessHandler {
    public static Process FindProcess( IntPtr yourHandle ) {
        foreach (Process p in Process.GetProcesses()) {
            if (p.Handle == yourHandle) {
                return p;
            }
        }

        return null;
    }
}
二智少女 2024-08-09 09:59:46

.Net API 似乎没有简单的方法可以做到这一点。问题是,你从哪里得到这个句柄?如果通过同样的方式您可以访问进程 ID,您可以使用:

Process.GetProcessById (int iD)

There seems to be no simple way to do this by the .Net API. The question is, where you got that handle from? If by the same way you can get access to the processes ID, you could use:

Process.GetProcessById (int iD)

乱世争霸 2024-08-09 09:59:46

我长期使用这些方法:

    public static Process FindProcess(IntPtr handle) => FindProcess(p => p.Handle == handle);
    public static Process FindProcess(int id) => FindProcess(p => p.Id == id);
    public static Process FindProcess(string title) => FindProcess(p=> p.MainWindowTitle == title);
    public static Process FindProcess(Func<Process,bool> comparer)
    {
        foreach (Process p in Process.GetProcesses())
            if (comparer(p))
                return p;
        return null;
    }

享受......

I'm using these methods for long time:

    public static Process FindProcess(IntPtr handle) => FindProcess(p => p.Handle == handle);
    public static Process FindProcess(int id) => FindProcess(p => p.Id == id);
    public static Process FindProcess(string title) => FindProcess(p=> p.MainWindowTitle == title);
    public static Process FindProcess(Func<Process,bool> comparer)
    {
        foreach (Process p in Process.GetProcesses())
            if (comparer(p))
                return p;
        return null;
    }

Enjoy...

罪#恶を代价 2024-08-09 09:59:46

您可以使用 GetWindowThreadProcessId WinAPI 调用

http://www.pinvoke.net/default .aspx/user32/GetWindowThreadProcessId.html

要获取进程 ID - 然后使用它获取 Process 对象......

但是为什么不想枚举正在运行的进程的 id 呢?

You could use the GetWindowThreadProcessId WinAPI call

http://www.pinvoke.net/default.aspx/user32/GetWindowThreadProcessId.html

To get the Process Id - then get a Process object using that.....

But why don't you want to enumerate the ids of the running processes?

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