Marshal.copy抛出错误,试图读取或写入受保护的内存 [英] Marshal.copy throwing error as attempted to read or write protected memory

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

问题描述





我从c ++代码收到

Hi,

I have

IntPtr

变量。这是c ++代码中的整数数组,尝试将其转换为c#代码中的Interger数组。抛出错误,



variable received from c++ code. Which is integer array in c++ code, try to convert that to Interger array in c# code. Throwing error as,

"

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





我的尝试:



"

What I have tried:

public IntPtr iHeartBeat;




int[] value = new int[8];





以下行是一次投掷错误



The below line is the one throwing error

Marshal.Copy(iHeartBeat, value, 0, 8);

推荐答案

首先使用调试器来查看iHeartbeat中的内容 - 我猜它不是一个有效的指针值,而你需要仔细查看从哪里获得价值。要么它返回一个你忽略的错误代码,它没有被设置为任何东西,或者你调用C ++代码的方式有问题。



我们可以告诉它 - 它需要代码,并运行代码以找出任何信息,我们不能为您做任何事情!所以打破调试器,并开始寻找到底发生了什么!
Start by using the debugger to look at what is in iHeartbeat - I'm guessing that it's not a valid pointer value, and that you need to look at exactly where you get the value from. Either it's returning an error code that you are ignoring, it's not being set to anything, or it's a problem with the way you are calling the C++ code.

We can't tell - it needs the code, and to have the code running to find out any information and we can't do any of that for you! So break out teh debugger, and start looking for exactly what is going on!


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

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