9

编辑:让它在 32 位下工作,我现在正试图让它在 64 位下工作。我已经获得了 DLL 的源代码,并且 DLL 和应用程序都被编译为 64 位。我每次都遇到访问冲突。这是 DLL 代码(Visual Studio 2005中的 C++ ):

#pragma pack( push, 2 )
// Output Results Structure
typedef struct tagTVA_RESULTS {
    int   iID;             /* Difference ID 1 .. n */
    int   iLeft;           /* Bounding rectangle */
    int   iRight;
    int   iTop;
    int   iBottom;
    double dCx;            /* Center of gravity */
    double dCy;
    double dMajor;         /* Shape information */
    double dMinor;
    double dAngle;         /* Rotational information */
    int    lArea;          /* Number of pixels */
    int    iEdge;          /* Set if difference is at the edge of the image */
    double dNormalDensity;
    int    iNormalCount;
    double dDifferenceDensity;
} TVA_RESULTS, *PTVA_RESULTS;
#pragma pack ( pop )

请注意,它将包设置为 2。我也尝试在应用程序中将其设置为 2,但失败了。我尝试了其他值,甚至尝试了不同的值。我已经尝试显式使用 4 作为整数大小和 8 作为双倍大小。但我会假设(知识有限)如果两个包装尺寸相同,它应该可以工作。

在这一点上,我怀疑该函数是如何被调用的。它的第一个参数是一个指向这些结构数组的指针。应用程序传入数组 ByRef 的第一个元素,我认为它可以实现这一点。但是有一个错误的指向数组的指针可以解释这些症状。这是DLL中的函数定义。

int WINAPI MNtvaAnalyzeVB (TVA_RESULTS *pResults, int iMaxCount)

我的老板建议这可能是一个大/小字节序问题,但如果它们都在同一个环境中编译,这似乎不太可能。

我该怎么办?

编辑结束>>>


我正在将 Visual Basic 6.0 应用程序转换为 VB.NET。我有几个结构可以传递给外部 DLL 文件。这不起作用,我感觉这是由于结构没有正确传递。

这是原始结构:

Public Type TVA_PARAMETERS
    iStandardFilterOnOff As Long
    iSampleFilterOnOff As Long
    iDifferenceFilterOnOff As Long
    iRotationCorrectionOnOff As Long
    iLocalCorrectionOnOff As Long
    iStandardAOIx As Long
    iStandardAOIy As Long
    iStandardAOIdx As Long
    iStandardAOIdy As Long
    iSampleAOIx As Long
    iSampleAOIy As Long
    iSampleAOIdx As Long
    iSampleAOIdy As Long
    iRepeatHorizontal As Long
    iRepeatVertical As Long
    dSensitivity As Double
    iMergeWidth As Long
    iMergeHeight As Long
    iMinimumDifferenceArea As Long
    iMaximumDifferenceArea As Long
End Type

如果我在 Visual Basic 6.0 中对该类型的变量执行 LenB,我会得到 84 个字节。(注意:我不确定这是否是确定其大小的有效方法。)

因此,我试图将其转换为 VB.NET:

Public Structure TVA_PARAMETERS
    Public iStandardFilterOnOff As Integer
    Public iSampleFilterOnOff As Integer
    Public iDifferenceFilterOnOff As Integer
    Public iRotationCorrectionOnOff As Integer
    Public iLocalCorrectionOnOff As Integer
    Public iStandardAOIx As Integer
    Public iStandardAOIy As Integer
    Public iStandardAOIdx As Integer
    Public iStandardAOIdy As Integer
    Public iSampleAOIx As Integer
    Public iSampleAOIy As Integer
    Public iSampleAOIdx As Integer
    Public iSampleAOIdy As Integer
    Public iRepeatHorizontal As Integer
    Public iRepeatVertical As Integer
    Public dSensitivity As Double
    Public iMergeWidth As Integer
    Public iMergeHeight As Integer
    Public iMinimumDifferenceArea As Integer
    Public iMaximumDifferenceArea As Integer
End Structure

在 VB.NET 中,System.Runtime.InteropServices.Marshal.sizeof()给出 88 个字节。我希望因为这些只是数值,所以这会起作用(我知道字符串可能会很痛苦)。我没有外部函数的代码,但它是这样声明的:

Declare Function MNtvaParameters Lib "MNTva.dll" (ByRef pParameters As TVA_PARAMETERS) As Integer

我猜这个结构的大小不一样,所以 DLL 文件调用失败,但我没有收到错误,正如我所说,我没有代码来查看它。它返回一个零,如果它成功则应该返回一个零,但显然它实际上没有效果。

我已经玩了一些Runtime.InteropServices.StructLayoutAttribute,但如果这是答案,我无法确定正确的参数。

我有另一个这样的结构,但它是如此相似。我猜如果我能解决这个问题,我就能解决另一个问题。

4

2 回答 2

8

当然,我尝试的下一件事就是解决了这个问题。像这样定义结构:

<Runtime.InteropServices.StructLayout(Runtime.InteropServices.LayoutKind.Sequential, Pack:=1)> _
Public Structure TVA_PARAMETERS
  Public iStandardFilterOnOff As Integer
  ...
  etc.

解决了这个问题。

于 2012-11-13T01:58:28.117 回答
3

这里有很好的资源:

您的转换看起来不错,longVisual Basic 6.0 中的 a 是 4 个字节,即 32 位,在 VB.NET 中是一个整数。在 VB.NET 和 Visual Basic 6.0 中双精度都是 8 个字节(根据上述文章)。我还在 VB.NET 中获得了 84 个字节。

Option Strict On

Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim A As New TVA_PARAMETERS
        MsgBox(A.ByteCount.ToString)
    End Sub

    Public Structure TVA_PARAMETERS
        Public iStandardFilterOnOff As Int32
        Public iSampleFilterOnOff As Int32
        Public iDifferenceFilterOnOff As Int32
        Public iRotationCorrectionOnOff As Int32
        Public iLocalCorrectionOnOff As Int32
        Public iStandardAOIx As Int32
        Public iStandardAOIy As Int32
        Public iStandardAOIdx As Int32
        Public iStandardAOIdy As Int32
        Public iSampleAOIx As Int32
        Public iSampleAOIy As Int32
        Public iSampleAOIdx As Int32
        Public iSampleAOIdy As Int32
        Public iRepeatHorizontal As Int32
        Public iRepeatVertical As Int32
        Public dSensitivity As Double
        Public iMergeWidth As Int32
        Public iMergeHeight As Int32
        Public iMinimumDifferenceArea As Int32
        Public iMaximumDifferenceArea As Int32

        Function ByteCount() As Integer
            Dim Results As New List(Of Byte)
            AddBytesToList(Results, BitConverter.GetBytes(iStandardFilterOnOff))
            AddBytesToList(Results, BitConverter.GetBytes(iSampleFilterOnOff))
            AddBytesToList(Results, BitConverter.GetBytes(iDifferenceFilterOnOff))
            AddBytesToList(Results, BitConverter.GetBytes(iRotationCorrectionOnOff))
            AddBytesToList(Results, BitConverter.GetBytes(iLocalCorrectionOnOff))
            AddBytesToList(Results, BitConverter.GetBytes(iStandardAOIx))
            AddBytesToList(Results, BitConverter.GetBytes(iStandardAOIy))
            AddBytesToList(Results, BitConverter.GetBytes(iStandardAOIdx))
            AddBytesToList(Results, BitConverter.GetBytes(iStandardAOIdy))
            AddBytesToList(Results, BitConverter.GetBytes(iSampleAOIx))
            AddBytesToList(Results, BitConverter.GetBytes(iSampleAOIy))
            AddBytesToList(Results, BitConverter.GetBytes(iSampleAOIdx))
            AddBytesToList(Results, BitConverter.GetBytes(iSampleAOIdy))
            AddBytesToList(Results, BitConverter.GetBytes(iRepeatHorizontal))
            AddBytesToList(Results, BitConverter.GetBytes(iRepeatVertical))
            AddBytesToList(Results, BitConverter.GetBytes(dSensitivity))
            AddBytesToList(Results, BitConverter.GetBytes(iMergeWidth))
            AddBytesToList(Results, BitConverter.GetBytes(iMergeHeight))
            AddBytesToList(Results, BitConverter.GetBytes(iMinimumDifferenceArea))
            AddBytesToList(Results, BitConverter.GetBytes(iMaximumDifferenceArea))
            Return Results.Count
        End Function

        Sub AddBytesToList(ByRef List As List(Of Byte), addBytes As Byte())
            For Each B As Byte In addBytes
                List.Add(B)
            Next

        End Sub
    End Structure

End Class
于 2012-12-11T08:44:05.107 回答