472,142 Members | 1,073 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,142 software developers and data experts.

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

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Runtime.InteropServices;
  5.  
  6. namespace testing
  7. {
  8.     class Program 
  9.     {
  10.         // Thsi is the dll which i am importing
  11.         [DllImport ("encoder.dll", EntryPoint = "encoder")]
  12.         //this method takes 2 pointers as parameters and the result is stored in out_bits
  13.         public static extern unsafe void encoder (Int16* msg_bits, Int16* out_bits);
  14.  
  15.         static void Main(string[] args)
  16.         {
  17.             Byte i;
  18.             Int16[] msg_bit = new Int16[44];
  19.             Int16[] out_bit = new Int16[2 * 44];
  20.             //just filling the array with 1s
  21.             for (i = 0; i < 44; i++)
  22.             {
  23.                 msg_bit[i] = 1;
  24.                 Console.Write (msg_bit[i]);
  25.             }
  26.             //byte[] msg_bits = (byte) msg_bit; 
  27.             Console.WriteLine ("Testing begins");
  28.             unsafe
  29.             {
  30.                 //calling the dll function 
  31.                 fixed (Int16* pMsg_bit = msg_bit, pOut_bit = out_bit) encoder (pMsg_bit, pOut_bit);
  32.                 //Console.WriteLine ("Testing ends");  -->  here comes the error
  33. //--> 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
  34.             }
  35.             //commented these lines coz it doesnt work
  36.             //for (i = 0; i < 2*44; i++)
  37.             //{
  38.                 // I need the value from out_bit array               
  39.                //Console.Write (out_bit[i]);
  40.             //}
  41.  
  42.  
  43.         }
  44.     }
  45. }
hope to hear for u guys soon....
May 30 '07 #1
2 20465
Frinavale
9,735 Expert Mod 8TB
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
May 30 '07 #2
Plater
7,872 Expert 4TB
C# really doesn't do pointers very well.....

You could try to rewrite the DLL to have function such that
Expand|Select|Wrap|Line Numbers
  1. public static extern byte[] encoder (byte[] msg_bits);
  2.  
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.
May 30 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.