Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help! - Error: Attempted to read or write protected memory

Status
Not open for further replies.

jcoira

Programmer
Sep 26, 2001
51
0
0
US
I am working with visual basic express. I am having trouble with a code that works fine in VB6, but is giving me the error "Attempted to read or write protected memory. This is often an indication that other memory has been corrupted" in vb express.

I need to use the following function from a dll. It takes as a parameter the address of an array of pointers to the rows in a 2D array.

'The function is declared as follows...
Private Declare Function Identify Lib "Dompin.dll" (ByRef Template As Long) As Long

'....
Public class A

Private Template(200, 9) As Byte '2D array of bytes

Private Sub Indent()
Dim ret as Long
Dim Buffer(9) As Long 'Will store pointers to each of the 10 rows of Template
For i = 0 To 9
Buffer(i) = VarPtr(Template(0, i))
Next i
ret = Identify(Buffer(0))

end sub

'VarPtr is not available in VB6 so VarPtr method is coded as follows...

Public Function VarPtr(ByVal o As Object) As Integer
Dim GC As GCHandle = GCHandle.Alloc(o, GCHandleType.Pinned)
Dim ret As IntPtr = GC.AddrOfPinnedObject().ToInt32
GC.Free()
Return ret
End Function

End Class

When I call Indent I get the stated error in the line "ret = Identify(Buffer(0))".

Can anyone help me on this?

Thanks!

 
Instead of
ret = Identify(Buffer(0))

You should pass in the address of the buffer:

ret = Identify(AddressOf Buffer(0))



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top