如何从可执行文件调用 bpl 中的 Delphi 函数?
我有一个 Delphi 应用程序,我为它编写了一个相当简单的包装器 .exe。
基本上,有一个 dll,它有很多函数,一旦我的包装器完成了它需要的操作,我就会迭代调用其中一个函数。我无法控制这个 dll 文件,也永远不会控制。
好吧,现在这个 DLL 是一个 BPL,我不知道如何调用该文件中的函数。提前致谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用包中函数的简单方法是“使用”包含该函数的单元,像往常一样调用它,然后将该包放在项目的运行时包列表中。为此,有一些要求:
如果您不能满足第三个要求,或者您不想一直加载包,那么您可以调用
LoadPackage
来代替。实现这一点的方法是让另一个包始终被加载。它将被您的项目和您想要加载的包使用。中间包将公开一个接口(例如一些注册函数、变量或类),主包可以使用该接口告诉应用程序其功能是什么。您将无法直接在应用程序中“使用”主包的单元。如果您不能满足前两个要求,那么还有更困难的方法,如果您的应用程序不是用 Delphi 或 C++ Builder 编写的,这也是您需要做的。将包视为普通 DLL。使用
LoadLibrary
加载它。使用GetProcAddress
加载其Initialize
函数,然后调用它。 (请记住,调用约定是register
,而不是stdcall
。)然后加载您要调用的函数的地址,请记住该函数的名称已修改以包含一些单位和类型信息。在调用FreeLibrary
之前调用Finalize
函数。检查LoadPackage
和UnloadPackage
的来源;是否需要调用CheckForDuplicateUnits
可能取决于您是否可以满足要求 1。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:
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
. UseGetProcAddress
to load itsInitialize
function, and then call it. (Remember that the calling convention isregister
, notstdcall
.) 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 theFinalize
function before you callFreeLibrary
. Check the source forLoadPackage
andUnloadPackage
; whether you need to callCheckForDuplicateUnits
probably depends on whether you can satisfy requirement number 1.BPL 只是一个 DLL,其中添加了一些特定的内容。从它调用函数应该不会有任何问题,就像使用 DLL 一样,但有一个特定的警告:BPL 必须在与您使用的 Delphi 版本相同的版本中构建。如果您没有源代码,这可能是一个主要缺点。如果这对您来说是个问题,您可能应该与创建它的人交谈并要求他们将其重新制作成 DLL。
A BPL is just a DLL with a few specific additions to it. You should have no trouble calling functions from it just like you did with the DLL, with one specific caveat: The BPL has to be built in the same version of Delphi as you're using. This can be a major drawback if you don't have the source code. If this is a problem for you, you should probably talk with whoever created it and ask them to make it back into a DLL.
BPL 可以消除很多 DLL 问题。如果您可以静态链接它,则边框几乎变得透明。如果必须动态加载它,则需要一个 DLL 样式的访问函数(通常返回对象或接口)和一些常见类型(接口)定义。所有这些都应该由 BPL 制造商提供。
A BPL can eliminate a lot of DLL problems. If you can statically link it, the border becomes all but transparent. If you have to load it dynamically, you need one DLL-style access function (usually one that returns an object or an interface) and some common type (interface) definitions. All that should be supplied by the maker of the BPL.