user5005768Himadree
user5005768Himadree

Reputation: 1427

Delphi: How to make call to different procedures with its name string and parameters

I have to call many DLL functions of a class. here is a sample of function signature:

type
   loaderClass = class
       public
   procedure proceA(x : Integer); virtual; stdcall; abstract;
   procedure proceA1(x : Integer); virtual; stdcall; abstract;
   procedure proceB(y : Integer; name : PAnsiChar); virtual; stdcall; abstract;
   procedure proceB1(y : Integer; name : PAnsiChar); virtual; stdcall; abstract;
   procedure proceC(z : Integer; name : string); virtual; stdcall; abstract;
   procedure proceC1(z : Integer; name : string); virtual; stdcall; abstract;
   procedure proceD(d : Double; c : Char); virtual; stdcall; abstract;
   procedure proceD1(d : Double; c : Char); virtual; stdcall; abstract;
.....
.....
.....
end;

I can use if-else or case to call each procedure. But I want to skip the long list of if-else block. at runtime, my program can find the name of the procedure to be called. So is it possible to call all functions by passing with its name string and parameters. Somthing like this sample:

//Variant parameters
args: array of const;
cArray : array of AnsiChar;
functionName: string;

loader := loaderClass.Create;
functionName := 'proceB';
args[0] := 10;
cArray := 'Hello';
args[1] := cArray;
//runtime it should call proceB(10, 'Hello');
loader.functionName(args[0], args[1]);

Upvotes: 1

Views: 227

Answers (1)

fpiette
fpiette

Reputation: 12292

There are different ways of doing that.

One of the best IMO is to create an "automation server" with Delphi to put your code in an external file. This is part of Windows COM features. See Automation Servers and Automation Clients in Microsoft website: Automation makes it possible for your application to manipulate objects implemented in another application, or to expose objects so they can be manipulated.

Once your code is in an automation server, it is automatically auto-descriptive, that is the caller - in another application - can discover all the methods, their arguments, return values and call it.

Delphi has no only what is required to create automation server and automation client without writing much code yourself!

Calling an automation server is frequently used to call Office applications from a Delphi program.

Writing an automation server can do just the reverse: have Microsoft Office or any application able to use automation server (And I just said Delphi can do it easily) to call methods in the automation server.

Long time ago I wrote blog posts about both:

Those blog posts use Microsoft Office for the second tier but of course you can have Delphi in both client and server side.

If the DLL is already written, then you can encapsulate it in an automation server to benefit for the features.

Upvotes: 2

Related Questions