Convert Visual Basic 6.0 type to VB.NET 'Structure'

Dan Barron picture Dan Barron · Nov 13, 2012 · Viewed 10.5k times · Source

EDIT: Having gotten this to work under 32-bit, I'm now trying to get it to work for 64-bit. I've gotten source code for the DLL and both DLL and application are being compiled for 64-bit. I get an access violation every time. Here is the DLL code (C++ in Visual Studio 2005):

#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 )

Note it's setting the pack to 2. I've tried setting it to 2 in the application as well, and it fails. I tried other values, and I even tried values that weren't the same. I've tried explicit using 4 as integer size and 8 as double size. But I would assume (with limited knowledge) that if both pack sizes are the same, it should work.

At this point I'm suspecting how the function is called. Its first parameter is a pointer to an array of these structures. The application passes in the first element of the array ByRef, which I think accomplishes this. But having a bad pointer to the array would explain the symptoms. Here's the function definition in the DLL.

int WINAPI MNtvaAnalyzeVB (TVA_RESULTS *pResults, int iMaxCount)

My boss suggested it could be an big/little endian problem, but that seems unlikely if they're both being compiled in the same environment.

What should I do?

End of edit >>>


I am converting a Visual Basic 6.0 application to VB.NET. I have a couple of structures that get passed to external DLL files. This is not working, and I have a feeling it's due to the structures not being passed correctly.

Here's the original structure:

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

If I do a LenB on a variable of that type in Visual Basic 6.0, I get 84 bytes. (N.B.: I'm not sure if that's a valid way to determine its size.)

I have tried to convert it to VB.NET thusly:

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

In VB.NET, System.Runtime.InteropServices.Marshal.sizeof() gives 88 bytes. I was hoping since these are just numeric values this would work (I know strings can be a pain). I don't have code for the external function, but it's declared like this:

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

I'm guessing this structure is not the same size, so the DLL file call fails, but I get no error, and as I said, I don't have the code to look it. It returns a zero, as is should if it's successful, but it's clearly not actually having an effect.

I've played around a bit with Runtime.InteropServices.StructLayoutAttribute, but if that's the answer, I cannot determine the right parameters.

I have another structure like this, but it's so similar. I'm guessing if I can fix this one, I'll be able to fix the other.

Answer

Dan Barron picture Dan Barron · Nov 13, 2012

Well of course the very next thing I tried fixed the problem. Defining the structure like this:

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

fixed the problem.