导出和加载类 [英] Exporting and loading a class

查看:95
本文介绍了导出和加载类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究像Lua这样的脚本引擎大约2个月,现在已经完成了,我希望能够以DLL形式分发它。由于它的工作方式,我必须导出整个类(TQScript)。



并且为了添加新函数,用户必须定义​​一个新类(继承自TQScript,就像Lua-for-Delphi一样),如下所示:

  type  
TMyScript = class(TQScript)
已发布
过程MyNewFunction(Arg:TArgList); // TArgList是另一个保存参数的类
// 并传递结果
结束;





现在问题是我想要开源它,只是能够导出类,以便像你执行一个函数一样加载它:

  function  FunctionName(Arg:Byt e): Byte ; external '  SomeName.DLL'; 





但你怎么用类做这个呢?

顺便说一句我用的是Delphi,对C ++一无所知(除了基础知识)。

解决方案

Nafees Hassan写道



用户,我的意思是另一个德尔福程序员。 [...]我只针对Delphi,我不需要支持其他语言。

然后,这根本不是问题。



解决方案是:开发并使用运行时包而不是DLL:

http:// docwiki.embarcadero.com/RADStudio/Seattle/en/Libraries_and_Packages_Index [ ^ ],

http://docwiki.embarcadero。 com / RADStudio / Seattle / en / Packages [ ^ ],

http://docwiki.embarcadero.com/RADStudio/西雅图/ EN / Writing_Dynamically_Loaded_Libraries [ ^ ],
http://edn.embarcadero.com/article/27178 [ ^ ]。



您可以将这种类型的单位视为PE(DLL)的专用形式,Delphi的DLL。它们允许在这些单元之间自由分配代码,就像在一个可执行文件中一样。换句话说,所有构造,包括类都可以导入或导出。您使用普通的Delphi Pascal语法。运行时包之间的界限非常透明。



并非运行时包作为一个大致相当于.NET 程序集,但不是真正等效在他们的角色和开发技术方面。在.NET中,程序集通常是唯一的编译单元;即使每个都可以有多个模块,多模块组件根本不受欢迎,Visual Studio也不直接支持它们。在Delphi中,有两个级别的库单元: unit 是一个单独的编译单元,一个主要的封装设备,几个单元可以组成一个可执行模块,可以是应用程序,DLL或运行时包,因此可以将包视为更高级别的单元,并且可以在Delphi级别重复使用。



-SA

I've been working on a scripting engine, something like Lua, for about 2 months, and now that it's done, I want to be able to distribute it in a DLL form. Because of the way it works, I have to export the whole class (TQScript).

And to add new functions, the user has to define a new class (that inherits from TQScript, like Lua-for-Delphi does), like this:

type
  TMyScript = class(TQScript)
  published
    procedure MyNewFunction(Arg: TArgList);//TArgList is another class to hold arguments
                                           //and pass on the Result
  end;



Now the problem is that I do not want to open source it, just be able to export the class so that it can be loaded like you do a function:

function FunctionName(Arg: Byte): Byte; external 'SomeName.DLL';



But how do you do this with classes?
BTW I'm using Delphi, don't know a thing about C++ (except for basics).

解决方案

Nafees Hassan wrote


By user, I mean another Delphi programmer. […] And I'm only targeting Delphi, I don't need to support other languages.

Then, this is not a problem at all.

The solution is: develop and use runtime packages instead of DLLs:
http://docwiki.embarcadero.com/RADStudio/Seattle/en/Libraries_and_Packages_Index[^],
http://docwiki.embarcadero.com/RADStudio/Seattle/en/Packages[^],
http://docwiki.embarcadero.com/RADStudio/Seattle/en/Writing_Dynamically_Loaded_Libraries[^],
http://edn.embarcadero.com/article/27178[^].

You can consider this type of units as a specialized form of PE (DLL), "a DLL for Delphi". They allow to distribute code between those units freely, as it was in one executable file. In other words, all constructs, including classes can be "imported" or "exported". You use ordinary Delphi Pascal syntax. The boundaries between runtime packages are quite transparent.

Not that runtime packages as a are roughly equivalent to .NET assemblies, but not really equivalent in terms of their roles and development technology. In .NET, an assembly is usually the only compilation unit; even though each can have more than one module, multiple-module assemblies are not popular at all, and Visual Studio does not support them directly. In Delphi, there are two levels of library units: unit is a separate compilation unit, a main encapsulation device, and several units can make a single executable module, which can be application, DLL, or the runtime package, so a package can be considered as a higher-level unit and can be re-used "at Delphi level".

—SA


这篇关于导出和加载类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆