Click here to Skip to main content
15,892,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi! I'm trying to interface to an external dll but keep getting the error: "Attempted to read or write protected memory". googled a lot and tried a lot, but no progress. Does any one have suggestions please?
Joost.
here's the code:
C#
namespace iButtonReader
{
  class ButtonReader
  {
    [DllImport("IBFS32.DLL")]
    public static extern long TMExtendedStartSession(int portnum, int porttype, object reserved);

    [DllImport("IBFS32.DLL")]
    public static extern int TMEndSession(long sessionhandle);

    [DllImport("IBFS32.DLL")]
    public static extern int TMSetup(long sessionhandle);

    // documentation shows:
    // short far pascal TMFirst(long session_handle, void far *state_buffer);
    [DllImport("IBFS32.DLL", CallingConvention = CallingConvention.Winapi)]
    public static extern int TMFirst(long sessionhandle, ref byte[] statebuffer); 

    //[DllImport("IBFS32.DLL")]
    //public static extern int TMNext(long sessionhandle, ref byte[] statebuffer);

    //[DllImport("IBFS32.DLL")]
    //public static extern int TMRom(long sessionhandle, ref byte[] statebuffer, ref int[] ROM);

    private long _Handle = 0;
    private byte[] _Statebuffer = new byte[15360];

    public string Read()
    {
      try
      {
        string rom = string.Empty;
        int rslt = 0;
        _Handle = TMExtendedStartSession(1, 5, null);
        if (_Handle != 0)
        {
          rslt = TMSetup(_Handle);
        }
        rslt = TMFirst(_Handle, ref _Statebuffer); // <---------- Attempted to read or write protected memory.
        if (rslt == 1)
        {
          // tbd
        }
        rslt = TMEndSession(_Handle);
        return rom;
      }
      catch (Exception ex)
      {
        Debug.Print(ex.ToString());

        throw;
      }
    }
  }  
}
Posted

As can be seen in "private byte[] _Statebuffer = new byte[15360]" the buffer size is 15360 bytes. The declaration can be seen in the comment of the function: "short far pascal TMFirst(long session_handle, void far *state_buffer)". thanks.
 
Share this answer
 
How big should _Statebuffer be? If TMFirst is exceeding the buffer size, that would explain it.
Alternatively, what is the native declaration of TMFirst? Is the ref byte[] the correct parameter type?
 
Share this answer
 
Hello,

You make wrong way wrapper for your TMFirst function. You should not use byte[] as argument without specifing size of the buffer. As your functiona declaration does not have size argument you should use IntPtr as input type. It should looks:

C#
[DllImport("IBFS32.DLL", CallingConvention = CallingConvention.Winapi)]
public static extern int TMFirst(long sessionhandle, IntPtr statebuffer);

long _Handle;
byte[] _Statebuffer;
// Allocate memory
IntPtr _ptr = Marshal.AllocCoTaskMem(_Statebuffer.Length);
// Copy Data to that buffer
Marshal.Copy(_ptr,_Statebuffer,0,_Statebuffer.Length);
// Call your method
TMFirst(_Handle,_ptr);
// Copy data back to your array
Marshal.Copy(_Statebuffer,0,_ptr,_Statebuffer.Length);
// Free Memory
Marshal.FreeCoTaskMem(_ptr);


Regards,
Maxim.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900