How to enable console app to run in its own window via double-click?

Just starting out? Need help? Post your questions and find answers here.
User avatar
Nudgy
User
User
Posts: 27
Joined: Mon May 27, 2024 8:11 pm

How to enable console app to run in its own window via double-click?

Post by Nudgy »

I've created a small console program and it runs just fine, if I compile it and run it from CLI/terminal.

However, nothing happens if try to double-click it to launch it. Does anyone know why the console program won't launch in its own window?

I don't think it is quitting silently, because the first thing the console program shows is a text menu, prompting the user to select any option.

I am using Linux, and I already tried setting compiler options to "console" (the file itself is also set to allow execution as program).

Thanks!
User avatar
Michael Vogel
Addict
Addict
Posts: 2835
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: How to enable console app to run in its own window via double-click?

Post by Michael Vogel »

Not a linux user, sorry - for windows, his can be handled easily (see code below). But I would create a log file to be sure your program does anything when being started using the mouse...

Code: Select all

; First lines of Code
Macro Logger(text)
	If OpenFile(666,"App.log",#PB_File_Append)
		WriteStringN(666,FormatDate("%hh:%ii:%ss", Date())+#TAB$+text)
		CloseFile(666)
	EndIf
EndMacro
Logger("App started")
Windows only:

Code: Select all

Procedure.s GetParentExe(hWnd=0,pid=0)
	
	If hWnd<>0 Or pid<>0
		snap=CreateToolhelp32Snapshot_(#TH32CS_SNAPPROCESS,0)
		If snap
			pinfo.PROCESSENTRY32\dwSize=SizeOf(PROCESSENTRY32)
			If Process32First_(snap,@pinfo)
				If hWnd<>0
					GetWindowThreadProcessId_(hWnd,@pid)
				EndIf
				While Process32Next_(snap,@pinfo)
					If pid=pinfo\th32ProcessID
						CloseHandle_(snap)
						snap=CreateToolhelp32Snapshot_(#TH32CS_SNAPPROCESS,0)
						If snap
							parentinfo.PROCESSENTRY32\dwSize=SizeOf(PROCESSENTRY32)
							If Process32First_(snap,@parentinfo)
								While Process32Next_(snap,@parentinfo)
									If pinfo\th32ParentProcessID=parentinfo\th32ProcessID
										CloseHandle_(snap)
										exe$=LCase(PeekS(@parentinfo\szExeFile))
										Break
									EndIf
								Wend
							EndIf
							CloseHandle_(snap)
						EndIf
					EndIf
				Wend
			EndIf
			CloseHandle_(snap)
		EndIf
	EndIf
	
	ProcedureReturn exe$
	
EndProcedure

OpenConsole()
WinMode=Bool(LCase(GetParentExe(0,GetCurrentProcessId_()))<>"cmd.exe")

For i=1 To 10
	PrintN(Str(i))
Next i

If WinMode
	PrintN("Press key")
	Input()
EndIf

User avatar
Nudgy
User
User
Posts: 27
Joined: Mon May 27, 2024 8:11 pm

Re: How to enable console app to run in its own window via double-click?

Post by Nudgy »

Thanks for the suggestion. The logger confirms that the executable is launching, but I am not able to try the Windows code snippet.

I can still only run my console app via the terminal, and not by double-clicking it. I wonder if there is some Linux limitation here.

Here's one short console example that runs fine in terminal, but does not pop up if launched via double click:

Code: Select all

If OpenConsole()
  EnableExplicit
  
  ; First lines of Code
  Macro Logger(text)
  	If OpenFile(666,"App.log",#PB_File_Append)
  		WriteStringN(666,FormatDate("%hh:%ii:%ss", Date())+#TAB$+text)
  		CloseFile(666)
  	EndIf
  EndMacro
  Logger("App started")
  

  Print("Hello")
  
  If ProgramParameter(0) = ""
    PrintN("")
    Print("Press ENTER to exit...")
    Input()
  EndIf
  
EndIf
User avatar
Michael Vogel
Addict
Addict
Posts: 2835
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: How to enable console app to run in its own window via double-click?

Post by Michael Vogel »

Hopefully anyone else can give you more hints.

Not sure if the compiler options in the Linux version also allows to create different executables like for Windows (Console, Windows, etc.).

But I'm quite sure that additional log statements like Logger("'"+ProgramParameter(0)+"'") won't help you and an explicit Input() also.

In windows I would do a workaround by starting another console instance from the program itself with something like RunProgram("cmd.exe",".","/c mapping.exe parameter").
User avatar
Nudgy
User
User
Posts: 27
Joined: Mon May 27, 2024 8:11 pm

Re: How to enable console app to run in its own window via double-click?

Post by Nudgy »

Thanks, your last hint seems to work!

See the adjusted code below. It's tested only on my Linux PC, but with the compiler directive, it hopefully also works on Windows. In short, my app now launches the terminal, which in turn relaunches the app inside itself. The behaviour is the same in the terminal, but the new window can be suppressed by adding the "--cli" parameter in the terminal (or "--from-terminal", but that is intended to be just an internally used parameter).

If there is a more elegant solution I don't know, but here's the code that at least works on Linux (and hopefully Windows):

Code: Select all

Define suppressRelaunch.i = #False
Define i.i

For i = 0 To CountProgramParameters() - 1
  Select ProgramParameter(i)
    Case "--from-terminal"
      suppressRelaunch = #True
    Case "--cli"
      suppressRelaunch = #True
  EndSelect
Next

If suppressRelaunch = #False
  CompilerIf #PB_Compiler_OS = #PB_OS_Windows

    ; Windows: relaunch inside cmd.exe
    RunProgram("cmd.exe",
               "/k " + Chr(34) + ProgramFilename() + Chr(34) + " --from-terminal",
               GetPathPart(ProgramFilename()))
    End

  CompilerElse

    ; Linux / Unix: try common terminal emulators
    Define cmd.s = Chr(34) + ProgramFilename() + Chr(34) + " --from-terminal"

    If RunProgram("x-terminal-emulator", "-e " + cmd, "")
    ElseIf RunProgram("gnome-terminal", "-- " + cmd, "")
    ElseIf RunProgram("konsole", "-e " + cmd, "")
    Else
      RunProgram("xterm", "-e " + cmd, "")
    EndIf

    End
  CompilerEndIf
EndIf
Post Reply