How to check if procedure exists and call it during runtime?

Everything else that doesn't fall into one of the other PB categories.
User avatar
Kukulkan
Addict
Addict
Posts: 1352
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

How to check if procedure exists and call it during runtime?

Post by Kukulkan »

Hi,

I wonder if it is possible to check during runtime, if a function with a given name exists and how to call it then?

Pseudo-Code example where I named a conceived function FindFunctionRuntime() for showing:

Code: Select all

Procedure test1()
  MessageRequester("Test 1", "Just a test", #PB_MessageRequester_Ok)
EndProcedure

*pointer = FindFunctionRuntime("test1"); will find something :)
If *pointer <> 0
    CallFunctionFast(*pointer)
EndIf

*pointer = FindFunctionRuntime("test2"); will not find something :(
If *pointer <> 0
    CallFunctionFast(*pointer)
EndIf
Is such possible?
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: How to check if procedure exists and call it during runt

Post by wilbert »

Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
kenmo
Addict
Addict
Posts: 1967
Joined: Tue Dec 23, 2003 3:54 am

Re: How to check if procedure exists and call it during runt

Post by kenmo »

Code: Select all

Runtime Procedure test1()
  MessageRequester("Test 1", "Just a test", #PB_MessageRequester_Ok)
EndProcedure

*pointer = GetRuntimeInteger("test1()"); will find something :)
If *pointer <> 0
    CallFunctionFast(*pointer)
EndIf

*pointer = GetRuntimeInteger("test2()"); will not find something :(
If *pointer <> 0
    CallFunctionFast(*pointer)
EndIf
Fred
Administrator
Administrator
Posts: 16619
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: How to check if procedure exists and call it during runt

Post by Fred »

Best is to use a real function pointer instead of CallFunctionFast() declared with Prototype
User avatar
Kukulkan
Addict
Addict
Posts: 1352
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

Re: How to check if procedure exists and call it during runt

Post by Kukulkan »

Thanks, I was not aware of these functions! Great :-)
Joubarbe
Enthusiast
Enthusiast
Posts: 555
Joined: Wed Sep 18, 2013 11:54 am
Location: France

Re: How to check if procedure exists and call it during runt

Post by Joubarbe »

Prototypes can have parameters and return value:

Code: Select all

Runtime Procedure test(param.i)
	MessageRequester("Test 1", "Just a test, with a parameter: " + param, #PB_MessageRequester_Ok)
	ProcedureReturn 2
EndProcedure

*pointer = GetRuntimeInteger("test()")
Prototype prot(param.i)
Define foo.prot = *pointer
result = foo(1)
Debug result
Post Reply