AccessViolationException on API Call
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: AccessViolationException on API Call

  1. #1
    Join Date
    Oct 2012
    Posts
    1

    AccessViolationException on API Call

    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

  2. #2
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: AccessViolationException on API Call

    I think we'll need to see the working Structure in the VB6 side of things...

    Sure it's a probably a bad structure problem...
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center