Get "Attempted to read or write protected memory" Exception from generated DLL imported in C#
14 views (last 30 days)
Show older comments
I created a C-shared library DLL and this DLL is imported in C# Here is the sample code:
[DllImport(@"libmx.dll", CallingConvention = CallingConvention.Cdecl)]
private static unsafe extern IntPtr mxCreateDoubleScalar([In]double value);
[DllImport(@"CSharedLib.dll", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, SetLastError = false, CharSet = CharSet.Auto, EntryPoint = @"CSharedLibInitialize")]
private static unsafe extern void CSharedLibInitialize();
[DllImport(@"CSharedLib.dll", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, SetLastError = false, CharSet = CharSet.Auto)]
private static unsafe extern void mlfChi2cdf([In]Int32 nargout, ref IntPtr output, [In]IntPtr number1, [In]IntPtr number2);
IntPtr iValue = IntPtr.Zero;
CSharedLibInitialize();
IntPtr pointer1 = mxCreateDoubleScalar(1);
IntPtr pointer2 = mxCreateDoubleScalar(1);
IntPtr output = IntPtr.Zero;
mlfChi2cdf(1, ref output, pointer1, pointer2);
When I run into mlfChi2cdf, it throws out the AccessViolationException was thrown, "Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
Any idea? I think I follow the standard way to use the generated DLL
0 Comments
Answers (1)
Preetha Joy
on 25 Sep 2017
I also had the same error. Could you manage to find the solution ?
0 Comments
See Also
Categories
Find more on String Parsing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!