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
- bool InitIntrusion(
- int width,
- int height,
- unsigned char *regionOfInterest,
- char *iniPath,
- int engineID
- );
the iniPath can also be set to null.
C# Code
So, I set the DLLImports with:
Expand|Select|Wrap|Line Numbers
- [DllImport("myDLL.dll", CharSet = CharSet.Ansi)]
- public unsafe extern static bool InitIntrusion(
- int width,
- int height,
- ref IntPtr regionOfInterest,
- ref IntPtr iniPath,
- int engineID
- );
- [DllImport("myDLL.dll")]
- public extern static bool ControllerInit();
Expand|Select|Wrap|Line Numbers
- IntPtr ROI = IntPtr.Zero;
- IntPtr INIPath = IntPtr.Zero;
- InitIntrusion(1, 1, ref ROI, ref INIPath, 0);
"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.