我的主机是我的还是从父母那里继承的?

发布于 2024-11-08 06:50:18 字数 65 浏览 3 评论 0原文

NT 字符模式应用程序如何确定其控制台是否是从父进程继承的,而不是在 CreateProcess 中新分配的控制台?

How can NT character-mode application determine if its console has been inherited from parent process, as opposed to newly allocated console within CreateProcess?

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

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

发布评论

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

评论(2

夜唯美灬不弃 2024-11-15 06:50:18

一些想法可能有帮助,也可能没有帮助 - 这并不是真正的答案,但它太长,无法放入注释中。*

您可以使用 GetConsoleWindow() 来确定控制台的 HWND。然后可以看看是否有其他人在分享。尝试在其上调用 GetWindowThreadProcessId - 在某些版本的 Windows 上,如果我没记错的话,这似乎会返回 CSRSS 进程的 PID - 这没有帮助。但似乎在Win7上,它返回最初拥有该窗口的进程的PID。

例如,我启动了一个CMD窗口,然后输入了more;所以我们有 cmd.exe 和 more.exe 共享同一个窗口。 Spy++ 报告 HWND 属于 cmd.exe。

但是使用“start more”,因此创建一个包含 more 的新控制台,spy++ 报告新窗口属于 more.exe。

然而,这可能是 Win7 中的新行为(或者至少在以前的版本中可能不一致);控制台窗口实际上由一个辅助进程拥有,Win7 中为 conhost.exe,以前版本中为 csrss。 GetWindowThreadProcessId 可能会返回以前版本中这些辅助进程的 PID。谁知道它会在 Windows 的未来版本中返回什么 - 控制台窗口是“特殊的”。

--

不依赖 GetWindowThreadProcessID 的另一种方法是:

  • 确定父进程的进程 ID(检查 stackoverflow 以获取此问题的过去答案!)
  • AttachConsole(pid)、GetConsoleWindow() 和 FreeConsole() 来“查看”什么console HWND 您的父进程正在使用(如果有)。
  • 这样做的问题是,一个进程一次只能附加到一个控制台 - 所以你必须在一个单独的帮助进程中进行“查看”(!) - 否则你必须放弃你自己的进程首先控制台。

长话短说,也许可以近似这一点,但不清楚你是否真的想“在现实生活中”这样做; “如果没有参数则暂停”可能是最好的方法。

[*此答案仅供娱乐,禁止等情况无效]

Some ideas that may or may not help - this isn't really an answer, but it's too long to fit into the comments.*

You can use GetConsoleWindow() to determine the HWND of your console. Could then see if anyone else is sharing that. Try calling GetWindowThreadProcessId on it - on some versions of windows, if I recall correctly, it seems this returns the PID of the CSRSS process - which isn't helpful. But it seems that on Win7, it returns the PID of the process that initially owns that window.

For example, I started a CMD window, and typed in more; so we have cmd.exe and more.exe sharing the same window. Spy++ reports that the HWND belongs to cmd.exe.

But use "start more" so create a new console with more in it, and spy++ reports that the new window belongs to more.exe.

This may be new behavior in Win7 (or at least may not be consistent in previous versions), however; console windows are actually owned by a helper process, conhost.exe in Win7 and csrss in previous versions. It's possible that GetWindowThreadProcessId will return the PID of those helper processes on previous versions. And who knows what it will return in a future version of Windows - console windows are "special".

--

A different approach that doesn't rely on GetWindowThreadProcessID is to:

  • determine your parent's process ID (check stackoverflow for past answers to this question!)
  • AttachConsole(pid), GetConsoleWindow(), and FreeConsole() to "peek" at what console HWND your parent process is using (if any).
  • The catch with this is that a process can be attached to only one console at a time - so you'd have to do this "peek" in a separate helper process (!) - otherwise you'd have to let go of your own console first.

Long story short, it might be possible to approximate this, but not clear that you'd actually want to do it "in real life"; the "pause if no parameters" is likely the best way to go.

[*This answer is provided for entertainment purposes only, void where prohibited, etc.]

暗恋未遂 2024-11-15 06:50:18

您可以尝试GetConsoleProcessList,第一个进程将是询问的进程,第二个是父进程,但如果它已退出(例如start.exe),则列表链断裂,它将仅包含当前正在运行的进程。

尽管当我使用 Sysinternals Process Explorer 检查进程时,它在“线程”选项卡中显示 ConEmuHk64.dll 。当我正常运行我的应用程序时,该函数会报告 ConEmuC64.exe <= Far.exe <= ConEmuC64.exe 进程(没有第一个),或者 cmd.exepowershell.exe 从标准 Windows 终端运行时。

所以实际上父进程可能会退出但控制台仍然是继承的

you may try GetConsoleProcessList, the first process will be the one that asks, the second is the parent, but if it has quit (for example start.exe) then the list chain breaks and it will contain only the currently running process.

although when i check the process with Sysinternals Process Explorer, it shows ConEmuHk64.dll in Threads tab. when i normally run my app that function reports ConEmuC64.exe <= Far.exe <= ConEmuC64.exe processes (without the first one), or either cmd.exe or powershell.exe when running from the standard windows terminal.

so actually the parent process may quit but the console stays inherited

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