Issues in Integrating Matlab generated cpp dll in C# application
Afficher commentaires plus anciens
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 commentaires
Guillaume
le 13 Juin 2016
Presumably your Cpp shared library is actually a C shared library?
That is, all the exported functions in your dll are wrapped in extern "C" {}
Pure C++ shared dll can only be called by C++ code compiled with the same compiler and compiling options.
cbjan
le 13 Juin 2016
Kyle
le 16 Juin 2016
Since you are using C# to write your driver code, is it possible for you to compile your MATLAB code into a .NET assembly instead of a C++ shared library?
Guillaume
le 17 Juin 2016
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.
cbjan
le 21 Juin 2016
Réponses (0)
Catégories
En savoir plus sur Deploy to C++ Applications Using mwArray API (C++03) dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!