Converting a Visual Basic 6.0 Type to VB.NET Structure

EDIT: Having got this to work under 32-bit, I am now trying to get it to work on 64-bit. I got the source code for the DLL, and both the DLL and the application are 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 ) 

Please note that installing the package on 2. I tried to install it on 2 in the application, and it does not work. I tried other values, and I even tried values ​​that were not the same. I tried explicitly using 4 as an integer size and 8 as a double size. But I would suggest (with limited knowledge) that if both package sizes are the same, they should work.

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

 int WINAPI MNtvaAnalyzeVB (TVA_RESULTS *pResults, int iMaxCount) 

My boss suggested this might be a big / little endian problem, but it seems unlikely if they are both compiled in the same environment.

What should I do?

End of editing β†’>


I am converting a Visual Basic 6.0 application to VB.NET. I have a couple of structures that are transferred to external DLL files. This does not work, and I feel it due to the incorrect transfer of structures.

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 LenB for a variable of this type in Visual Basic 6.0, I get 84 bytes. (NB: I'm not sure if this is the right way to determine its size.)

I tried converting it to 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 

In VB.NET, System.Runtime.InteropServices.Marshal.sizeof () gives 88 bytes. I was hoping these were just numerical values ​​that would work (I know that strings can be pain). I do not have code for an external function, but it was declared as follows:

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

I assume that this structure is not the same size, so the DLL file call fails, but I do not get an error, and, as I said, I do not have code to view it. It returns zero, as it should if it is successful, but it clearly has no effect.

I played a little with Runtime.InteropServices.StructLayoutAttribute , but if that is the answer, I cannot determine the correct parameters.

I have another structure similar to this one, but it is so similar. I guess I can fix it, I can fix another.

+8
vb6-migration
source share
2 answers

Well, of course, the very next thing that I tried to fix the problem. The structure definition is as follows:

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

fixed problem.

+8
source share

Here are some good resources:

Your conversion looks good, long in Visual Basic 6.0 is 4 bytes, which is 32 bits, which is an integer in VB.NET. 8 bytes are doubled both in VB.NET and in Visual Basic 6.0 (in accordance with the above articles). I also get 84 bytes in VB.NET.

 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 
+2
source share

All Articles