Call PB function by name

Just starting out? Need help? Post your questions and find answers here.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5353
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Call PB function by name

Post by Kwai chang caine »

Hello at all :D

Is it possible, to call a PB function like "MessageRequester()" or other by his name ?

Have a good day
ImageThe happiness is a road...
Not a destination
walbus
Addict
Addict
Posts: 929
Joined: Sat Mar 02, 2013 9:17 am

Re: Call PB function by name

Post by walbus »

I understand not exactely what you mean

Mean you this ?

Code: Select all

  Procedure MessageRequester_()
    ProcedureReturn MessageRequester("Error", "Activate firstly unicode in the PB compiler options !")
  EndProcedure
  Macro MessageRequester()
    MessageRequester_()
  EndMacro
  
  Debug MessageRequester()
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5353
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Call PB function by name

Post by Kwai chang caine »

Hello WALBUS :D

I have see several code for call procedure with for exampler his adress

Code: Select all

Procedure Test_A()
  Debug "A"
EndProcedure

Procedure Test_B(t.s)
  Debug t
EndProcedure

Dim MyFnctions.i(100)

MyFnctions(0) = @Test_A()
MyFnctions(1) = @Test_B()

CallFunctionFast(MyFnctions(0))
CallFunctionFast(MyFnctions(1), @"B")
But first it's not PB function, but personal function
And the call his with the adress not the name

For my project i have searched a langage Script, and found RUBY, apparently a good one (Simple, portable, tiny) 8)
But after i have change my mind, because i'm forced to include it into my project :|

Then after, i say to me it's perhaps more simple to try to do it myself, it's the reason why i search to create a PbScript program
I have see several code on the forum, but not really found what i search :|

For the moment i have do something a little bit like this, for numerous function of PB :oops:

Code: Select all

Procedure ScriptExec(LigneCommande.s)

 Parametre$ = StringField(LigneCommande, 2, "(")
 Parametre$ = RemoveString(Parametre$, Chr(32))
 Param1$ = StringField(Parametre$, 1, ",")
 Param2$ = StringField(StringField(Parametre$, 2, ","), 1, ")")
 
 Select StringField(LigneCommande, 1, "(")
 
  Case "MessageRequester"
  
   MessageRequester(Param1$, Param2$)
  
 EndSelect 

EndProcedure

ScriptExec("MessageRequester(Hello, Kcc)")
So i wonder, if it's possible to send a text at the place of the name of the function, i'm not forced to writing each function :wink:
A little bit like this

Code: Select all

RunFunction("NameOfPbFunction", *Param1, *Param2, *Param3, *Param4)
RunFunction("MessageRequester", @"Hello", @"Kcc")
Last edited by Kwai chang caine on Wed Jul 26, 2017 4:44 pm, edited 6 times in total.
ImageThe happiness is a road...
Not a destination
User avatar
Sicro
Enthusiast
Enthusiast
Posts: 538
Joined: Wed Jun 25, 2014 5:25 pm
Location: Germany
Contact:

Re: Call PB function by name

Post by Sicro »

Code: Select all

Procedure$ CallPBFunction(FunctionName$, Parameter1$="", Parameter2$="", Parameter3$="", Parameter4$="", Parameter5$="", Parameter6$="")
  
  Protected Result$
  
  Select FunctionName$
    Case "MessageRequester"    : Result$ = Str(MessageRequester(Parameter1$, Parameter2$, Val(Parameter3$)))
    Case "InputRequester"      : Result$ = InputRequester(Parameter1$, Parameter2$, Parameter3$, Val(Parameter4$))
    Case "ElapsedMilliseconds" : Result$ = Str(ElapsedMilliseconds())
    Case "Date"                : Result$ = Str(Date(Val(Parameter1$), Val(Parameter2$), Val(Parameter3$), Val(Parameter4$), Val(Parameter5$), Val(Parameter6$)))
  EndSelect
  
  ProcedureReturn Result$
      
EndProcedure

Define Start = Val(CallPBFunction("ElapsedMilliseconds"))
Debug "Your name: " + CallPBFunction("InputRequester", "Test", "What is your name?")
Debug "Elapsed time: " + Str(Val(CallPBFunction("ElapsedMilliseconds")) - Start)
With the "PB-Runtime" library unfortunately I had no luck.

I guess there is a solution using ASM code.
Image
Why OpenSource should have a license :: PB-CodeArchiv-Rebirth :: Pleasant-Dark (syntax color scheme) :: RegEx-Engine (compiles RegExes to NFA/DFA)
Manjaro Xfce x64 (Main system) :: Windows 10 Home (VirtualBox) :: Newest PureBasic version
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5353
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Call PB function by name

Post by Kwai chang caine »

Hello SICRO :D

You have posted in the same time of me :lol:

Yes your method his the same of mine, but i'm forced to rewriting all the functions manually :|
Furthermore, the management of parameters make problem, in some function if you send nothing or zero that's not works :|
Like this :

Code: Select all

Procedure$ CallPBFunction(FunctionName$, Parameter1$="", Parameter2$="", Parameter3$="", Parameter4$="", Parameter5$="", Parameter6$="")
 
  Protected Result$
 
  Select FunctionName$
  
    Case "MessageRequester"    
    
     If Parameter3$ <> ""
      Result$ = Str(MessageRequester(Parameter1$, Parameter2$, Val(Parameter3$)))
     Else
      Result$ = Str(MessageRequester(Parameter1$, Parameter2$))
     EndIf 
    
  EndSelect
 
  ProcedureReturn Result$
     
EndProcedure

CallPBFunction("MessageRequester", "Hello", "SICRO")
Thanks when even for your answer :wink:
Sicro wrote:I guess there is a solution using ASM code.
What the gods of programming hear you !! :mrgreen:
ImageThe happiness is a road...
Not a destination
jack
Addict
Addict
Posts: 1336
Joined: Fri Apr 25, 2003 11:10 pm

Re: Call PB function by name

Post by jack »

hello Kwai chang caine, have you looked at the code posted here? http://www.purebasic.fr/english/viewtop ... 13&t=67832
normeus
Enthusiast
Enthusiast
Posts: 415
Joined: Fri Apr 20, 2012 8:09 pm
Contact:

Re: Call PB function by name

Post by normeus »

how about something like this:

Code: Select all

  Import "User32.lib"
    KCCMsgBox(Window.i, Body$, Title$, Flags.i=0) As "_MessageBoxW@16" ; "_MessageBoxA@16" A = non_unicode, W = unicode                                                   
 EndImport
 
 Dim MyFnctions.i(100)
 MyFnctions(0) = @KCCMsgBox()
 
 ;some extra defines for fun!
  #PB_MessageRequester_Button3=512
  #PB_MessageRequester_Question=32
  #PB_MessageRequester_AbortRetryIgnore=2
  kccwin=0 ;
  
  result=CallFunctionFast(MyFnctions(0), kccwin,@"Who new it was so difficult?",@"POTUS",#PB_MessageRequester_Question|#PB_MessageRequester_AbortRetryIgnore|#PB_MessageRequester_Button3)
  Debug result
Edit: added some fun flags to the request

norm.
google Translate;Makes my jokes fall flat- Fait mes blagues tombent à plat- Machte meine Witze verpuffen- Eh cumpari ci vo sunari
User avatar
Fig
Enthusiast
Enthusiast
Posts: 351
Joined: Thu Apr 30, 2009 5:23 pm
Location: Côtes d'Azur, France

Re: Call PB function by name

Post by Fig »

*
Last edited by Fig on Thu Jul 27, 2017 8:45 am, edited 1 time in total.
There are 2 methods to program bugless.
But only the third works fine.

Win10, Pb x64 5.71 LTS
walbus
Addict
Addict
Posts: 929
Joined: Sat Mar 02, 2013 9:17 am

Re: Call PB function by name

Post by walbus »

I think we can learning a lot from this little thread

For coding efficient software :

Looking ever for the simplest and fastest way

Make codes never needless complicated

Think simple
Image
User avatar
Fig
Enthusiast
Enthusiast
Posts: 351
Joined: Thu Apr 30, 2009 5:23 pm
Location: Côtes d'Azur, France

Re: Call PB function by name

Post by Fig »

Code: Select all

 DisableDebugger
If #True=#False
   label:
   MessageRequester("","")
   EnableASM
   RET
   label2:
   MOV    eax,0
   PUSH   eax
   MOV    eax,0
   PUSH   eax
   label3: 
   DisableASM
EndIf
a.s="Title"
b.s="A text to display"

EnableASM
MOV eax,l_label
ADD eax,l_label3
SUB eax,l_label2
PUSH l_retour
PUSH b
PUSH a
!JMP eax
DisableASM
retour:
MessageRequester("next","forward instructions")
Raise errors with debugger on else It seems to work... (tested on x64 windows and PB 5.44LTS x86 ...)
Last edited by Fig on Thu Jul 27, 2017 9:08 am, edited 2 times in total.
There are 2 methods to program bugless.
But only the third works fine.

Win10, Pb x64 5.71 LTS
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5353
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Call PB function by name

Post by Kwai chang caine »

First, thanks at all for your answers 8)

@Fig
What kind of magical alien code is it ??? :shock:

Image

I love it, i not understand...but i love when even :mrgreen:
I don't know if it's possible to do that with all PB functions, mainly with the functions with string return like ReplaceString() ?
Thanks a lot for your little jewel 8)

You have right, and i understand you :wink:
So be careful....sometime wife, like mine, can also be dangerous :mrgreen:

Image

@Normeus
It's a nice piece of code, thanks for it 8)
That's works very well for MessageRequester() with call User32
But all the others functions of PB, i thinks it's not really possible to do that
For exampler, by random the function ReplaceString() :wink:

@JACK
It's an expression evaluator
It's sure i need this style of code for evaluate the expression send in the script
I have found a good one for that, with DANILO code 8)
http://forums.purebasic.com/german/view ... =8&t=24256
But for the moment it's not exactely what i search
I search to simplify the call of PB functions :wink:

@WALBUS
Yes it's very interesting, but apparently not so simple...
I have also found this splendid code of Kapslok...but a little bit hard to understand for me :oops:
http://www.purebasic.fr/english/viewtop ... 08#p428387
Make PB fully scriptable can be again a new power feature for it, for remote machine by network or other....
Thanks for your interest 8) and really nice GIF :wink: :D
Last edited by Kwai chang caine on Thu Jul 27, 2017 9:11 am, edited 2 times in total.
ImageThe happiness is a road...
Not a destination
User avatar
Fig
Enthusiast
Enthusiast
Posts: 351
Joined: Thu Apr 30, 2009 5:23 pm
Location: Côtes d'Azur, France

Re: Call PB function by name

Post by Fig »

It's not jewel, it's bullshit... But i am sure someone will fix it.
There are 2 methods to program bugless.
But only the third works fine.

Win10, Pb x64 5.71 LTS
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5353
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Call PB function by name

Post by Kwai chang caine »

Make a day...where the gods give to me, the possibility of create "bulshit" like yours !! :shock:
Thanks a lot FIG 8), for spending your time for my REAL bushit :lol:
ImageThe happiness is a road...
Not a destination
Post Reply