Declare or not Declare a prototype ?

Just starting out? Need help? Post your questions and find answers here.
luce80
New User
New User
Posts: 5
Joined: Tue Jun 01, 2021 5:20 pm

Declare or not Declare a prototype ?

Post 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)
User avatar
skywalk
Addict
Addict
Posts: 3995
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Declare or not Declare a prototype ?

Post 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
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
luce80
New User
New User
Posts: 5
Joined: Tue Jun 01, 2021 5:20 pm

Re: Declare or not Declare a prototype ?

Post 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!
User avatar
Bisonte
Addict
Addict
Posts: 1232
Joined: Tue Oct 09, 2007 2:15 am

Re: Declare or not Declare a prototype ?

Post 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!
PureBasic 6.10 LTS (Windows x86/x64) | Windows10 Pro x64 | Asus TUF X570 Gaming Plus | R9 5900X | 64GB RAM | GeForce RTX 3080 TI iChill X4 | HAF XF Evo | build by vannicom​​
English is not my native language... (I often use DeepL to translate my texts.)
Post Reply