Click here to Skip to main content
15,867,756 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have
IntPtr
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);
Posted
Updated 26-Feb-18 21:44pm
Comments
TheRealSteveJudge 27-Feb-18 3:26am    
Which platform is the C++ dll? x86 or x64? What is the target platform of your C# solution?

1 solution

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!
 
Share this answer
 
Comments
ssyuvaraja 27-Feb-18 4:04am    
The problem is I am trying to convert "int iHeartBeat[4]" c++. To IntPtr of C#. Actually I have to create a "int* iHeartBeat=new int[4]" on c++ code. Its resolved. Thank you.

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