title: Convert Visual Basic 6.0 type to VB.NET 'Structure'
body: <p>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 <a href="http://en.wikipedia.org/wiki/Microsoft_Visual_Studio#Visual_Studio_2005" rel="nofollow">Visual&nbsp;Studio&nbsp;2005</a>):</p>
  
  <pre><code>#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 )
  </code></pre>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <pre><code>int WINAPI MNtvaAnalyzeVB (TVA_RESULTS *pResults, int iMaxCount)
  </code></pre>
  
  <p>My boss suggested it could be an <a href="https://en.wikipedia.org/wiki/Endianness" rel="nofollow">big/little endian</a> problem, but that seems unlikely if they're both being compiled in the same environment.</p>
  
  <p>What should I do?</p>
  
  <blockquote>
    <blockquote>
      <blockquote>
        <p>End of edit >>></p>
      </blockquote>
    </blockquote>
  </blockquote>
  
  <hr/>
  
  <p>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.</p>
  
  <p>Here's the original structure:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>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.)</p>
  
  <p>I have tried to convert it to VB.NET thusly:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>In VB.NET, <a href="https://msdn.microsoft.com/en-us/library/vstudio/system.runtime.interopservices.marshal.sizeof%28v=vs.100%29.aspx" rel="nofollow">System.Runtime.InteropServices.Marshal.sizeof()</a> 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:</p>
  
  <pre><code>Declare Function MNtvaParameters Lib "MNTva.dll" (ByRef pParameters As TVA_PARAMETERS) As Integer
  </code></pre>
  
  <p>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.</p>
  
  <p>I've played around a bit with <a href="https://msdn.microsoft.com/en-us/library/vstudio/system.runtime.interopservices.structlayoutattribute%28v=vs.100%29.aspx" rel="nofollow">Runtime.InteropServices.StructLayoutAttribute</a>, but if that's the answer, I cannot determine the right parameters.</p>
  
  <p>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.</p>