A Simple Program
Even before you spend time learning about a language, a quick look at the source code for a few programs can be very helpful in getting you oriented with the syntax and overall look of the language.
With that in mind, here's a quick look at a traditional "Hello World" program written for the PowerBASIC compiler.
#COMPILE EXE 'directs compiler to create EXE #DIM ALL 'requires that all variables be declared before use FUNCTION PBMAIN () AS LONG MSGBOX "Hello World" END FUNCTION
In PowerBASIC, execution of a program starts with the PBMAIN() function. When the last line of that function is executed, the program ends. In this example, the MSGBOX is displayed and the program stays open until the box is closed by clicking on the button. At that point there are no more lines of code to execute and the program ends.
A Less Simple Program
Hello World! is boring. So here's a second program that includes a Window with a button, along with the code to capture the button click to end the progrm.
#COMPILE EXE #INCLUDE "Win32API.inc" Function PBMain() AS Long Dim hDlg AS DWORD, hCtl AS DWORD Dialog NEW 0, "Caption",300,300,200,200, %WS_SYSMENU,0 TO hDlg Control Add Button, hDlg, 2, "Cancel", 100, 100, 40, 20 Dialog Show Modal hDlg CALL DlgProc TO Result& End Function CallBack Function DlgProc() AS Long If CBMSG = %WM_COMMAND Then Dialog END CBHNDL, 0 End Function
As already noted, the program execution begins with the PBMAIN() function. In this example, the four lines of code create a window with a button. The window is displayed in Modal form, meaning that subsequent lines of code are not executed until the window is closed.
The CallBack function is used by PowerBASIC to process messages received from the Windows operating system. When the button in the window is pressed, Windows sends a message to the PowerBASIC program and that message is processed by the CallBack function.
In this case, the CBMSG value is checked to verify only that a command has been returned by Windows and then ends the program by closing the Dialog (Window). In more complex programs the content of the message would be examined and one of many possible actions would be taken in response to the message.
A Simple DLL
In addition to creating executable applications (EXE), PowerBASIC can also create DLLs, which are simply libraries of code that can be accessed by any Windows program. Use of DLLs to stored often used code is a common practice by programmers.
Here's a quick look at a simple one-function DLL.
#COMPILE DLL 'directs compiler to create DLL FUNCTION RandomAdd ALIAS "RandomAdd" (BYVAL x AS SINGLE) EXPORT AS LONG RandomAdd = x + RND 'adds random amount to x END FUNCTION
The exported function 'RandomAdd' simply adds a random amount to the value of the variable x. The result is returned to the calling program. DLLs can contain exported functions, as in the above example, but can also contain private functions which may be called only by other functions within the DLL.
If you have any suggestions or corrections, please let me know.