C# .NET using C dll : Attempted to read or write protected memory.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • adypoly
    New Member
    • May 2007
    • 1

    C# .NET using C dll : Attempted to read or write protected memory.

    Hi guys...

    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

    Code:
    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]);
                //}
                 
    
            }
        }
    }
    hope to hear for u guys soon....
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Originally posted by adypoly
    Hi guys...

    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
    ...

    hope to hear for u guys soon....
    Have you throughly tested your C code?
    C code handles memory differently than .NET does. Make sure that all of your memory allocations are properly handled in the C code.

    -Frinny

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #3
      C# really doesn't do pointers very well.....

      You could try to rewrite the DLL to have function such that
      Code:
      public static extern byte[] encoder (byte[] msg_bits);
      or something similar, is a working def for it. encoder() will take in the message as a byte array (pretty much what your int16 pointer would be pointing too) and return the msg encoded as another byte[].

      Have you taken a look at System.Security .Authentication and System.Security .Cryptography ? Many algorithms have already been implemented.

      Comment

      Working...