Hi,
I migrated a VB6 ActiveX to Vb.Net and I am having an issue on API Calls.
The ActiveX consist of making API calls to read or write data from flat files.
It works pretty well in VB6, but throws an exception in Vb.Net : AccessViolationException
Let me know if you want me to post the working VB6 Code

Also, I apologize for my english skills. =)

Here is the VB.Net Code:

Code:
Module nif_types
'Define structure for RGETDAT_BITS procedure call
    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _
    Public Structure rgetdat_bits_data_str
        Dim type As Short 'data type (set internally)
        Dim file As Short 'file in database
        Dim rec As Short 'record in file
        Dim word As Short 'word offset in record
        Dim start_bit As Short 'start bit in word
        Dim length As Short 'number of bits
        Dim flags As Short 'flags
        Dim padding As Short
        Dim value As Short 'database value
        <VBFixedArray(2)> Dim padV() As Short
        Dim status As Short 'return status
        <VBFixedArray(2)> Dim padA() As Short
        Public Sub Initialize()
            ReDim padV(2)
            ReDim padA(2)
        End Sub
    End Structure
End Module

Module HSCNAPI

Public Declare Function rgetdat_bits Lib "hscnetapi.dll" Alias "rgetdat_bits_vb" (ByVal Server As String, ByVal num_points As Short, ByRef getdat_bits_data() As rgetdat_bits_data_str) As Short

End Module

Class MyControl

<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=255)> Dim ServerName As String ' For fixed length compatibility

 Public Sub GetServerName()
                ServerName = RegQuery("Honeywell", "Experion PKS Server", "Server", "ConnectedServerName")
 End Sub

 Public Function HSC_get_bits_data(ByVal iFile As Short, ByVal iRec As Short, ByVal iWord As Short, ByVal iStart As Short, Optional ByVal DataNumber As Integer = 1) As Array
            Dim ArrData(0) As rgetdat_bits_data_str
            Dim ArrReturn(0 To 100) As String
            Dim iWordCount As Integer = 0
            Dim iBitCount As Integer = 0
            Try
                For i As Integer = 0 To DataNumber - 1
                    ArrData(0).file = (iFile)
                    ArrData(0).rec = Convert.ToInt16(iRec)
                    ArrData(0).word = Convert.ToInt16(iWord + iWordCount)
                    ArrData(0).start_bit = Convert.ToInt16(iStart + iBitCount)
                    ArrData(0).length = Convert.ToInt16(1)
                    ArrData(0).flags = Convert.ToInt16(0)
                    rgetdat_bits(ServerName, Convert.ToInt16(1), ArrData) ' <------------------- Stacktrace shows this Exception happens on this line, which is the API Call
                    ArrReturn(i) = CStr(ArrData(0).value)
                    iBitCount += 1 ' Counter to increment the StartBit by 1: 1 bit = 1 bit. 
                    If iBitCount = 16 Then ' 0 to 15 represent 16 bits. When 16 bit count is reached, increment word and reset bit counter
                        iWordCount += 1
                        iBitCount = 0
                    End If
                Next
                Return ArrReturn
            Catch Ex1 As AccessViolationException
                'mylogWriter.WriteLine("Record Time: " & time.ToString(timeformat) & " :: " & "HSC_get_bits_data: " & Ex1.Message, Ex1.InnerException.ToString)
            Catch Ex As Exception
                'mylogWriter.WriteLine("Record Time: " & time.ToString(timeformat) & " :: " & "HSC_get_bits_data: " & Ex.Message, Ex.InnerException.ToString)
            End Try
            Return Nothing
        End Function
End Class
Thanks
Alex