473,597 Members | 2,258 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

1 New Member
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 20707
Frinavale
9,735 Recognized Expert Moderator Expert
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 Recognized Expert Expert
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
7111
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 System.Data.Common.UnsafeNativeMethods.OraMTSJoinTxn(OciEnlistContext pCtxt, IDtcTransaction pTrans) at System.Data.OracleClient.TracedNativeMethods.OraMTSJoinTxn(OciEnlistContext pCtxt, IDtcTransaction pTrans)
0
2058
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 get: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
3
11930
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 people posted, but since still cannot, pls give me some commnet.(this dll run smoothly at vb 6 ) module: ' example Declare Function TwTsxInitPar Lib "TSDKVC.DLL" _
1
3156
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 others people posted, but since still cannot, pls give me some commnet.(this dll run smoothly at vb 6 ) module: ' example
2
21968
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 exception of type 'System.AccessViolationException' occurred in mscorlib.dll Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt." The progran ends on a windows form designer...
2
4170
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 during the last 10 days. It happens mostly during startup or closing of the application, but sometimes just in the middle of nowhere. It doesn't point to anyting special. I don't get a clue... It's a Windows Forms VB.NET 2005 application. I roll...
6
49891
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, may be the records have increased and so i am getting this. anyone has any idea why and when do we get this. Its a windows based application with c# 2.0 and .Net Framework 2.0.
0
1796
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 tables from my db (MS Access db). DB is connnected properly and it works, but I'm unable to open DB and drag and drop tables on DataSet.xsd.... Finally, my goal is to make connection between CrystalReports and DataSet... It seems so far... :(
6
4483
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 exception occurs pointing to FileSaveDialog1.ShowDialog(). The exception also indicates some problem with system.drawing.dll. The exception text is: "Attempted to read or write protected memory. This is often an
0
7970
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7887
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8036
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8259
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5847
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5434
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3886
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3930
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1494
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.