I am having a typical problem in using one of the native dll in C#
I'll explain what am trying to do, I've a dll written in C language which i am trying to include in my C# project, the solutions builds fine but once i try to run the program it gives an exception as "Attempted to read or write protected memory. This is often an indication that other memory is corrupt" I am passing two integer pointer variables as the parameters, one as input and the other stores the output, the function jus changes the format of the data from input. I tried using break points and tried to read the data in the immediate window and for my surprise i can get the values in the immediate window, but when am trying to write that into the console window it gives an error, not only the value if i try to write anything in the console or try to execute any statement after the function call it gives an error, I tried lots of solutions those were available in the net, like changing the compiler option and all.. but nothing works.. it wud b gud if anyone cud give me a solution for this... thanx in advance...
here goes the source code
Expand|Select|Wrap|Line Numbers
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Runtime.InteropServices;
- namespace testing
- {
- class Program
- {
- // Thsi is the dll which i am importing
- [DllImport ("encoder.dll", EntryPoint = "encoder")]
- //this method takes 2 pointers as parameters and the result is stored in out_bits
- public static extern unsafe void encoder (Int16* msg_bits, Int16* out_bits);
- static void Main(string[] args)
- {
- Byte i;
- Int16[] msg_bit = new Int16[44];
- Int16[] out_bit = new Int16[2 * 44];
- //just filling the array with 1s
- for (i = 0; i < 44; i++)
- {
- msg_bit[i] = 1;
- Console.Write (msg_bit[i]);
- }
- //byte[] msg_bits = (byte) msg_bit;
- Console.WriteLine ("Testing begins");
- unsafe
- {
- //calling the dll function
- fixed (Int16* pMsg_bit = msg_bit, pOut_bit = out_bit) encoder (pMsg_bit, pOut_bit);
- //Console.WriteLine ("Testing ends"); --> here comes the error
- //--> after executing this function if u keep a breakpoint and try to get the values in out_bit array i can get the values, but if u try to write it in console it gives an error : Attempted to read or write protected memory. This is often an indication that other memory is corrupt
- }
- //commented these lines coz it doesnt work
- //for (i = 0; i < 2*44; i++)
- //{
- // I need the value from out_bit array
- //Console.Write (out_bit[i]);
- //}
- }
- }
- }