尝试读取或写入受保护的内存错误 [英] Attempted to read or write protected memory error

查看:83
本文介绍了尝试读取或写入受保护的内存错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!我正在尝试连接到外部dll,但始终收到错误消息:尝试读取或写入受保护的内存".谷歌很多,尝试了很多,但没有任何进展.请问有人有建议吗?
Joost.
这是代码:

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:

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;
      }
    }
  }  
}

推荐答案

在"private byte [] _Statebuffer = new byte [15360]"中可以看到,缓冲区大小为15360字节.可以在函数的注释中看到该声明:"short far pascal TMFirst(long session_handle,void far * state_buffer)".谢谢.
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.


_Statebuffer应该有多大?如果TMFirst超过了缓冲区大小,那就可以解释.
另外,TMFirst的本机声明是什么? ref byte[] 是正确的参数类型吗?
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?


您好,

您为TMFirst函数制作了错误的方法包装.在未指定缓冲区大小的情况下,请勿使用byte[]作为参数.由于您的functiona声明没有size参数,因此应使用IntPtr作为输入类型.它看起来应该是:

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:

[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);



问候,
Maxim.



Regards,
Maxim.


这篇关于尝试读取或写入受保护的内存错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆