Code archives/Miscellaneous/Console or GUI?

This code has been declared by its author to be Public Domain code.

Download source code

Console or GUI? by xlsior2009
Checks whether the current (or other) program is compiled in GUI or Console mode
' This function will check whether or not a blitzmax executable is a console or GUI program.
' Tested with BlitzMax 1.26 and 1.30
'
' By Marc van den Dikkenberg - http://www.xlsior.org
'
' Can be useful to merge both behaviours into a single program, so a single
' source can be maintained and just compiled twice
' This will only work with windows .exe files.
' Note that this will not work with UPX compressed executables
'

SuperStrict

If CheckConsole()=True Then
'	Print "This is a console program"
	Notify "This is a Console program"

Else
	Notify "This is a GUI program"
End If 

Function CheckConsole:Int(CurrentFile:String="")
	' If no filename specified, use the running file name
	If currentfile="" Then 
		currentfile=AppArgs[0]
		' If launched from the command prompt, the .exe suffix will be missing, so lets check for that
		If Instr(Upper(currentfile),".EXE")=0 Then
			currentfile=currentfile+".exe"
		End If
	End If
	Local myfile1:TStream=ReadFile(currentfile)
	SeekStream(myfile1,$A8)
	Local CheckVar:Int=Asc(ReadString(myfile1,1))
	If CheckVar=176
		' This is a GUI application
		Return False
	Else
		' This is a Console application
		Return True
	End If
	CloseFile myfile1
End Function

Comments

grable2009
There is no garuantee that the .EXE wont be there, as its up to whomever started the application if they want to supply .EXE or not.

Oh and btw, probably a good idea to run CloseFile before returning from the function ;)


xlsior2009
There is no garuantee that the .EXE wont be there, as its up to whomever started the application if they want to supply .EXE or not.


I know -- You can pass whatever string you wish, but if you leave it empty it will use the name of the default application, as contained in AppArgs[0]. This may or may not contain the .exe suffix, depending on how the application was launched, therefore it looks whether or not the string contains the .exe portion and will add it it it can't be found.

The .exe has to be part of the name, or windows wouldn't have launched the program in the first place.

Oh, one thing I forgot to mention: This will only work under Windows, not Mac/Linux since those use a different binary file format.

Oh and btw, probably a good idea to run CloseFile before returning from the function ;)


Oops. Thanks for catching that, code fixed.


klepto22009
Why using AppArgs[0] at all? Use AppFile$ instead.


Code Archives Forum