Access Violation Exception Error

G

Guest

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.dll" () As Integer

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


Declare Function GetAsciiA _
Lib "M530DLL.dll" 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(ByVal 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(NPCommand, DeviceID) 'this procedure will send the
command to the instrument.
ret = GetAsciiA(DeviceID, strRes, bytesRead) -> this is where the
error occurs

If ret = M530USBINVALIDADDRESS Then
MsgBox("USB Address Invalid", vbOKOnly + vbCritical, "M530")
ElseIf ret = M530ERROR Then
MsgBox("USB Communication Error", vbOKOnly + vbCritical, "M530")
ElseIf ret = M530USBADDRESSNOTFOUND Then
MsgBox("USB Address not Found", vbOKOnly + vbCritical, "M530")
End If
Return Trim(strRes)
End Function
 
T

Tom Shelton

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.dll" () As Integer

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

Declare Function GetAsciiA _
Lib "M530DLL.dll" 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(ByVal 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(NPCommand, DeviceID) 'this procedure will send the
command to the instrument.
ret = GetAsciiA(DeviceID, strRes, bytesRead) -> this is where the
error occurs

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

Could you post the VB6 sample... Most likely, there is a problem with
your delarations.
 
A

Armin Zingler

xblessing said:
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.dll" 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
 
G

Guest

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 said:
xblessing said:
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.dll" 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top