Issues in Integrating Matlab generated cpp dll in C# application
조회 수: 3 (최근 30일)
이전 댓글 표시
Hi,
I have generated a cpp shared library using Matlab and integrated with C# application. I have started with a simple Matlab function to accept two numbers and return sum of those numbers. When I integrated with my C# application ,an Access Violation error is occurred and displays "Attempted to read or write protected memory"
Here is my C# code:
[DllImport(@"mclmcrrt8_3.dll")]
private static extern bool mclInitializeApplication_proxy(string options,Int32 count);
[DllImport(@"mclmcrrt8_3.dll")]
private static extern void mclTerminateApplication();
[DllImport("Add2nos.dll")]
private static extern bool Add2nosInitialize();
[DllImport("Add2nos.dll")]
private static extern void Add2nosTerminate();
[DllImport(@"mclmcrrt8_3.dll")]
private static extern IntPtr mxGetPr([In]IntPtr mxArray);
[DllImport(@"libmx.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr mxCreateDoubleScalar([In]double value);
[DllImport("Add2nos.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern void mlxAdd2nos(int nargout, ref IntPtr c, [In]IntPtr a, [In]IntPtr b);
mclInitializeApplication_proxy("NULL", 0);//initialized successfully
Add2nosInitialize();//initialized successfully
IntPtr inValA = mxCreateDoubleScalar( 3.0 );
IntPtr inValB = mxCreateDoubleScalar(4.0);
IntPtr outVal = IntPtr.Zero;
mlxAdd2nos(1, ref outVal,inVal, inValB); //Exception generates here
When the dll function is called "Access violation Exception" occurs
My matlab version is : R2014a 64 bit Visual Studio : 2015 64 bit
Can any one tell me whats wrong with my code?
Thanks
댓글 수: 5
Guillaume
2016년 6월 17일
This may only be a matter of semantics, but I'll say it again:
A C++ shared library can only be used by C++ code compiled with the same compiler and compiling options. It can't be used from .Net. It can't be used from matlab. If you do you'll likely get access violations. That is because the C++ ABI is not standardised.
A C shared library has no such problem. You can create a C shared library from C++ code as long as you wrap all exported functions in extern "C" and that those functions only use standard C types and do not throw exceptions.
답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Deploy to C++ Applications Using mwArray API (C++03)에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!