473,386 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 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 20660
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

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

Similar topics

3
by: OUSoonerTaz | last post by:
We are randomly getting this error message on our development and staging machines: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.; at...
0
by: Dhruba Bandopadhyay | last post by:
I have a problem. I have a C++ service and added a new API to it. I re-registered the service and re-added the reference in my ASP.NET Visual Web Developer. However when I call the new API I...
3
by: XJ | last post by:
Hi experts, i try to use vb.net 2005 call dll, then give me "Attempted to read or write protected memory.This is often an indication that other memory is corrupt". i have chk some message others...
1
by: ianyian | last post by:
Hi experts, i try to use vb.net 2005 call C++ dll, then give me "Attempted to read or write protected memory.This is often an indication that other memory is corrupt". i have chk some message...
2
by: Ilkka | last post by:
I have created an C++ application with Windows Forms, ADO and SQL server 2005. Now I need to change something and started debugging the code. Then suddenly I receive an error. "An unhandled...
2
by: Pieter | last post by:
Hi, Since 10 days (the first time was the 10th of november) I have some weird exception happening in an application here: All (except 1 of the total of 5) users had this error now 1 of 2 times...
6
by: Sugandh Jain | last post by:
Hi, I am getting the error message Attempted to read or write protected memory. This is often an indication that other memory is corrupt. It was not coming until yet, for around 2 months. Now,...
0
by: Mike | last post by:
How can i handle "attempted to read or write protected memory. this is often an indication that other memory is corrupt" I use VS 2005 framework 2.0 On the server explorer window I try to open...
6
by: Scott Gravenhorst | last post by:
Windows XP SP3 My application is set to open a SaveFile dialog when an exit is requested. When I click the app's close button, the save dialog opens, but when I click to change the folder, the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.