473,323 Members | 1,547 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,323 software developers and data experts.

Calling a MFC dll from C#: Protected Memory Error

Hello All,

I'm starting into the wide world of using unmanaged C++ dlls in my C# programs, and I've run into sort of an issue. I've used the win32 and user32 dlls with no problem in the past, but currently I'm trying to interface with a propriatary module, and getting protected Memory Errors whenever I try.

So, my DLL has two functions that I want to call:
bool ControllerInit()
and
Expand|Select|Wrap|Line Numbers
  1. bool InitIntrusion(
  2.     int            width,
  3.     int            height,
  4.     unsigned char    *regionOfInterest,
  5.     char            *iniPath,
  6.     int            engineID
  7. );
  8.  
the *regionOfInterest is an array of unsigned char where its size will be (row x cols) equal to the input image size. This can be set to null.

the iniPath can also be set to null.

C# Code
So, I set the DLLImports with:
Expand|Select|Wrap|Line Numbers
  1.         [DllImport("myDLL.dll", CharSet = CharSet.Ansi)]
  2.         public unsafe extern static bool InitIntrusion(
  3.             int width,
  4.             int height,
  5.             ref IntPtr regionOfInterest,
  6.             ref IntPtr iniPath,
  7.             int engineID
  8.             );
  9.        [DllImport("myDLL.dll")]
  10.         public extern static bool ControllerInit();
  11.  
When I call the ControllerInit(), I have no problem, and it correclty returns the bool. However, when I try calling the InitIntrusion:
Expand|Select|Wrap|Line Numbers
  1. IntPtr ROI = IntPtr.Zero;
  2. IntPtr INIPath = IntPtr.Zero;
  3. InitIntrusion(1, 1, ref ROI, ref INIPath, 0);
I get a System.AccessViolationException which says that:
"An attempt was made to read or write from protected memory. Other memory may be corrupted"

I have tried the following things to aleviate this problem:
Declaring code as unsafe
using ref and not using ref
using marshaled bytes ([MarshalAs(UnmanagedType.LPArray)] byte[])
using Marshaled string builder [MarshalAs(UnmanagedType.LPTStr)] stringbuilder

But I can't get rid of that error.

Does anyone have any suggestions?
I've been beating my head around this for days. :/

Thanks in advance.
Sep 11 '07 #1
0 1192

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

Similar topics

21
by: Joakim Hove | last post by:
Hello, I have implemented a small library with a function a datatype to manage temporary storage, and handle out correctly casted storage. The function to get a double pointer is for instance: ...
7
by: TT (Tom Tempelaere) | last post by:
Hi there The line marked *** doesn't compile. I wonder why the designers of C# decided to disallow this. Rationale? Are there plans to change this? This feature forces me to make things public...
5
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
9
by: hiralparikh | last post by:
Hi, I am using .NET 2.0 and trying to use a function from a native DLL file. Here is the syntax that I am using: definition: public static extern String getPwd(String strServerName, String...
4
by: Seok Bee | last post by:
Dear Experts, I have created a script to extract the Event Logs from the system into an excel sheet. The logs are separated into 2 worksheets (Application Log and System Log). After this excel...
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,...
3
by: sriram347 | last post by:
Hi I am a newbie to ASP.NET. I developed a web page (project type is web application) and I keep getting this error. B]Error message : "System.AccessViolation Exception attempted to read or...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.