VB6 dll调用win32 C函数
我需要为别人的VB6项目用C编写一些函数(过时超出了这个问题的范围)。
在最初的测试中,我无法让呼叫正常工作。我提供了一个 .def 文件,我尝试使用 __declspec(dllexport)、stdcall 和 WINAPI 调用约定。每次调用时,我都会收到 VB6 中的错误消息,指出“错误的 dll 调用约定”。
Win32 C 函数原型:
long WINAPI BitmapFile_Open(char *pszFileName);
void WINAPI BitmapFile_Close(long bmf);
请注意,上面我尝试了其他几种调用约定,包括 __declspec(dllexport) 和 stdcall,但都不起作用。
Def 文件:
LIBRARY ImageLib
EXPORTS
BitmapFile_Open @1
BitmapFile_Open @2
VB 全局模块:
Declare Function BitmapFile_Open Lib "ImageLib.dll" (ByVal fileName As String) As Long
Declare Function BitmapFile_Close Lib "ImageLib.dll" (ByVal bmFile As Long)
VB 代码:
Dim myFile As Long
myFile = BitmapFile_Open("test.bmp")
BitmapFile_Close (myFile)
还要注意,在原始函数中,bmFile 实际上是一个地址(指向结构的指针),但在 VB 中它将表示为 long。但是,由于 VB6 不支持指针,因此我在 C 代码中从 long 进行转换。我希望你能理解我在这里想表达的意思。它与正在发生的错误无关。任何帮助表示赞赏。
编辑:我使用了依赖项遍历器来确定函数确实正在导出。 VB6 只是调用它们时不会出错。
I need to write some functions in C for someone else's VB6 project (that being outdated is beyond the scope of this question).
During initial tests, I could not get the calls to work. I have supplied a .def file, I tried to use __declspec(dllexport), stdcall and WINAPI calling conventions. Each call I get an error message in VB6 saying "bad dll calling convention."
Win32 C function prototypes:
long WINAPI BitmapFile_Open(char *pszFileName);
void WINAPI BitmapFile_Close(long bmf);
note in the above I have tried several other calling conventions, including __declspec(dllexport) and stdcall, and neither work.
Def file:
LIBRARY ImageLib
EXPORTS
BitmapFile_Open @1
BitmapFile_Open @2
VB Global Module:
Declare Function BitmapFile_Open Lib "ImageLib.dll" (ByVal fileName As String) As Long
Declare Function BitmapFile_Close Lib "ImageLib.dll" (ByVal bmFile As Long)
VB Code:
Dim myFile As Long
myFile = BitmapFile_Open("test.bmp")
BitmapFile_Close (myFile)
Also note that in the original functions, the bmFile is actually an address (pointer to a structure) but in VB it will be represented as long. However, since VB6 doesn't support pointers, I am casting from long in the C code. I hope you can understand what I'm trying to get at here. It has nothing to do with the error that is occurring. Any help is appreciated.
Edit: I have used a dependency walker to determine that the functions are indeed being exported. VB6 is just not calling them without error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
BitmapFile_Close 应在 VB6 中声明为 Sub。我看不出还有什么问题。
请参阅 Microsoft 建议,了解编写从 VB 调用的 C DLL。最初随 VB5 一起发布,但仍与 VB6 相关。
BitmapFile_Close should be declared as a Sub in the VB6. I can't see anything else wrong.
Look at the Microsoft advice on writing C DLLs to be called from VB. Originally released with VB5 but still relevant to VB6.
尝试从有问题的参数(在声明部分内)中一一删除 ByVal,然后测试并尝试删除所有参数,然后再次测试。如果可以的话,进行增量测试并报告。那应该可以解决问题!
Try removing the ByVal from the arguments in question (inside the declaration section) one by one, then test and try removing for all arguments then test again. Do the incremental tests and report back if you can. That should do the trick!
使用 MIDL 为 DLL 生成类型库,然后 VB6 可以使用其类型信息而不是
Declare Function
例程。对于全局函数,我似乎记得您想要定义一个
库
和模块
。请参阅VB - 以隐式方式链接 DLL
Use MIDL to generate a type library for your DLL, then VB6 can use its type information instead of
Declare Function
routines.For global functions, I seem to recall that you want a
library
andmodule
defined.See VB - Linking a DLL in implicit way