我们将一个 C++ 程序编译为 DLL,并希望从 VB6 中使用它。该程序有类似
的子程序int __stdcall setup(int exposure_time, double shutter, double gain, int numImages) {
....
}
int __stdcall test() {
return 8;
}
Def文件定义为:
LIBRARY
EXPORTS
setup=setup
test=test
我们在 VB6 中这样声明它们:
Public Declare Function setup Lib "C:\MyDll.dll" () As Long
Public Declare Function test Lib "C:\MyDll.dll" () As Long
然后尝试以一种形式访问:
Private Sub Form_Load()
Debug.Print (test())
End Sub
但是当执行到第一个函数调用时,我们在 VB 中得到“找不到文件”! MyDll.dll程序在声明的位置,不需要注册。缺少什么申报?
你好芭丝谢芭,
我听从了您的建议,但VB程序仍然找不到dll。
VB 中的声明:
Public Declare Function setup Lib "C:\Math\FlyCapture2\bin\PGLCTrigger.dll" ( _
ByVal exposure_time As Long, _
ByVal shutter As Double, _
ByVal gain As Double, _
ByVal numImages As Long) As Long
Public Declare Function test Lib "C:\Math\FlyCapture2\bin\PGLCTrigger.dll" () As Long
定义文件:
LIBRARY
EXPORTS
setup=@1
test=@2
C++程序:
__declspec(dllexport) int __stdcall setup(int exposure_time, double shutter, double gain, int numImages) {
....
}
__declspec(dllexport) int __stdcall test() {
return 8;
}
以及VB调用程序:
Private Sub Form_Load()
setup 12, 24#, 1#, 10
test
End Sub
一旦执行到上面程序中的设置行,就会出现“找不到 dll”错误。
我按照 Compile a DLL in C/C++, then call it from another program 的建议在 .def 文件中定义了以下内容:
//DLL Export-Import definitions
#ifdef BUILD_DLL
#define EXPORT __declspec(dllexport)
#else
#define EXPORT __declspec(dllimport)
#endif
这样我就可以将 DLL 中的函数引用为
EXPORT int __stdcall setup(int exposure_time, double shutter, double gain, int numImages)
但是 VS2010 会为导入生成错误消息。
所以我卡住了。任何进一步的帮助将不胜感激。谢谢。
最佳答案
其他人告诉您必须声明函数的参数。如果 DLL 不会加载,而您确定它在那里,那么它可能缺少依赖项。使用 Dependency Walker 调试它。加载可执行文件并从配置文件菜单以配置文件模式运行它。这将记录加载程序事件,您将看到失败的确切原因。
关于无法找到从 VB6 调用的 C++ DLL 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19120333/