VB - 以隐式方式链接 DLL
我正在开发 VB6 图形界面,并且需要隐式链接到 DLL。
这样做的动机来自我的上一个问题。有问题的 DLL 使用静态 TLS,__declspec(thread)
,当然,当使用 LoadLibray 显式链接 DLL 时,这会严重失败。
我真的很想避免对 DLL 进行修改,所以有人知道如何欺骗 VB6 可执行文件隐式链接到特定的 DLL 吗?
I'm working on a VB6 graphic interface and I need to make an implicit linking to a DLL.
The motivation for this comes from my previous question. The DLL in question uses static TLS, __declspec(thread)
, and of course this fails horribly when the DLL is linked explicitly using LoadLibray.
I'd really like to avoid modifications to the DLL, so does anyone know how to trick a VB6 executable to link to a specific DLL implicitly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为 DLL 创建一个 IDL 文件,用于描述
模块< 中导出的函数/code>
子句。
使用 MIDL 编译器进行编译并引用 VB6 项目中生成的 tlb 文件(项目 - 参考)。
并删除所有
声明函数
。tlb 文件仅用于编译(在本例中),您不必将其包含到安装程序中。
Create an IDL file for your DLL that describes your exported functions in a
module
clause.Compile with the MIDL compiler and reference the resulting tlb file from your VB6 project (Project - References).
And remove all
Declare Function
s.The tlb file is only used for compilation (in this case), you don't have to include it into the setup.
以下是从标准操作系统 dll 导入函数的示例 IDL
Here is a sample IDL that import functions from standard OS dlls
最后,由于 GSerg 和 David Heffernan 的帮助,我能够解决这个问题。
这里的 IDL 用于生成 .tlb 要
编译它,请在 Visual Studio 命令提示符中使用命令“midl”。
生成的 .tlb 文件应与 DLL 一起放置在 VB6 项目的同一目录中。
在 VB6 项目中的 Project->References 下,可以添加 .tlb 文件。
如果一切顺利,按 F2,可以在可用库列表中注意到“myTypeLib”。
现在可以在 VB6 项目中调用“myFunc”了!
然而,有两个问题需要指出:
1)某些变量类型在 VB6 和 C 之间不兼容。这个问题的一个例子是 char 数组。在 VB6 中,它们被声明为
Dim myStr as String
,而在 C 中,它们通常被声明为char myStr[MAX_DIM];
。为了在不修改 DLL 的情况下实现 VB6 和 C 之间的转换,可以在 VB6 端将字符串声明为Dim myStr as String * 256
,而在 IDL 文件中,应传递相应的字符串到函数LPSTR myStr
。2)VB6 在创建 .exe 之前不会链接 DLL。但如果 DLL 没有被链接,那么它的函数是不可见的。因此,必须在 VB6 项目中使用的隐式链接 DLL 的所有功能都必须包含在 IDL 文件中。
而且,出于同样的原因,即使所有函数都包含在IDL文件中之后,也不可能从IDE中运行程序(它会崩溃)来调试它。运行应用程序的唯一方法是创建 .exe。
Finally I was able to solve the problem thanks to GSerg and David Heffernan help.
Here the IDL to be used to generate the .tlb
To compile it use the command "midl" in the Visual Studio command prompt.
The resulting .tlb file should be placed in the same directory of the VB6 project, together with the DLL.
In the VB6 project under Project->References it's possible to add the .tlb file.
If all is gone well, pressing F2, would be possible to notice "myTypeLib" in the list of the available library.
Now it's possible to call "myFunc" inside the VB6 project!
However there are two issue to point out:
1)Some variable types are not compatible between VB6 and C. An example of this issue is rapresented by char arrays. While in VB6 they are declared as
Dim myStr as String
, in C they are usually declared aschar myStr[MAX_DIM];
. To make possible the translation between VB6 and C, without modifing the DLL, it's possible to declare on VB6 side the strings asDim myStr as String * 256
, while in the IDL file the corrispondent string should be passed to the function asLPSTR myStr
.2)VB6 does not link the DLLs until the .exe is created. But if a DLL is not linked, then its functions are not visible. For this reasons, all the function of the implicitly linked DLLs that have to be used in the VB6 project, must be included in the IDL file.
Moreover, for the same reason, even after all the functions have been included in the IDL file, won't be possible to run the program from the IDE (it will crash) as to debug it. The only way to run the application is to create the .exe.