【问题标题】:Calling C++ DLL from VB6 passes garbage data for arguments?从 VB6 调用 C++ DLL 传递垃圾数据作为参数?
【发布时间】:2019-04-29 14:30:46
【问题描述】:
VC functions all use _stdcall
functions exported using .def file 
     (e.g.  AliasFuncName = _FuncName@NumCallArgBytes)
Existing C DLL was revised to have some new call arguments 
  (revised function names, built to new dll name)

Functions with unrevised call arguments work when calling the new DLL
Functions with revised call arguments do not work when calling the new DLL
   (all call arguments are garbage on entry)
Call arguments are several input doubles and a few return double*

Prototype.h call definition matches c source code definition
Visual Basic declarations to new DLL match in style those to the old DLL
   (several ByVal double input args and a few ByRef return args)

在调用 VC 调试器之前,参数在 VB 调试器中看起来不错(例如 1.34867e-308、3.49732e-88 等)。

如果您对可能的原因有任何想法,我将不胜感激。我已经为此苦苦挣扎了几天。顺便说一句,我不会选择使用遗留代码!

下面是 C 头文件原型、.DEF 定义和 VB 声明。

头文件定义:

LONG _stdcall SYSDll_FRoulSlideXa(
    double ATest, double Hc, double Hivr,
    double Eeq, double Rx, double Rk,
    double L, double U, double SlRol,
    double R, double Wlc, double Wpc,
    double Mu, double MuOil, double Cor2AL,
    double Fs, double Ft,
    double *FRoul, double *FSlid);

.DEF 文件定义:

LIBRARY "SYSx32d10a"

DESCRIPTION 'SYSx Dlls'

EXPORTS
    SYSDll_FRoulSlideXa = _SYSDll_FRoulSlideXa@144

VB6 声明:

Declare Function SYSDll_FRoulSlideXa Lib "SYSX32D10A.DLL" ( _
    ByVal ATest As Double, ByVal Hc As Double, ByVal Hivr As Double, _
    ByVal Eeq As Double, ByVal rx As Double, ByVal Rk As Double, _
    ByVal L As Double, ByVal U As Double, ByVal SlRol As Double, _
    ByVal r As Double, ByVal Wlc As Double, ByVal Wpc As Double, _
    ByVal Mu As Double, ByVal MuOil As Double, ByVal Cor2AL As Double, _
    ByVal Fs As Double, ByVal Ft As Double, _
    FRoul As Double, FSlid As Double)

注意:我已经在最后两个参数上尝试了显式 ByRef,而不是依赖默认的传递约定 ByRef

【问题讨论】:

    标签: vb6 visual-c++-6


    【解决方案1】:

    您的 VB Declare 不包含函数的返回类型。除非您没有显示DEFxxx 语句,否则这意味着VB 需要Variant。因为Variant 函数使用隐藏参数返回它们的值,所以堆栈将错位。仅此一项就可以导致您所看到的。

    解决方法是在VB中添加正确的返回类型Declare

    【讨论】:

    • 您可以在答案中添加VB6声明中函数的返回值应为As Long的信息。
    • 我不敢相信我错过了。添加返回类型解决了这个问题。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-04
    相关资源
    最近更新 更多