Attempted to read or write protected memory.

Jun 11, 2009 at 6:23 PM

Hello,  First let me say that I am very impressed with this project.  I've worked with scanners of just about every make and model for years, but always through different access mediums (ISIS, Silver Bullet, etc...).  I am currently trying to retrieve the extended information from a canon CR180 check scanner, but when I do, I recieve the following exception.

"Attempted to read or write protected memory. This is often an indication that other memory is corrupt."

This occurs in twCapibility.vb, Function: m_ReadStr255

The code line throwing the exception is:  byt = Runtime.InteropServices.Marshal.ReadByte(fixPtr, Position)

 

I am stumped.  Any help that can be given on this is greatly appreciated.

Coordinator
Jun 11, 2009 at 7:52 PM

If I remember corrently there was a bug in the ReadStr255 function, but I thought I fixed it...  

but then maybe I only applied the fix to my internal code and had 'planned' to update the codeplex project when I did the other upgrades (thay I havent found time to do...) 

I'll check my local source code and post the readstr255 I have here.

Otherwise, glad to hear the feedback,

 

Coordinator
Jun 11, 2009 at 8:04 PM

...odd my code looks a little too much like the opentwain,.. there out to have been a comment for the bug thatI thought I fixed. In any case try this function, add a breakpoint before the loop and step through.  The only real difference is a try catch around the function and the parameter name is slightly different:

    Public Shared Function m_ReadStr255(ByVal ptr As IntPtr, ByRef Position As Int32) As String

        'Str255 = &HC                'TW_STR128

        'typedef unsigned char TW_STR255[256], FAR *pTW_STR255;

        Dim origPosition As Int32 = Position

        Dim byt As Byte

        Dim strData As New Text.StringBuilder(256)

        Dim strChar As Char

        If Not ptr = IntPtr.Zero Then

            Try

                For iStr As Int16 = 0 To 255

                    byt = Runtime.InteropServices.Marshal.ReadByte(ptr, Position)

                    strChar = DirectCast(ChrW(byt), Char)

                    If Not byt = 0 Then

                        strData.Append(strChar)

                    Else

                        Exit For

                    End If

                    Position += 1

                Next

                Position += 1

            Catch ex As Exception

                myTrace.WriteLine("Unable to read Str255", TraceLevel.Error)

                myTrace.Indent()

                myTrace.WriteLine(ex.Message, TraceLevel.Error)

                myTrace.Unindent()

            End Try

        Else

            myTrace.WriteLine("Unwilling to read Str255, IntPtr == IntPtr.Zero", _

                              TraceLevel.Error)

        End If

        Position = origPosition + 256

        Return strData.ToString

    End Function

Coordinator
Jun 11, 2009 at 8:07 PM

ah, but I found where I remember someone reporting this.  it was over on twainforum.org (search for m_ReadStr255 and you'll find the thread).  I said I'd put a try catch around it and then I never heard from the user again.  let me know how the debugging goes or if I can help in any other way,

.

Jun 11, 2009 at 8:37 PM

Ok... Dropping that function in caused a host of other issues.  It appears I am not running the latest version of the code.  The last one listed here was DEC 07.  Is there a more recent version that I can compile?  I checked the subversion repository, but it appears empty.

Also, would it help to tell you that it fails when trying to read one particular capability?  1213 4627 EndorsedText  causes the failure.  I tried a try catch earlier, and all it did was move the debug point of the failure to the ctwain.vb code that was calling the function.

Thank you for your help so far, I am sorry to be a bother.

Coordinator
Jun 12, 2009 at 11:02 AM

I've started to update the codeplex code branch at least three times but each time work comes up with a 6-8 month project and by the time I get my free time back I've had to re-start. 

I'll check if I've completed enough of the previous update build to post - worst case I'll get that function to work with the 07 release within a try-catch.  I wonder if the case is as I suspected earlier - that there was no text returned by the driver.  Hopefully one of the Kodak simulators has a str255 funtion I can reference, if I remember correctly the i65 simulator did...

You're no trouble, a bug is a bug.  You're just helping to find them,  I'll get back to this.

Jun 12, 2009 at 12:51 PM

I do appreciate it.

And I think you are correct about the driver not returning any text.  There is an imprinter function for the cr-180, and since it is not in use, nothing would be endorsed.  I thought I might be able to bypass the error by enabling the imprinter with blank text, but the driver will not allow without the print cartrige for the imprinter.

Again, thanks for your help.  Good luck, and let me know if I can provide any information/assistance in the matter.

Jun 12, 2009 at 3:19 PM

Ok,  I've implemented a simple null pointer check in the m_readstr255 function.

        Dim strChar As Char
        '****************************** begin catch 0 pointer 2009.06.12 -FB
        If Not CBool(fixPtr) Then
            Return " "
            Exit Function
        End If
        '****************************** end catch 0 pointer 2009.06.12 -FB
        For iStr As Int16 = 0 To 255

I found that returning a vbNullString caused an exception in the code that calls the function. 

While this may not be your ideal solution, it does seem to work, and gets me through the endorse text issue I was having. 

This leads me to a second question... however it should probably be a new thread, because it is not at all related to the error above.

Coordinator
Jun 12, 2009 at 9:01 PM

here's a little better version, shouldn't generate more errors.  Only real differencr was that the internal code had a custom tracelistener, sorry bout that.

In your version I'm pretty sure bad things will happen unless you take care of the 'Position' variable.  With it being passed ByRef, if i remember correctly, you need to made sure you update Position regardless if there was something found and failure to update Position sets the alignment off..

Try this one:

   Friend Shared Function m_ReadStr255(ByVal fixPtr As IntPtr, ByRef Position As Int32) As String
       
        Dim origPosition As Int32 = Position
        Dim byt As Byte
        Dim strData As New Text.StringBuilder(256)
        Dim strChar As Char

        If Not fixPtr = IntPtr.Zero Then

            Try

                For iStr As Int16 = 0 To 255

                    byt = Runtime.InteropServices.Marshal.ReadByte(fixPtr, Position)
                    strChar = DirectCast(ChrW(byt), Char)
                    If Not byt = 0 Then

                        strData.Append(strChar)

                    Else

                        Exit For

                    End If

                    Position += 1

                Next

                Position += 1

            Catch ex As Exception

                Trace.WriteLine("Unable to read Str255")
                Trace.WriteLine(ex.Message.ToString)

            End Try

        Else

            Trace.WriteLine("Unwilling to read Str255, IntPtr == IntPtr.Zero")

        End If

        Position = origPosition + 256

        Return strData.ToString


    End Function

 

Jun 15, 2009 at 12:43 PM

Yeah, I thought I had accounted for the position, but looks like I missed it.

Thanks for all your help on this.