如何从可执行文件中调用bpl中的Delphi函数? [英] How do I call Delphi functions in a bpl from an executable?

查看:275
本文介绍了如何从可执行文件中调用bpl中的Delphi函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Delphi应用程序,我已经写了一个相当简单的包装.exe。



基本上,有一个dll有一堆函数,其中一个我会迭代地调用一次我的包装器做了所需要的。我不能控制这个dll文件,永远不会。



嗯,现在这个DLL是一个BPL,我不知道如何调用函数那个文件。感谢提前。

解决方案

从包中使用函数的简单方法是使用包含该功能的单元,像往常一样调用它,并将包放在项目的运行时包的列表中。为了工作,有一些要求:


  1. 您的项目必须使用与用于编译包的相同的Delphi版本。 / li>
  2. 您必须具有该单元的DCU文件或至少包的DCP文件的权限。

  3. 该包必须存在于操作中您的程序启动时系统的搜索路径。

如果您不能满足第三个要求,或者您不想要该包装一直加载,那么您可以调用 LoadPackage 。完成这项工作的方法是一直加载 的另一个包。它将被您的项目和您要加载的包使用。中间包将暴露主包可以用来告诉应用程序其功能的接口(例如一些注册函数,变量或类)。您将无法直接在您的应用程序中使用主软件包的单元。



如果不能满足前两个要求,那么有很多更难的方式,这也是你需要做的,如果你的应用程序不是用Delphi或C ++ Builder编写的。像普通的DLL一样处理包。加载 LoadLibrary 。使用 GetProcAddress 加载其初始化函数,然后调用它。 (请记住,调用约定是注册,而不是 stdcall 。)然后加载要调用的函数的地址请记住,功能的名称已经被破坏以包含一些单位和类型的信息。在调用 FreeLibrary 之前,调用 Finalize 函数。检查源代码 LoadPackage UnloadPackage ;是否需要调用 CheckForDuplicateUnits 可能取决于您是否可以满足要求1。


I have a Delphi application that I have written a fairly simple wrapper .exe for.

Basically, there was a dll that had a bunch of functions, one of which I would call iteratively once my wrapper did what it needed to. I am not in control of this dll file, and will never be.

Well, now this DLL is a BPL, and I'm not sure how to call functions within that file. Thanks in advance.

解决方案

The easy way to use functions from a package is to "use" the unit that contains the function, call it as usual, and put the package on the list of your project's runtime packages. For that to work, there are a few requirements:

  1. Your project must use the same Delphi version as was used to compile the package.
  2. You must have access to the DCU file for the unit, or at least the DCP file for the package.
  3. The package must exist in the operating system's search path when your program starts.

If you can't satisfy the third requirement, or if you don't want to have the package loaded all the time, then you can call LoadPackage for it instead. The way to make that work is to have another package that is loaded all the time. It will be used by both your project and the package you wish to load. The intermediate package will expose an interface (such as some registration functions, a variable, or a class) that the main package can use to tell the application what its functions are. You won't be able to "use" the main package's unit in your application directly.

If you can't satisfy the first two requirements, then there is the much harder way, which is also what you'd need to do if your application isn't written in Delphi or C++ Builder. Treat the package like an ordinary DLL. Load it with LoadLibrary. Use GetProcAddress to load its Initialize function, and then call it. (Remember that the calling convention is register, not stdcall.) Then load the address of the function you wish to call, keeping in mind that the name of the function has been mangled to include some unit and type information. Call the Finalize function before you call FreeLibrary. Check the source for LoadPackage and UnloadPackage; whether you need to call CheckForDuplicateUnits probably depends on whether you can satisfy requirement number 1.

这篇关于如何从可执行文件中调用bpl中的Delphi函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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