By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,730 Members | 1,567 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,730 IT Pros & Developers. It's quick & easy.

Access Violation Exception Error

P: n/a
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

--
xblessing
Sep 19 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Sep 18, 7:34 pm, xblessing <xbless...@discussions.microsoft.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.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

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

--
Tom Shelton

Sep 19 '07 #2

P: n/a
"xblessing" <xb*******@discussions.microsoft.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.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

Sep 19 '07 #3

P: n/a
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" <xb*******@discussions.microsoft.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.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

Sep 21 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.