Board index » delphi » Calling a Procedure with it's name in a variable

Calling a Procedure with it's name in a variable

Calling a Procedure with it's name in a variable
How can I call a procedure whose name comes from a table, list, etc.?
In other words, based on the environment I want to load a procedure name
into a variable and call it.  What would be the instruction?

Any Ideas?

Thanks for the help in advance.

 

Re:Calling a Procedure with it's name in a variable


In article <3477E2BD.A5B5...@21stSoft.com>, Micha...@21stSoft.com says...

Quote
> Calling a Procedure with it's name in a variable
> How can I call a procedure whose name comes from a table, list, etc.?
> In other words, based on the environment I want to load a procedure name
> into a variable and call it.  What would be the instruction?

unit ProcDict;

interface

type MyProc = procedure (s : String);

procedure RegisterProc(procName : String; proc : MyProc);
procedure ExecuteProc(procName : String; arg : String);

implementation

uses Classes;
var ProcDict : TStringList;

procedure RegisterProc(procName : String; proc : MyProc);
begin
   ProcDict.AddObject(procName, TObject(@proc));
end;

procedure ExecuteProc(procName : String; arg : String);
var
   index : Integer;
begin
   index := ProcDict.IndexOf(ProcName);
   if index >= 0 then
      MyProc(ProcDict.objects[index])(arg);
// Missing error reporting
end;

initialization
   ProcDict := TStringList.Create;
   ProcDict.Sorted := true;

finalization
   ProcDict.Free;

end.

Hope it helps

--- Raoul

Re:Calling a Procedure with it's name in a variable


In article <3477E2BD.A5B5...@21stSoft.com>,
   "Michael A. Cordova" <Micha...@21stSoft.com> wrote:

Quote
>Calling a Procedure with it's name in a variable
>How can I call a procedure whose name comes from a table, list, etc.?
>In other words, based on the environment I want to load a procedure name
>into a variable and call it.  What would be the instruction?

>Any Ideas?

>Thanks for the help in advance.

I think the easiest way, and most people will probably use the same approach,
is to use a series of if statements.

procedure RunProcedure(ProcName: String);
begin
  if ProcName = 'Procedure1' then
    Procedure1;
  if ProcName = 'Procedure2' then
    Procedure2;
  :
  :
end;

I made not appear a really fancy way of doing it, but it is very easy to
implement and not likely to become buggy.

Trevor Hand

Re:Calling a Procedure with it's name in a variable


In article <65a6cn$...@newsserver.trl.OZ.AU>, someone calling themselves t.h...@trl.telstra.com.au (Trevor Hand) wrote:

Quote
>In article <3477E2BD.A5B5...@21stSoft.com>,
>   "Michael A. Cordova" <Micha...@21stSoft.com> wrote:
>>Calling a Procedure with it's name in a variable
>>How can I call a procedure whose name comes from a table, list, etc.?
>>In other words, based on the environment I want to load a procedure name
>>into a variable and call it.  What would be the instruction?
>>Any Ideas?

There must be an expert out there who could substantiate this, but
couldn't you do it the same way you would from a DLL?

Like use LoadLibrary with the exe itself, then use GetProcInstance to
find a handle to the function?

Just a thought...

|~\  /~| /~~| |~|   The .sig wears a ring of polymorph! --More--
|  \/  |/ / |_| |__ The .login hits! The .cshrc bites!
|      ' /| |_| | / ________________________________________________
| |\/|  /\  | |  /  Official member of STI:
| |  |_/  \_| | /           The Search for Terrestrial Intelligence!
=\|===========|/==========- The Mabster:  mhamil...@bunge.com.au -==

Other Threads