如何从可执行文件调用 bpl 中的 Delphi 函数?

发布于 2024-08-03 23:52:21 字数 178 浏览 4 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

给妤﹃绝世温柔 2024-08-10 23:52:21

使用包中函数的简单方法是“使用”包含该函数的单元,像往常一样调用它,然后将该包放在项目的运行时包列表中。为此,有一些要求:

  1. 您的项目必须使用与编译包相同的 Delphi 版本。
  2. 您必须有权访问该单元的 DCU 文件,或者至少有权访问该包的 DCP 文件。
  3. 当程序启动时,该包必须存在于操作系统的搜索路径中。

如果您不能满足第三个要求,或者您不想一直加载包,那么您可以调用 LoadPackage 来代替。实现这一点的方法是让另一个包始终被加载。它将被您的项目和您想要加载的包使用。中间包将公开一个接口(例如一些注册函数、变量或类),主包可以使用该接口告诉应用程序其功能是什么。您将无法直接在应用程序中“使用”主包的单元。

如果您不能满足前两个要求,那么还有更困难的方法,如果您的应用程序不是用 Delphi 或 C++ Builder 编写的,这也是您需要做的。将包视为普通 DLL。使用LoadLibrary加载它。使用GetProcAddress加载其Initialize函数,然后调用它。 (请记住,调用约定是 register,而不是 stdcall。)然后加载您要调用的函数的地址,请记住该函数的名称已修改以包含一些单位和类型信息。在调用 FreeLibrary 之前调用 Finalize 函数。检查LoadPackageUnloadPackage的来源;是否需要调用 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:

  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.

请持续率性 2024-08-10 23:52:21

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.

够运 2024-08-10 23:52:21

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文