简体   繁体   English

Delphi:如何使用名称字符串和参数调用不同的过程

[英]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:我必须调用 class 的许多 DLL 函数。这是 function 签名的示例:

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.我可以使用 if-else 或 case 来调用每个过程。 But I want to skip the long list of if-else block.但我想跳过长长的 if-else 块列表。 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]);

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.最好的 IMO 之一是使用 Delphi 创建一个“自动化服务器”,以将您的代码放入外部文件中。 This is part of Windows COM features.这是 Windows COM 功能的一部分。 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.请参阅 Microsoft 网站中的自动化服务器自动化客户端自动化使您的应用程序可以操作在另一个应用程序中实现的对象,或公开对象以便对其进行操作。

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.一旦您的代码位于自动化服务器中,它就会自动自动描述,即调用者 - 在另一个应用程序中 - 可以发现所有方法,它们的 arguments,返回值并调用它。

Delphi has no only what is required to create automation server and automation client without writing much code yourself! Delphi 不仅拥有创建自动化服务器和自动化客户端所需的一切,而且无需自己编写大量代码!

Calling an automation server is frequently used to call Office applications from a Delphi program.调用自动化服务器经常用于从 Delphi 程序调用 Office 应用程序。

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.编写自动化服务器可以做相反的事情:让 Microsoft Office 或任何能够使用自动化服务器的应用程序(我刚才说 Delphi 可以很容易地做到这一点)来调用自动化服务器中的方法。

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.这些博客文章在第二层使用 Microsoft Office,但当然您可以在客户端和服务器端使用 Delphi。

If the DLL is already written, then you can encapsulate it in an automation server to benefit for the features.如果 DLL 已经写入,那么您可以将其封装在自动化服务器中以利用这些功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM