PDA

View Full Version : qt about powerbasic :)



Lionheart008
28-09-2009, 16:46
short comeback ;)

please, can anybody help to fix this powerbasic dll example "input.bas" ? I tried to compile it but without success, line 28 may be a problem. I want to understand the dll handling for thinbasic too. I was working at an project during my training course (with freebasic and thinbasic for one of our programming course as viewing how to create message boxes examples). Now I would like to understand how to do this one on pb :)

I have bought last week-end powerbasic 9.1 and have had a short look at this interesting tool. now my first question. would be great to get any help. I have in mind to create new things for thinbasic too, my novel is going up to the end now and I am very proud of it to finish the book until end of october, so there will be more time for thinbasic.

powerbasic code:


'
' Example DLL to display a modal dialog and return a value to the calling
' application.
'
' Requires PB/DLL v1.10 or later to compile.
'

$COMPILE DLL

$INCLUDE "WIN32API.INC"

DECLARE FUNCTION InputDlgProc(BYVAL hDlg AS INTEGER, _
BYVAL wMsg AS INTEGER, _
BYVAL wParam AS INTEGER, _
BYVAL lParam AS LONG) AS INTEGER


GLOBAL DlgAddr AS DWORD
GLOBAL hCurInstance AS WORD

GLOBAL gText AS ASCIIZ * 255
GLOBAL gCaption AS ASCIIZ * 255
GLOBAL gDefault AS ASCIIZ * 255

FUNCTION LIBMAIN (BYVAL hInstance AS LONG, _
BYVAL wDataSeg AS DWORD, _
BYVAL wHeapSize AS DWORD, _
lpszCmdLine AS ASCIIZ) EXPORT AS INTEGER ' ??? mistake here?

'Save the instance handle of the DLL
hCurInstance = hInstance

'Remember! We have to return a "one" to indicate successful
' initialization
LIBMAIN = 1 'success!

END FUNCTION


FUNCTION InputBox(TEXT AS ASCIIZ PTR, _
Caption AS ASCIIZ PTR, _
DEFAULT AS ASCIIZ PTR) EXPORT AS INTEGER

' Get the address of the dialog callback
DlgAddr = MakeProcInstance(CODEPTR(InputDlgProc), hCurInstance)

' If pointers are non-null, set default values for dialog
IF TEXT THEN
gText = @TEXT
ELSE
gText = "Enter Text"
END IF

IF Caption THEN
gCaption = @Caption
ELSE
gCaption = "INPUT BOX"
END IF

IF DEFAULT THEN
gDefault = @DEFAULT
ELSE
gDefault = ""
END IF

' Pop up the modal dialog and get return value
FUNCTION = DialogBox(hCurInstance, "INPUTBOX", 0, DlgAddr)

' If default buffer was passed, return the input text
IF DEFAULT THEN
@DEFAULT = gDefault
END IF

END FUNCTION


' **
' ** Generic routine to center any window given its handle.
' **
SUB CenterWindow(BYVAL hWnd AS WORD)

DIM WndRect AS RECT
DIM x AS INTEGER
DIM y AS INTEGER

GetWindowRect hWnd, WndRect

x = (GetSystemMetrics(%SM_CXSCREEN)-(WndRect.nRight-WndRect.nLeft))\2
y = (GetSystemMetrics(%SM_CYSCREEN)-(WndRect.nBottom-WndRect.nTop+GetSystemMetrics(%SM_CYCAPTION)))\2

SetWindowPos hWnd, %NULL, x, y, 0, 0, %SWP_NOSIZE OR %SWP_NOZORDER

END SUB


FUNCTION InputDlgProc(BYVAL hDlg AS INTEGER, _
BYVAL wMsg AS INTEGER, _
BYVAL wParam AS INTEGER, _
BYVAL lParam AS LONG) EXPORT AS INTEGER

' See INPUTBOX.RC script for details on the dialog itself

SELECT CASE wMsg
CASE %WM_INITDIALOG

'Center the window
CenterWindow hDlg

'Assign the initial values to the dialog
SetWindowText GetDlgItem(hDlg, 101), gText
SetWindowText GetDlgItem(hDlg, 102), gDefault
SetWindowText hDlg, gCaption

FUNCTION = 1
EXIT FUNCTION

CASE %WM_COMMAND
SELECT CASE wParam
CASE 103, %IDOK
'When the "OK" button is pressed, get the edit box value
' and end the dialog
GetWindowText GetDlgItem(hDlg, 102), gDefault, 255
EndDialog hDlg, 1
FUNCTION = 1
EXIT FUNCTION
'When the "CANCEL" button is pressed, or escape is pressed, or
' the dialog is closed, end the dialog
CASE 104, %IDCANCEL
EndDialog hDlg, 0
FUNCTION = 1
EXIT FUNCTION
END SELECT
END SELECT

END FUNCTION

thanks in advance, lionheart

ErosOlmi
28-09-2009, 18:00
I didin't check fully your code because I'm on hurry and didn't try to compile so take my reply as suggestion.
You seems to have taken code from an old example. Can it be?

Check your PowerBasic help. It is very detailed.
Anyway, PowerBasic LIBMAIN function is described at http://www.powerbasic.com/support/help/pbwin/html/LIBMAIN_DLLMAIN_function.htm

In your code there are also other problems like:

LIBMAIN = 1
is not a PB code. To return a parameter function you have to to use

FUNCTION = ...
and not the name of the function.


...
DEFAULT AS ASCIIZ PTR) EXPORT AS INTEGER
Is wrong. PTR parameters are always passed BYVAL so change to something

...
BYVAL DEFAULT AS ASCIIZ PTR) EXPORT AS INTEGER
The same for all other parameters PTR passed. All PTR must be declared as BYVAL ...

Lionheart008
28-09-2009, 18:36
hello eros,

the code I have pushed here is directly way from powerbasic.com website:

http://www.powerbasic.com/support/downloads/demos.htm

title: modal-dialog.

so they have done something wrong with this example, isn't it ? ;)
I have tried it three, four times, you can imagine I have tested this modal-dialog closer.

lionheart

Petr Schreiber
28-09-2009, 19:06
Hi Frank,

the example you downloaded is from 1996, so ... old :)
The declaration of WinMain/LibMain is more accurate now, there are some more oddities as well.

I could post corrected version, but I have more valuable information for you.
If you want high quality examples, 100% working with latest release of compilers, neatly divided to categories, please visit Josť Roca forum (http://www.jose.it-berater.org/smfforum/index.php).

There are masters of Win32 coding, there is the latest technology use.

I have nothing against examples on PB website archive, it has been useful resource for me for ages, but it might be little difficult for PB beginner to fix compatibility problems between old code and PB/WIN 9.

PB compilers do not differ that much, but it is understandable 13 years old code is not directly usable with latest release.

The examples on PB forum are usually also very good, just watch out for the date of the post.
Once you become more experienced, you can come back to PB archive of samples and manage to translate it yourself without problem - Dave Navarro, Borje Hagsten and other PB heroes had always interesting examples to show.


Petr

ErosOlmi
28-09-2009, 21:37
Petr is 100% right.

It is not matter of right or wrong but syntactically correct for the compiler you are using now.
PowerBasic back compatibility has been always very very good but in so big window time things change.

In this case what changed is the way PTR (pointers) are passed as parameters.
With current PowerBasic pointers are ALWAYS passed BYVAL so when you see something like the following in sub/function parameters:

AnyVar AS <whatevertype> PTR
change to

BYVAL AnyVar AS <whatevertype> PTR

Default passing parameters type in PowerBasic is BYREF so

AnyVar AS <whatevertype> PTR
is in reality confusing because it seems you are passing a pointer to a pointer while

BYVAL AnyVar AS <whatevertype> PTR
is for sure passing the pointer value.

In PowerBasic to avoid headache be sure to always use BYVAL in function parameters unless you are conscious of what you want to achieve.
With BYVAL, compiler will always do internal conversions of numeric parameters. Without explicit BYVAL, BYREF is the default and compiler expects the exact variable type to be passed. See BYVAL/BYREF help in PowerBasic manual.

Ciao
Eros

Josť Roca
29-09-2009, 01:18
I have adapted the code. See: http://www.jose.it-berater.org/smfforum/index.php?topic=3270.new#new

It was so old that it was 16-bit and was using INTEGER instead of LONG and WORD instead of DWORD, plus many other things.

What has taken me more time was that the call to DialogBoxParam was failing with an error of -1, until I discovered that the variable used to store the instance handle, hCurInstance, had been declared as WORD.

There are no mistakes in the original code. It will compile if used with the compiler for what it was written, PB/DLL 1!