2

I am calling a function from a native .NET dll like this:

string v = myDLL.GetValueFromString("header");
MessageBox.Show(v);

Upon execution of the program, I get this weird error (on the line which executes this function): "Attempted to read or write protected memory. This is often an indication that other memory is corrupt."

What does this error message mean? And is there a way to fix this problem?

3
  • 5
    This is usually an indication that your interop call is wrong. There is a way to fix it. Of course for this you will have to show the signature of the unmanaged function you are trying to invoke as well as the managed signature you defined for it. Commented Nov 20, 2011 at 10:54
  • Way too many possibilities. We'd need to see more code to help you debug this. Commented Nov 20, 2011 at 10:55
  • 1
    What is myDLL? What is its class and how you create this instance? Commented Nov 20, 2011 at 10:56

1 Answer 1

5

This is an AccessViolationException. It is a 'hard' exception, the processor actually crashes trying to execute the machine code. Usually because it is trying to access unmapped memory through a bad pointer value. It is all too common with native code, especially the kind of code that works with C strings.

I'm going to guess that you didn't write this code, you'll need help from the author. Send him a small test program that reproduces the problem. If you want a shot at debugging this yourself then you need the source code for the DLL and switch the debugger to mixed mode so that you can debug both your C# and the native code. Project + Properties, Debug tab, tick the "Enable unmanaged code debugging" option. Set a breakpoint in the native function you are calling.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.