【发布时间】:2013-10-17 13:10:07
【问题描述】:
当我启动我的应用程序时,我会尝试确定该应用程序是否还有另一个进程。我还试图弄清楚它是否在不同的用户会话中运行。
到目前为止一切顺利,这就是它在 C# 中的样子:
private static bool isThereAnotherInstance() {
string name = Path.GetFileNameWithoutExtension(Application.ExecutablePath);
Process[] pAll = Process.GetProcessesByName(name);
Process pCurrent = Process.GetCurrentProcess();
foreach (Process p in pAll) {
if (p.Id == pCurrent.Id) continue;
if (p.SessionId != pCurrent.SessionId) continue;
return true;
}
return false;
}
但是要求已经改变,我需要使用普通 WinAPI 的 C++ 中的这段代码。
到目前为止,我可以通过使用CreateToolhelp32Snapshot、OpenProcess等找到具有相同可执行路径的进程。
缺少的部分是如何获取进程的会话 ID(当前和其他进程,以及当前和其他会话)
如何做到这一点?
【问题讨论】:
标签: c# c++ winapi session process