Got an idea for enhancing PureBasic? New command(s) you'd like to see?
nco2k
Addict
Posts: 1344 Joined: Mon Sep 15, 2003 5:55 am
Post
by nco2k » Wed Aug 30, 2017 2:39 pm
please add a IsConsole() function, or allow multiple calls of OpenConsole(), without getting an IMA:
Code: Select all
OpenConsole()
OpenConsole()
Input()
c ya,
nco2k
If OSVersion() = #PB_OS_Windows_ME : End : EndIf
RSBasic
Moderator
Posts: 1218 Joined: Thu Dec 31, 2009 11:05 pm
Location: Gernsbach (Germany)
Contact:
Post
by RSBasic » Wed Aug 30, 2017 2:48 pm
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)
nco2k
Addict
Posts: 1344 Joined: Mon Sep 15, 2003 5:55 am
Post
by nco2k » Wed Aug 30, 2017 3:15 pm
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
If OSVersion() = #PB_OS_Windows_ME : End : EndIf
cas
Enthusiast
Posts: 597 Joined: Mon Nov 03, 2008 9:56 pm
Post
by cas » Wed Aug 30, 2017 8:43 pm
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()
Rinzwind
Enthusiast
Posts: 638 Joined: Wed Mar 11, 2009 4:06 pm
Location: NL
Post
by Rinzwind » Fri Feb 21, 2020 12:46 pm
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).