Page 1 of 1

Declare or not Declare a prototype ?

Posted: Tue Jun 01, 2021 6:06 pm
by luce80
How can I prototype a dll function and then use it inside a procedure ? This gives me error:

Code: Select all

If 0 = OpenLibrary(0, "USER32.DLL") : End : EndIf
        
Prototype.i ProtoMessageBox(Window.i, Body$, Title$, Flags.i = 0)
; 'MsgBox' is a variable with a 'ProtoMessageBox' type
;
MsgBox.ProtoMessageBox = GetFunction(0, "MessageBoxW")
;Declare MsgBox(Window.i, Body$, Title$, Flags.i = 0) ; if uncommented errors: prototype already declared
Procedure.s msg(a.s)
  MsgBox(0, "Hello", a) ; errors: MsgBox() is not a function
  ProcedureReturn a
EndProcedure

res = msg("World")
Debug res

CloseLibrary(0)

Re: Declare or not Declare a prototype ?

Posted: Tue Jun 01, 2021 6:51 pm
by skywalk
Yeah, you really have to read the manual.
And use EnableExplicit.
You have too many errors to begin down this road.

Code: Select all

EnableExplicit
Prototype.i ProtoMessageBox(Window.i, Body$, Title$, Flags.i = 0)
If OpenLibrary(0, "User32.dll")
  ; 'MsgBox' is a variable with a 'ProtoMessageBox' type
  ;
  Global MsgBox.ProtoMessageBox = GetFunction(0, "MessageBoxW")
  MsgBox(0, "Hello", "World") ; We don't specify the flags
EndIf

Re: Declare or not Declare a prototype ?

Posted: Wed Jun 02, 2021 3:30 pm
by luce80
Thanks a lot! Not so obvious for a novice like me. EnableExplicit is not explicitly mentioned in Prototype part of the manual but it works!

Re: Declare or not Declare a prototype ?

Posted: Thu Jun 03, 2021 7:06 am
by Bisonte
Enableexlicit also has nothing to do with Prototype,
it is used to detect typos.

You have to declare each variable so that no errors occur....
and if you misspell the variable name the compiler will throw an error message.

This saves a lot of debugging!