Change console color of child process - windows

I'm creating a new process which calls a console application. I want to change the colors of this child process's console window.
The reason is that I can't redirect the console's stdout because the application manipulates the console cursor. Instead I'm stripping the console frame, clipping the info I want and embed the console in my application wholesale. I just want to change the colors so it fits in better.
I know of the SetConsoleTextAttribute function but I don't know how to get to the stdout handle of the child process to use it.
Anyone have any ideas?

The documentation for DuplicateHandle says:
Console handles can be duplicated for use only in the same process
(They are not real handles) so even if you could inject code into the child you could not go down this route.
I'm assuming the parent application does not already have a console (You can only have one per process without doing horrible hacks), if that is the case you should be able to use AllocConsole(), GetStdHandle(), SetConsoleTextAttribute(), CreateProcess() and finally FreeConsole() (You don't need FreeConsole if you only run one child process at the time)
The other option is to use cmd.exe: cmd.exe /T:?? /C childapplication.exe (Replace ?? with the color values you find by running color /? in cmd)

Related

How does the Command::new API hide the window of the calling program on Windows?

For example, I get the result of Query User by calling powershell.exe:
let output = Command::new("powershell.exe")
.arg("Query")
.arg("User")
.output()?;
// Parse the output in std...
Unfortunately, doing so will bring up a powershell window (although it will soon disappear).
Is there a way to call the console program without displaying any windows?

How can I tell if the Window Handle is main window handle?

Hello hello Thank you for reading my question.
I am currently building a program which detects new window creation / termination.
Gratefully, I have successfully managed to implement WinEvents and SetWinEventHook to capture events and filter window creation and termination.
However, I am facing a challenging task to accurately filter correct Window handles.
For example when putty.exe runs, many handles including main handle, button handle...etc are captured by my function.
However, I only desire to filter main Window handle.
Therefore, I used if statement as following.
if (event == EVENT_OBJECT_CREATE && GetParent(hwnd)==NULL)
This seemed to work for a while.
However, this if statement prevented my program from capturing child window's main handle.
Is there any way to tell if Window handle(HWND) belongs to main window?
My colleague tells me to implement GetWindowLong and compare style.
But, I have no clue about that.
If anyone knows answer, please help.
Again thanks for reading this question.

How do I enumerate another process' windows in Delphi?

I have a process foo.exe that creates a process bar.exe with the CreateProcess function. I want (in foo.exe) to enumerate the controls of a window created in bar.exe and for that I (assume that I) need the window HWND.
I know all the window classes in bar.exe, and that bar.exe only creates one window for each class at a time, so I can use the class names to find the window I want.
But what function should I use to enumerate the windows in another process? I'm looking for something that take a process handle or PID (both returned by the CreateProcess function) and an EnumProc callback procedure. Should I find bar.exe's thread ID (it is a single-threaded application) and use that with the EnumThreadWindows function?
Call EnumWindows to enumerate the top level windows.
Pass each top level window handle to GetWindowThreadProcessId to find out which process ID it is associated with.
When you find a top level window that matches your process ID, check that the window is the main window of the app, presumably by checking its class name.
Finally, call EnumChildWindows on that main window to enumerate all children of that main window.

Recreate Window Using GetConsoleScreenBufferInfo Method

I believe that the GetConsoleScreenBufferInfo method gets information about the window passed as a parameter into it into a CONSOLE_SCREEN_BUFFER_INFO structure. Can this information be used to recreate a window? Or is there a method which can create a window taking CONSOLE_SCREEN_BUFFER_INFO as a parameter?
There is no function that I know of that will create a new window given a CONSOLE_SCREEN_BUFFER_INFO structure. There certainly isn't such a thing described in the Console Functions reference.
I'd be interested to know why you want such a thing. Are you trying to create a duplicate console window? If so, you should note that a process can be associated with only one console. And you can't create a console window that's not attached to a process. The console goes away when the last process detaches from it.

How do I get a HWND from inside a DLL?

I have a DLL that I want to play sounds using Direct Sound. In order to play sounds, I need the HWND of the executable. I don't have a HWND of the executable that loads the DLL. How do I get that in the DLL without passing it in from the executable?
You could use GetCurrentProcessId to get the current process Id.
You could then call EnumWindows, and check each window with GetWindowThreadProcessId to find a window associated with your process.
However, an easier option might be to just generate your own Window. You can create a 1x1 pixel window that is not visible, and use it with Direct Sound.
This has the advantage of working even if your calling process doesn't have a usable window (or deletes window handles regularly).
Call GetGUIThreadInfo on the main thread. This gets you a bunch of HWNDs. If you need a top-level HWND, pick any valid one (not all values may be filled) and find its top level ancestor with GetAncestor(GA_ROOT).

Resources