Access Violation Exception Error

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?eGJsZXNzaW5n?=

    #1

    Access Violation Exception Error

    Hello,
    I'm trying to communicate with a TEC Controller Newport 350B using their
    ..dll and VB.NET. I was able to use their example code with VB6. However,
    I've got following error when using VB.NET:
    "Access Violation Exception was unhandled. Attempted to read or write
    protected memory. This is often an indication that other memory is corrupt."
    I have no problem when send commands to my TEC Controller (using
    "SendAsciiA " - see below for the code). But, I always got error when trying
    to query the TEC Controller using "GetAsciiA. " Thanks in advance!

    Here is the declaration on my code:

    Declare Function InitSystem _
    Lib "M530DLL.dl l" () As Integer

    Declare Function SendAsciiA _
    Lib "M530DLL.dl l" Alias "SendAscii" _
    (ByVal deviceID As Integer, _
    ByVal command As String, _
    ByVal length As Integer) As Integer


    Declare Function GetAsciiA _
    Lib "M530DLL.dl l" Alias "GetAscii" _
    (ByVal deviceID As Integer, _
    ByVal response As String, _
    ByVal bytesRead As Integer) As Integer

    Here is the code to call the function:

    Public Function NPWriteRead(ByV al NPCommand As String, ByVal DeviceID As
    Short) As String
    Dim strRes As String = New String(" ", 64)
    Dim bytesRead As Integer
    Dim ret As Integer

    Call NPWrite(NPComma nd, DeviceID) 'this procedure will send the
    command to the instrument.
    ret = GetAsciiA(Devic eID, strRes, bytesRead) -this is where the
    error occurs

    If ret = M530USBINVALIDA DDRESS Then
    MsgBox("USB Address Invalid", vbOKOnly + vbCritical, "M530")
    ElseIf ret = M530ERROR Then
    MsgBox("USB Communication Error", vbOKOnly + vbCritical, "M530")
    ElseIf ret = M530USBADDRESSN OTFOUND Then
    MsgBox("USB Address not Found", vbOKOnly + vbCritical, "M530")
    End If
    Return Trim(strRes)
    End Function

    --
    xblessing
  • Tom Shelton

    #2
    Re: Access Violation Exception Error

    On Sep 18, 7:34 pm, xblessing <xbless...@disc ussions.microso ft.com>
    wrote:
    Hello,
    I'm trying to communicate with a TEC Controller Newport 350B using their
    .dll and VB.NET. I was able to use their example code with VB6. However,
    I've got following error when using VB.NET:
    "Access Violation Exception was unhandled. Attempted to read or write
    protected memory. This is often an indication that other memory is corrupt."
    I have no problem when send commands to my TEC Controller (using
    "SendAsciiA " - see below for the code). But, I always got error when trying
    to query the TEC Controller using "GetAsciiA. " Thanks in advance!
    >
    Here is the declaration on my code:
    >
    Declare Function InitSystem _
    Lib "M530DLL.dl l" () As Integer
    >
    Declare Function SendAsciiA _
    Lib "M530DLL.dl l" Alias "SendAscii" _
    (ByVal deviceID As Integer, _
    ByVal command As String, _
    ByVal length As Integer) As Integer
    >
    Declare Function GetAsciiA _
    Lib "M530DLL.dl l" Alias "GetAscii" _
    (ByVal deviceID As Integer, _
    ByVal response As String, _
    ByVal bytesRead As Integer) As Integer
    >
    Here is the code to call the function:
    >
    Public Function NPWriteRead(ByV al NPCommand As String, ByVal DeviceID As
    Short) As String
    Dim strRes As String = New String(" ", 64)
    Dim bytesRead As Integer
    Dim ret As Integer
    >
    Call NPWrite(NPComma nd, DeviceID) 'this procedure will send the
    command to the instrument.
    ret = GetAsciiA(Devic eID, strRes, bytesRead) -this is where the
    error occurs
    >
    If ret = M530USBINVALIDA DDRESS Then
    MsgBox("USB Address Invalid", vbOKOnly + vbCritical, "M530")
    ElseIf ret = M530ERROR Then
    MsgBox("USB Communication Error", vbOKOnly + vbCritical, "M530")
    ElseIf ret = M530USBADDRESSN OTFOUND Then
    MsgBox("USB Address not Found", vbOKOnly + vbCritical, "M530")
    End If
    Return Trim(strRes)
    End Function
    >
    --
    xblessing
    Could you post the VB6 sample... Most likely, there is a problem with
    your delarations.

    --
    Tom Shelton

    Comment

    • Armin Zingler

      #3
      Re: Access Violation Exception Error

      "xblessing" <xblessing@disc ussions.microso ft.comschrieb
      Hello,
      I'm trying to communicate with a TEC Controller Newport 350B using
      their .dll and VB.NET. I was able to use their example code with
      VB6. However, I've got following error when using VB.NET:
      "Access Violation Exception was unhandled. Attempted to read or
      write protected memory. This is often an indication that other
      memory is corrupt." I have no problem when send commands to my TEC
      Controller (using
      "SendAsciiA " - see below for the code). But, I always got error
      when trying to query the TEC Controller using "GetAsciiA. " Thanks
      in advance!
      [...]
      Declare Function GetAsciiA _
      Lib "M530DLL.dl l" Alias "GetAscii" _
      (ByVal deviceID As Integer, _
      ByVal response As String, _
      ByVal bytesRead As Integer) As Integer
      I guess it must be "ByRef bytesRead As Integer". Probably, in the VB6 code,
      there is neither ByVal nor ByRef. Unspecified, it's ByRef by default. In
      VB.Net, unspecified, it's auto-completed with ByVal.


      Armin

      Comment

      • =?Utf-8?B?eGJsZXNzaW5n?=

        #4
        Re: Access Violation Exception Error

        Tom and Armin,

        Thanks for the reply. I tried to change the last byVal to byRef. The code
        is working well now. Thanks a lot for your help.

        ------
        xblessing


        "Armin Zingler" wrote:
        "xblessing" <xblessing@disc ussions.microso ft.comschrieb
        Hello,
        I'm trying to communicate with a TEC Controller Newport 350B using
        their .dll and VB.NET. I was able to use their example code with
        VB6. However, I've got following error when using VB.NET:
        "Access Violation Exception was unhandled. Attempted to read or
        write protected memory. This is often an indication that other
        memory is corrupt." I have no problem when send commands to my TEC
        Controller (using
        "SendAsciiA " - see below for the code). But, I always got error
        when trying to query the TEC Controller using "GetAsciiA. " Thanks
        in advance!
        [...]
        Declare Function GetAsciiA _
        Lib "M530DLL.dl l" Alias "GetAscii" _
        (ByVal deviceID As Integer, _
        ByVal response As String, _
        ByVal bytesRead As Integer) As Integer
        >
        I guess it must be "ByRef bytesRead As Integer". Probably, in the VB6 code,
        there is neither ByVal nor ByRef. Unspecified, it's ByRef by default. In
        VB.Net, unspecified, it's auto-completed with ByVal.
        >
        >
        Armin
        >
        >

        Comment

        Working...