MATLAB: Having issue while using matlab exported .dll into visual studio c#.

MATLABnetnet assembly

I am trying to export a .dll (.Net Assembly) using Matlab R2013a ( with a simple add function which adds two numbers and trying to use this .dll file in visual studio 2013 for c#. I have added the reference to the .dll files for Matlabtest.dll and MWArray.dll. Now while I am trying to make the object of the Class "Adding" (the name of class from Matlabtest.dll) visual studio stops working without any error and unable to make the object of class. Please suggest me any solution with some reference code etc. What step i am missing need suggestions and help.
Here is my matlab function
function output = adding( a,b )
output = a + b;
Here is my C# code on a button click
using MathWorks.MATLAB.NET.Arrays;
using MathWorks.MATLAB.NET.Utility;
using MatlabTest;
namespace TestingMatlab
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
private MatlabTest.Adding obj;
public MainWindow()
private void btn_sum_Click(object sender, RoutedEventArgs e)
MWArray a;
MWArray b;
a = (MWArray)txt_num1.Text;
b = (MWArray)txt_num2.Text;
Adding obj = new Adding();
MWArray output = obj.adding((MWArray)a, (MWArray)b);
catch (Exception ex)

Best Answer

So, After Struggling I came back with a solution so that someone else may get help. The Problem was actually with MCR Initializer and "The MCR instance could not be initialized" for solving it i follow the following steps:
  • 1. Changed the Target framework to .NET Framework 3.5 in Visual Studio
  • 2. WhileExporting .dll using deploytool in Matlab goto settings and uncheck Embed CTF archive into the application.
  • 3. Now while exporting the.dll you would have a projectname.CTF file copy this file to your Visual Studio Debug Directory e.g. C:\Program\VS\Debug\projectname.CTF
  • 4. Check if you have exported .dll using x86 or x64 architecture and then select the Platform Target accordingly in the Visual Studio.
Follwing the steps above solves my problem...:)