ハンドルが既にわかっている場合、プロセス クラスを使用して C# で実行中のプロセスをキャプチャする方法を教えてもらえますか?
getrunning processes メソッドも列挙する必要はありません。pInvoke は可能であれば問題ありません。
単純な 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 を受け取る を呼び出すことによって取得されます。
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;
}
}
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)
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?