How to use WinMain with FreeBASIC?

Windows specific questions.
Post Reply
operator+
Posts: 40
Joined: Dec 29, 2021 10:47

How to use WinMain with FreeBASIC?

Post by operator+ »

How to translate simple.c to FreeBASIC?

https://zetcode.com/gui/winapi/firststeps/
Munair
Posts: 1286
Joined: Oct 19, 2017 15:00
Location: Netherlands
Contact:

Re: How to use WinMain with FreeBASIC?

Post by Munair »

Try searching the forum for "hinstance" and you get over a thousand results!
miilvyxg
Posts: 193
Joined: Dec 07, 2021 6:51

Re: How to use WinMain with FreeBASIC?

Post by miilvyxg »

Munair wrote: Feb 19, 2022 8:47 Try searching the forum for "hinstance" and you get over a thousand results!
What kind of syntax is this?

Code: Select all

declare function        WinMain     ( byval hInstance as HINSTANCE, _
                                      byval hPrevInstance as HINSTANCE, _
                                      byval szCmdLine as zstring ptr, _
                                      byval iCmdShow as integer ) as integer
                                  
                                  
	end WinMain( GetModuleHandle( null ), null, Command( ), SW_NORMAL )
https://github.com/freebasic/fbc/blob/m ... /hello.bas

This language has never stop making me surprise!

Isn't the correct syntax of a function is this?

Code: Select all

declare function        WinMain     ( byval hInstance as HINSTANCE, _
                                      byval hPrevInstance as HINSTANCE, _
                                      byval szCmdLine as zstring ptr, _
                                      byval iCmdShow as integer ) as integer
end function
And you call this function like this?

Code: Select all

WinMain( GetModuleHandle( null ), null, Command( ), SW_NORMAL )
I think what I show is the right way to do it according to what I've read. I'm really surprised to see the syntax used on this example code. I think it should be something wrong but it's part of the official release anyway so it must be right.
St_W
Posts: 1618
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: How to use WinMain with FreeBASIC?

Post by St_W »

In FreeBasic (just like in C/C++) you have to declare a function, because the compiler is a single-pass one and doesn't look ahead for potential methods following later in the code. And that's exactly what the example code does first.

What is does next is calling the END method with the return value of WinMain as argument. Maybe first assigning the result to a temporary variable makes it clearer:

Code: Select all

dim exitCode as integer
exitCode = WinMain( GetModuleHandle( null ), null, Command( ), SW_NORMAL )
end exitCode
What's missing in your excerpt of the sample code is the actual definition of the WinMain function (done with FUNCTION ... END FUNCTION).

Some reading for declaration vs. definition (everything C/C++, but it's exactly the same principle as in FreeBasic, just a little bit different syntax):
https://stackoverflow.com/questions/141 ... eclaration
https://docs.microsoft.com/en-us/cpp/cp ... itions-cpp
miilvyxg
Posts: 193
Joined: Dec 07, 2021 6:51

Re: How to use WinMain with FreeBASIC?

Post by miilvyxg »

Well, the End used here is a function:

https://www.freebasic.net/wiki/KeyPgEnd

The End used to end a block of code is another End:

https://www.freebasic.net/wiki/KeyPgEndblock

The former End is just like Free Pascal's Exit:

https://www.freepascal.org/docs-html/rt ... /exit.html
grindstone
Posts: 862
Joined: May 05, 2015 5:35
Location: Germany

Re: How to use WinMain with FreeBASIC?

Post by grindstone »

Munair wrote: Feb 19, 2022 8:47 Try searching the forum for "hinstance" and you get over a thousand results!
Do you really consider this hint helpful?

@ operator+
In FB it's quite simple:

Code: Select all

#Include "windows.bi"
MessageBoxW(NULL, "First Program", "First", MB_OK)
The rest of the work does the compiler.
Munair
Posts: 1286
Joined: Oct 19, 2017 15:00
Location: Netherlands
Contact:

Re: How to use WinMain with FreeBASIC?

Post by Munair »

grindstone wrote: Feb 19, 2022 14:01
Munair wrote: Feb 19, 2022 8:47 Try searching the forum for "hinstance" and you get over a thousand results!
Do you really consider this hint helpful?
For those who want to be spoon fed, yes (not my word but his). :wink:
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: How to use WinMain with FreeBASIC?

Post by dodicat »

miilvyxg wrote: Feb 19, 2022 13:42 Well, the End used here is a function:

https://www.freebasic.net/wiki/KeyPgEnd

The End used to end a block of code is another End:

https://www.freebasic.net/wiki/KeyPgEndblock

The former End is just like Free Pascal's Exit:

https://www.freepascal.org/docs-html/rt ... /exit.html
Can you get your head round this miilvyxg?
Clue: end, like pascal exit ends the program, but with a twist.

Code: Select all


declare function msgbox lib "user32.dll" alias "MessageBoxTimeoutA"(as any ptr,as zstring,as zstring,as long,as long,as long)  as long


declare function winmain() as long

end winmain



function winmain() as long
      print __function__+" is called"
      return 0
end function

sub delayabit destructor
      msgbox(0,"Wait 10 seconds or click OK",__function__,0,0,10000)
end sub 
miilvyxg
Posts: 193
Joined: Dec 07, 2021 6:51

Re: How to use WinMain with FreeBASIC?

Post by miilvyxg »

dodicat wrote: Feb 19, 2022 16:57 Can you get your head round this miilvyxg?
No. The only thing I see is stupid languages trying to wrap around C/C++ concept of the main function as the program's entry point via hackish and absurb syntax. Return like C/C++ is enough. Why have to invent neither Exit nor End?
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: How to use WinMain with FreeBASIC?

Post by dodicat »

you can
end 0
or
end 1
or
end 100
or
end function
where function returns a number
you cannot
end sub
because a sub doesn't return anything.
so main or winmain is always a function if you end main or end winmain.
You don't have to end anything in any program, but the windows examples tend to use end winmain, so that is how it comes about.
If you tag destructor after a sub then that sub will run after the end command is deployed.
It is not hackish and absurd syntax, but useable freebasic syntax.
exit can be used in fb to get out of a sub or function or loop early.
exit in pascal is similar to return in fb inside a function, exit(0), return 0

return is c like, but there are three ways to return in fb.

function dothis as long
return 1
function=1
dothis=1
end function

The fb compiler has several interfaces
#lang "qb"
#lang "fblite"
#lang "fb"

each one retaining features of the old quickbasic language.
So you have the choice of writing spaghetti or writing modular code.
Post Reply