Closing Console Exits GUI Application

Everything else that doesn't fall into one of the other PB categories.
User avatar
grabiller
Enthusiast
Enthusiast
Posts: 309
Joined: Wed Jun 01, 2011 9:38 am
Location: France - 89220 Rogny-Les-Septs-Ecluses
Contact:

Closing Console Exits GUI Application

Post by grabiller »

Hello,

I have a windowed application with a standard event loop (event = WaitWindowEvent(), etc..).

In my application, at some point, the user can decide to open a console window (internally with OpenConsole()) for logging purpose. On Windows, this indeed opens a new "Console" window. But when the user close that window, the all application quit !

And this, without receiving any specific message in the event loop that I can intercept. Or is there ?

Perhaps I'm doing something wrong ? Any idea how I can display a console window without the all application exiting whenever the user is closing that specific window ?

Cheers,
Guy.
guy rabiller | radfac founder / ceo | raafal.org
User avatar
TI-994A
Addict
Addict
Posts: 2752
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Closing Console Exits GUI Application

Post by TI-994A »

grabiller wrote:On Windows, this indeed opens a new "Console" window. But when the user close that window, the all application quit !
You're right. The CloseConsole() function works well, but when the close window [X] button is clicked, it terminates the program.
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
User avatar
RSBasic
Moderator
Moderator
Posts: 1228
Joined: Thu Dec 31, 2009 11:05 pm
Location: Gernsbach (Germany)
Contact:

Re: Closing Console Exits GUI Application

Post by RSBasic »

You can add a close handler to detect:

Code: Select all

EnableExplicit

Procedure SaveSettings()
  Debug "Saving..."
  Delay(2000)
EndProcedure

If OpenWindow(0, 0, 0, 500, 400, "Window", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  If OpenConsole("My Console")
    SetConsoleCtrlHandler_(@SaveSettings(), #True)
    
    PrintN("Hello")
  EndIf
  
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
        SaveSettings()
        End
    EndSelect
  ForEver
EndIf
Or you can disable the close button:

Code: Select all

EnableExplicit

Define ConsoleHandle

If OpenLibrary(0, "Kernel32.dll")
  Prototype GetConsoleWindow()
  Define GetConsoleWindow.GetConsoleWindow = GetFunction(0, "GetConsoleWindow")
  CloseLibrary(0)
EndIf

If OpenWindow(0, 0, 0, 500, 400, "Window", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  If OpenConsole("My Console")
    ConsoleHandle = GetConsoleWindow()
    DeleteMenu_(GetSystemMenu_(ConsoleHandle, #False), 6, #MF_BYPOSITION)
    SendMessage_(ConsoleHandle, #WM_NCPAINT, 1, 0)
    
    PrintN("Hello")
  EndIf
  
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
        End
    EndSelect
  ForEver
EndIf

Image
Image
User avatar
grabiller
Enthusiast
Enthusiast
Posts: 309
Joined: Wed Jun 01, 2011 9:38 am
Location: France - 89220 Rogny-Les-Septs-Ecluses
Contact:

Re: Closing Console Exits GUI Application

Post by grabiller »

RSBasic wrote:You can add a close handler to detect: .../...
But this doesn't prevent the application to quit, am I right ?

Or should I return a specific value from the handler in order to prevent the application to quit ?

Disabling the close button is not really an option as this would feel really odd for any Windows user : /
guy rabiller | radfac founder / ceo | raafal.org
User avatar
RSBasic
Moderator
Moderator
Posts: 1228
Joined: Thu Dec 31, 2009 11:05 pm
Location: Gernsbach (Germany)
Contact:

Re: Closing Console Exits GUI Application

Post by RSBasic »

Or you can create a own console window. (OpenWindow with EditorGadget)
Or you create a new PB project and execute your console program with RunProgram().
Image
Image
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Closing Console Exits GUI Application

Post by Dude »

grabiller wrote:when the user close that window, the all application quit !
That's normal. You can't stop it. According to Microsoft for Visual Basic at https://support.microsoft.com/en-us/kb/171654 :
Microsoft wrote:If you close the console window, the Visual Basic EXE will terminate.
See also:

http://stackoverflow.com/questions/1195 ... -whole-app
http://stackoverflow.com/questions/1803 ... -is-closed

[January 2017 Edit] The above Microsoft article is now removed, so here was its original text:
Microsoft KB Article 171654 originally wrote: .
HOWTO: Attach a Console Window to Your Visual Basic Program

-------------------------------------------------- ------------------------------
The information in this article applies to:

Microsoft Visual Basic Learning, Professional, and Enterprise Editions for Windows, version 6.0
Microsoft Visual Basic Control Creation, Learning, Professional, and Enterprise Editions for Windows, version 5.0
Microsoft Visual Basic Standard, Professional, and Enterprise Editions, 32-bit only, for Windows, version 4.0

-------------------------------------------------- ------------------------------


SUMMARY
This article demonstrates attaching a console window to your Visual Basic application, writing to it, and running another application in the console window.



MORE INFORMATION
If a Visual Basic application is started from a console application, the operating system automatically detaches it from the console, preventing the Visual Basic application from interacting with it. This article does not provide a method to prevent this from happening, but does demonstrate creating a New console window that your application can interact with. It also demonstrates running a console application (batch file, in this case) from Visual Basic, which utilizes the created console.

WARNING: ANY USE BY YOU OF THE CODE PROVIDED IN THIS ARTICLE IS AT YOUR OWN RISK. Microsoft provides this code "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and / Or fitness for a particular purpose.


Step-by-Step Example
Use Notepad to create the following batch file:

DIR / W

And save it as C: \ TEST.BAT


In Visual Basic, create a new project with a form and a module.


Type the following API declarations in the module:


Option Explicit

Declare Function AllocConsole Lib "kernel32" () As Long
Declare Function FreeConsole Lib "kernel32" () As Long
Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) _
As Long
Declare Function GetStdHandle Lib "kernel32" (ByVal _
NStdHandle As Long) As Long
Declare Function WriteConsole Lib "kernel32" Alias ??"WriteConsoleA" _
(ByVal hConsoleOutput As Long, lpBuffer As Any, ByVal _
NNumberOfCharsToWrite As Long, lpNumberOfCharsWritten As Long, _
LpReserved As Any) As Long

Public Const STD_OUTPUT_HANDLE = -11 &




Add a CommandButton to the form and enter the following code:


Dim hConsole As Long

Private Sub Command1_Click ()
Dim Result As Long, sOut As String, cWritten As Long
SOut = "Hi There" & vbCrLf
Result = WriteConsole (hConsole, ByVal sOut, Len (sOut), cWritten, _
ByVal 0 &)
Shell "C: \ TEST.BAT"
End Sub

Private Sub Form_Load ()
If AllocConsole () Then
HConsole = GetStdHandle (STD_OUTPUT_HANDLE)
If hConsole = 0 Then MsgBox "Could not allocate STDOUT"
Else
MsgBox "Could not allocate console"
End If
End Sub

Private Sub Form_Unload (Cancel As Integer)
CloseHandle hConsole
FreeConsole
End Sub




Run the application. A blank console window will appear.


Click the CommandButton. Both the text in sOut and the output from the batch file will appear in the console.


Close the form. The console window will terminate.


NOTES:

If you run another application in the console, it will run asynchronously with your Visual Basic application. Output from the two applications can become interspersed.


If the console application has not terminated prior to your Visual Basic application closing, the console window will remain open.


If you close the console window, the Visual Basic EXE will terminate. If you are in the Visual Basic development environment (IDE), closing the console window will terminate the IDE and it may hang the console window. Use the Task Manager to terminate the Task.
Post Reply