Page 1 of 1
IsConsole()
Posted: Wed Aug 30, 2017 2:39 pm
by nco2k
please add a IsConsole() function, or allow multiple calls of OpenConsole(), without getting an IMA:
Code: Select all
OpenConsole()
OpenConsole()
Input()
c ya,
nco2k
Re: IsConsole()
Posted: Wed Aug 30, 2017 2:48 pm
by RSBasic
Code: Select all
EnableExplicit
If OpenLibrary(0, "Kernel32.dll")
Prototype GetConsoleWindow()
Global GetConsoleWindow.GetConsoleWindow = GetFunction(0, "GetConsoleWindow")
CloseLibrary(0)
EndIf
Procedure IsConsole()
If GetConsoleWindow()
ProcedureReturn 1
Else
ProcedureReturn 0
EndIf
EndProcedure
Debug IsConsole()
OpenConsole()
Debug IsConsole()
CloseConsole()
Debug IsConsole()
OpenConsole()
Debug IsConsole()
Input()
(Windows only)
Re: IsConsole()
Posted: Wed Aug 30, 2017 3:15 pm
by nco2k
the problem is, if you compile the exe as a console application (executable format: console), then your function will return true, because the application already has the console set up by default. but in order to use PrintN() etc, you have to call OpenConsole() first, to attach the calling process and get access to the existing console. we need a cross-platform IsConsole() function, that reliably tells us that we can safely work with the existing console. or an IMA free OpenConsole() when called multiple times. or both.
c ya,
nco2k
Re: IsConsole()
Posted: Wed Aug 30, 2017 8:43 pm
by cas
If i understand your request correctly, maybe this code will help you:
console.pbi
Code: Select all
EnableExplicit
Global is_console_opened=#False
Procedure _OpenConsole(l)
If is_console_opened=#False
OpenConsole()
is_console_opened=#True
Debug "called OpenConsole() @ line "+l
Else
Debug "skip call to OpenConsole() @ line "+l
EndIf
EndProcedure
Procedure _CloseConsole(l)
If is_console_opened=#True
CloseConsole()
is_console_opened=#False
Debug "called CloseConsole() @ line "+l
Else
Debug "skip call to CloseConsole() @ line "+l
EndIf
EndProcedure
Macro OpenConsole()
_OpenConsole(#PB_Compiler_Line)
EndMacro
Macro CloseConsole()
_CloseConsole(#PB_Compiler_Line)
EndMacro
Macro IsConsole()
is_console_opened
EndMacro
Test program:
Code: Select all
EnableExplicit
IncludeFile "console.pbi"
OpenConsole()
OpenConsole()
Input()
Re: IsConsole()
Posted: Fri Feb 21, 2020 12:46 pm
by Rinzwind
Still missing. If run from terminal, a console is already open.
PB should have a native equivalent of GetConsoleWindow() (which is the workaround for Windows).
2 OpenConsole()'s are (now?) allowed (second one is ignored), but CloseConsole causes crash when no console is open (should fail silently imho).