我拥有我的控制台或我从我的父母继承它吗? [英] Do i own my console or i inherited it from my parent?

查看:166
本文介绍了我拥有我的控制台或我从我的父母继承它吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



中新分配的控制台,因此不受欢迎的标记!添加窗口以吸引合适的程序员






@anonymous downvoter:我可以根据一些反馈进行有限的扩展。状态什么不清楚(但是需要熟悉Windows内核和子系统)。请记住,我们很多都是程序员,因此我们的幽默非常具体,不要判断标题太苛刻。

解决方案

一些想法可能或可能不会帮助 - 这不是一个真正的答案,但它太长,适合评论* / p>

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



例如,我开始一个CMD窗口,所以我们有cmd.exe和more.exe共享同一个窗口。 Spy ++报告HWND属于cmd.exe。



但是使用开始更多,所以创建一个更多的控制台,spy ++报告新窗口属于to more.exe。



这可能是Win7的新行为(或者至少在以前的版本可能不一致),但是;控制台窗口实际上是由助手进程所拥有,conhost.exe在Win7和csrss在以前的版本。 GetWindowThreadProcessId可能会返回以前版本中的那些帮助进程的PID。而且谁知道它将在未来的Windows版本返回的控制台窗口是特殊的。



-



不依赖GetWindowThreadProcessID的不同方法是:




  • 确定父进程的ID(检查stackoverflow这个问题!)

  • AttachConsole(pid),GetConsoleWindow()和FreeConsole()来窥视父进程使用的控制台HWND(如果有)。
  • 这样做的一个原因是一个进程一次只能连接到一个控制台 - 所以你必须在一个单独的帮助进程(!)中偷看 - 否则你会



长短故事,可能是近似这个,但不清楚你实际上想在现实生活中做到这一点;



[*此回答仅为娱乐目的,在禁止的地方无效等。]

p>

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

wow, so unpopular tags! adding windows to attract appropriate programmers


@anonymous downvoter: i can do limited expansion of this question based on some feedback only. State what is not clear (familiarity with Windows kernel and subsystems is required, however). Remember, lot of us here are programmers, so our humour is very specific, do not judge title pun too harsh.

解决方案

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.]

这篇关于我拥有我的控制台或我从我的父母继承它吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆