9

ハンドルが既にわかっている場合、プロセス クラスを使用して C# で実行中のプロセスをキャプチャする方法を教えてもらえますか?

getrunning processes メソッドも列挙する必要はありません。pInvoke は可能であれば問題ありません。

4

4 に答える 4

10

単純な 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);

(署名は を使用しますが、プロセス ID は.NET BCL では s でDWORD表されます)int

ハンドルはあるのにプロセス ID がないというのは少し奇妙に思えます。OpenProcessプロセス ハンドルは、プロセス ID を受け取る を呼び出すことによって取得されます。

于 2009-08-14T08:16:53.793 に答える
3
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;
    }
}
于 2009-08-14T08:18:01.233 に答える
1

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)

于 2009-08-14T08:19:48.000 に答える
0

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?

于 2009-08-14T08:26:15.537 に答える