?
Solved

Run Function from Field

Posted on 1999-07-29
4
Medium Priority
?
156 Views
Last Modified: 2010-04-04
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
Comment
Question by:bozo7
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 15

Accepted Solution

by:
simonet earned 2000 total points
ID: 1391863
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
 
LVL 17

Expert Comment

by:inthe
ID: 1391864
Nice answer Alex :-)
0
 
LVL 15

Expert Comment

by:simonet
ID: 1391865
Thanks, Barry! Although I am moving to Linux I can still play with Delphi.
Alex
0
 
LVL 1

Author Comment

by:bozo7
ID: 1391866
Thanks for the quick reply.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question