IsConsole()

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
nco2k
Addict
Addict
Posts: 1344
Joined: Mon Sep 15, 2003 5:55 am

IsConsole()

Post 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
If OSVersion() = #PB_OS_Windows_ME : End : EndIf
User avatar
RSBasic
Moderator
Moderator
Posts: 1218
Joined: Thu Dec 31, 2009 11:05 pm
Location: Gernsbach (Germany)
Contact:

Re: IsConsole()

Post 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)
Image
Image
User avatar
nco2k
Addict
Addict
Posts: 1344
Joined: Mon Sep 15, 2003 5:55 am

Re: IsConsole()

Post 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
If OSVersion() = #PB_OS_Windows_ME : End : EndIf
cas
Enthusiast
Enthusiast
Posts: 597
Joined: Mon Nov 03, 2008 9:56 pm

Re: IsConsole()

Post 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()
Rinzwind
Enthusiast
Enthusiast
Posts: 638
Joined: Wed Mar 11, 2009 4:06 pm
Location: NL

Re: IsConsole()

Post 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).
Post Reply