• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 157
  • Last Modified:

Run Function from Field

I have a table that stores procedure/function names in one of the fields. How do I execute the function/procedure from the name stored in the field?
0
bozo7
Asked:
bozo7
  • 2
1 Solution
 
simonetCommented:
You can only do that if you exported the names of the functions from your executables. A requirement for this is that all the functions possible are known at compile time.

In question http://www.experts-exchange.com/jsp/qShow.jsp?ta=delphi&qid=10127197  I gave the answer to the very same question to user Girona. Here's the answer I gave her and it worked:


You have to export the functions. Make sure all of them use the same number of parameters and parameter types and all in the same sequence. Do it even if you have to make some "dumb" parameters (you know what I mean, right?).


First of all, create a new application. In form1, put a TListBox with the following items:

ThisIsProcedure1
ThisIsProcedure2
ThisIsProcedure3
ProcedureWithParam

Create a new button.

Here's the source code for the project.

########## Listing for Project1.dpr

program Project1;

uses
  Forms,
  Unit1 in 'Unit1.pas' {Form1};

{$R *.RES}

exports
  ThisIsProcedure1,
  ThisIsProcedure2,
  ThisIsProcedure3,
  ProcedureWithParam;


begin
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.


########## Listing for unit1.pas

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    ListBox1: TListBox;
    Edit1: TEdit;
    Button1: TButton;
    Label1: TLabel;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

type
  TWildProcedure = procedure (AnyInt : integer);


// All the procedures must follow the prototype defined by TWildProcedure.
// Otherwise they will need another "procedure type".
// Even if the procedure doesn't need any parameter, a "bumb" parameter
// must be defined.
// Not doing so will screw the stack up or not load the procedure.
procedure ThisIsProcedure1(Dumb : integer);
procedure ThisIsProcedure2(Dumb : integer);
procedure ThisIsProcedure3(Dumb : integer);
procedure ProcedureWithParam(Value : integer);


implementation

{$R *.DFM}

procedure ThisIsProcedure1(Dumb : integer);
begin
  Showmessage('procedure #1 executed!');
end;


procedure ThisIsProcedure2(Dumb : integer);
begin
  Showmessage('procedure #2 executed!');
end;

procedure ThisIsProcedure3(Dumb : integer);
begin
  Showmessage('procedure #3 executed!');
end;

procedure ProcedureWithParam(Value : integer);
begin
  ShowMessage('Sent value was '+ inttostr(Value));
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  MyProc : TWildProcedure;
begin
  @MyProc := GetProcAddress(hInstance, pchar(listbox1.items[listbox1.itemindex]));
  if (@MyProc<>nil) then
     MyProc(30);
end;

end.

################## that's it!

Assign the Onclick event of the TButton to Button1OnClick. Run the project. See how it works great?!

See, no confused calls, everything is simple. The trick here is to export the functions in the EXE (yes, an EXE can export functions and work like a DLL!). Another part of the trick is that all functions/procedures must have the exact same number and types of arguments/parameters (see the remarks on the source code).

Just another thing: the GetProcAddress stuff can be in any unit, not necessarily in the one that defines "ThisIsProcedureXXX". THe only requirement is that the other unit sees the definition for TWildProcedure.

In your specific case, the names of the functions will be coming from a field, in the example above they are in a ListBox, but that's only for testing purpose. The important thing here is how to achieve what you want.

Yours,

Alex
0
 
intheCommented:
Nice answer Alex :-)
0
 
simonetCommented:
Thanks, Barry! Although I am moving to Linux I can still play with Delphi.
Alex
0
 
bozo7Author Commented:
Thanks for the quick reply.
0

Featured Post

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now