views:

67

answers:

1

Hello,

I am having some strange problem. I have written a small module in VC++ using OpenCV.

It works fine. The code aggregates feed from the CCTV camera connected to the USB port.

however, I had to write the rest of my application in C#, so I made a DLL of the VC++ code and called the VC++ method from C#.

Now, I have ended up getting an error

Attempted to read or write protected memory. 
This is often an indication that other memory is corrupt.

Can anyone please suggest me any solution to this. Is there any Access Violation while accessing it in a managed code?

+1  A: 

If TrackBlob returns a string, you should be able to define your dllimport as such:

[DllImport("Tracking.dll", EntryPoint = "TrackIt")] 
public extern static string TrackBlob();

and skip trying to marshal it.

By returning it as an IntPtr, you're trying to get a pointer into memory owned by the unmanaged DLL... returning it as a string will return a copy of the string for you to work with.

Let me know if that works! James

* Edit *

Try one of these:

[DllImport("Tracking.dll", EntryPoint = "TrackIt")] 
public extern static [MarshalAs(UnmanagedType.BStr)] string TrackBlob();

or

[DllImport("Tracking.dll", EntryPoint = "TrackIt")] 
public extern static [MarshalAs(UnmanagedType.AnsiBStr)] string TrackBlob();

Check out this MSDN link on string marshalling:
http://msdn.microsoft.com/en-us/library/s9ts558h.aspx

James B
My dll is actually returning BSTR, since C cannot have Strings. I tried your method, keeping the return type in the dll as BSTR. but same error.
James
Can you post the C++ method definition?
James B
Hi, the method definition is huge. It doesn't take any input parameters. Computes the image feed and returns a BSTR.
James
Hi, I modified the code and I am getting a new error. The new thread is posted at http://goo.gl/WxNT
James
Looks like your other question got answered... hope I was able to help!
James B