找不到从 VB6 调用C++ DLL 程序

C++ DLL program called from VB6 cannot be found

本文关键字:DLL 程序 C++ 调用 VB6 找不到      更新时间:2023-10-16

我们将一个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:MathFlyCapture2binPGLCTrigger.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:MathFlyCapture2binPGLCTrigger.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未找到"错误。

我按照在

C/C++ 中编译 DLL 的建议在 .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 无法加载,并且您确定它在那里,则它可能缺少依赖项。使用依赖关系步行者调试它。加载可执行文件并从"配置文件"菜单以配置文件模式运行它。这将记录加载程序事件,你将看到失败的确切原因。

你需要告诉 VB6 关于 setup 的函数参数:

Public Declare Function setup Lib "C:MyDll.dll" ( _
    ByVal exposure_time As Long, _
    ByVal shutter As Double, _
    ByVal gain As Double, _
    ByVal numImages A Long) As long

我认为您的 .def 文件不正确。我使用

EXPORTS
   setup @1
   test @2

其中 1 和 2 是任意但不同的正整数,称为序数。几点评论:

VB 中的Long是C++中的int

可以使用 __declspec(dllexport)extern "C" {/*your function here*/} 代替 .def 文件。