I looked into this for quite a while during the v4 development, and i am sorry to say that there is no good solution for this.
The problem is this:
- A console process inherits the console from the parent process (Cmd.exe), but if the parent has no console, there is always one opened. (which looks bad if you have a GUI as well)
- A GUI process does not automatically open a console, but also never inherits the console from its parent. (which makes it useless here as well)
There is no way to get the best of both worlds. I do not really understand the reason for this,
but thats unfortunately how it is.
Since XP, there is the AttachConsole API. See this code:
Code: Select all
Prototype AttachConsole(dwProcessId)
#ATTACH_PARENT_PROCESS = -1
Text$ = "Hello World"+Chr(13)+Chr(10)
If OpenLibrary(0, "Kernel32.dll")
AttachConsole.AttachConsole = GetFunction(0, "AttachConsole")
If AttachConsole
If AttachConsole(#ATTACH_PARENT_PROCESS)
hConsole = GetStdHandle_(#STD_OUTPUT_HANDLE)
If hConsole <> #INVALID_HANDLE_VALUE
WriteFile_(hConsole, @Text$, Len(Text$), @nbWritten, 0)
WriteFile_(hConsole, @Text$, Len(Text$), @nbWritten, 0)
Else
MessageRequester("", "Invalid console handle!")
EndIf
FreeConsole_()
Else
MessageRequester("", "Not started from a console!")
EndIf
Else
MessageRequester("", "AttachConsole not present!")
EndIf
CloseLibrary(0)
EndIf
Compile in GUI mode and run both directly and from Cmd.exe
It works in general, but it looks like Cmd.exe is not prepared for a Child process to attach to its console.
It seems that it consideres what the program writes to the console like input
typed from the user, not like a usual console program output.
Here, it prints the path before the output, and you have to hit enter before you can go back to entering commands.
This is the closest i could get to a solution to this problem. I am afraid this is simply not possible.