1

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.

Community
  • 1
  • 1
Premature Optimization
  • 1,917
  • 1
  • 15
  • 24
  • 2
    Not sure why this is getting downvoted? Even before the expansion and title change, the question was pretty clear? Just because it's not the everyday "How do I output Hello World in JavaScript" question doesn't mean it's not a valid question? Puzzled. – Michael Stum May 23 '11 at 23:27
  • @Michael Stum, yep. I also realized what [winnt] tag have few attendees, [console] have low relevance to Windows consoles specifically, and [windows] is just too broad, while question itself is kinda language-independent, but i failed to figure out better tagset. Please retag the question if you know a better combination. Thanks for feedback and breaking the silence, BTW :-) – Premature Optimization May 24 '11 at 00:03
  • Curious as to why you want to do this? I don't think there's a simple way to do it; there may be a workaround, but depending on what you're trying to do, there may be a better approach entirely... – BrendanMcK May 24 '11 at 04:04
  • @BrendanMcK, my interest is more likely general, rather than specific. But consider an example i developed this interest from: end-user executed some character mode app from explorer, program detects no valid command line and prints usage guidelines and then exits. In NT (as opposed to Win9x, where console window remains) all these actions are almost completely useless besides giving user a slightest hint (by flashing console window) to repeat it from cmd (as parent process). Surely, i can detect specific cases (as no parameters) and wait for input, but general solution ought to be much better – Premature Optimization May 24 '11 at 19:15

1 Answers1

2

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

BrendanMcK
  • 14,252
  • 45
  • 54
  • why do you feel this to be "not an answer"? I think it's a good answer. – default May 25 '11 at 10:34
  • While it's interesting information perhaps, I wanted to make it clear that I wouldn't actually recommend using those techniques in real world code - they seem somewhat fragile. The value here is the insight it gives you into how consoles work; but it doesn't actually answer the original question as asked. And there might be an actual answer that I don't know about... So it's less of an answer and more a reply that says that there may be no [good] answer - which ironically may be the best answer. – BrendanMcK May 25 '11 at 10:44