Maxbox - Starter70 NoGUI Development
Maxbox - Starter70 NoGUI Development
NoGUI
____________________________________________________________________________
maXbox Starter70 – How to redirect form to a shell.
From Shell to Hell?
HellShell!
It attaches the calling process to the console of the specified process and if
the function succeeds, the return value is nonzero. A process or app can use the
FreeConsole() function to detach itself from its console. If other processes
share the console, the console is not destroyed, but the same process that
called FreeConsole() cannot refer to it.
Next we have a function to get the parent process name:
This allows me to run my GUI app from a command prompt and display output to the
same console where my app was launched. Otherwise, it will run the full GUI part
of the app and shows the window as a win- or webform.
The GetParentProcessName() asks the command prompt (powershell or cmd):
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
C:\Windows\SysWOW64\cmd.exe
1/4
There's no reliable way for a GUI subsystem application to attach to the console
of its parent process. If you try to do so you end up with two active processes
sharing the same console and only one is running:
ParentName:= strlower(GetParentProcessName());
ParentName:= PathExtractName(ParentName);
Set_ReportMemoryLeaksOnShutdown(false)
if (ParentName='cmd.exe') or (ParentName='powershell.exe') then begin
AttachConsole(-1);
NativeWriteln('Start with maXbox4 Console Output--->');
for it:= 1 to 50 do if IsPrime(it) then
NativeWriteln(IntToStr(it)+' is prime');
NativeWriteln('-----end-----');
FreeConsole();
2/4
This is OK if you are just wanting to display output into the command line. But
operations like redirecting output into a file for example are not working e.g.:
start /wait Checker.exe > out.txt would still output into console and not into
file out.txt. Different solution exists for the PowerShell:
If you are lost into the source code then you could easily add parameters to
your app to write output to a file instead of the console: -o out.txt, since
it's your tool doing the writing, you can write wherever you want for example to
start out of the shell and get output to the shell and in the end plot an image
to another file output as a png-graphic like below:
We believe the best option is to create two separate executables or scripts. One
for the GUI subsystem, and one for the shell subsystem. This is the approach
also taken by:
3/4
FreeConsole. After a process calls FreeConsole, it can call the AllocConsole
function to create a new console or AttachConsole to attach to another console.
http://www.softwareschule.ch/examples/866_native_console.txt
Ref:
http://www.softwareschule.ch/box.htm
https://scikit-learn.org/stable/modules/
Doc:
https://maxbox4.wordpress.com
4/4