MATLAB Answers


Matlab shared library is not initialized in release directory from Visual C++/CLI program

Asked by Oliver
on 18 Sep 2013


I have programmed a small application in Visual C++/CLI (Visual Studio 2012 Professional) in which I call a Matlab function. This Matlab function has been compiled to a C++ shared library by the Matlab Compiler R2012a. I'm working with Windows 7, 64 Bit, but my program and library are compiled for 32 Bit.

Up to now I always started the program in the debug mode directly from Visual C++ and everything worked fine, especially the calling of the Matlab function did not give any problems. But now I wanted to compile the program in release mode and tried to start it separately from Visual studio. In the beginning there were some problems with Matlab DLLs (could not be found), so I simply copied all DLLs to the release directory (there may be some better method). Now the program seems to start, but when it tries to load my Matlab library it stops without any message. Here is a small code snippet which shows how I load my Matlab library:

try {
    MessageBox::Show("Before mclInitializeApplication");
      // initialize MCR 
      if (!mclInitializeApplication(NULL,0))  {
          MessageBox::Show("Can't initialize MCL");
      } else {
          MessageBox::Show("Before thermoMatlabLibInitialize");
          if(!thermoMatlabLibInitialize()) {
              MessageBox::Show("Could not initialize the thermoMatlab-library");
} catch (const mwException& e) {
    MessageBox::Show("Caught mwException");

The first two success-MessageBoxes are displayed ("Before mclInitializeApplication" and "Before thermoMatlabLibInitialize") but then neither "Could not initialize the thermoMatlab-library" nor "Caught mwException" is shown. Furthermore, there is no system error from Windows about the application either. The file "thermoMatlabLib.dll" is also contained in the release directory.

Does anyone know why this happens? Why is the exception not caught? Is it the wrong exception type or is there no exception at all?

Thank you very much in advance! Oliver


Show 1 older comment

No, unfortunately not. After mclInitializeApplication the return value of mclGetLastErrorMessage is an empty string and if I call it after thermoMatlabLibInitialize() it is not reached.

Is there anything I have to regard when using Matlab libs in release mode?

In Visual C++ I specified the location of the .h and .lib file. I also added the .lib-file in the "Additional dependencies" section of the linker settings. Do I have to do anything like this with the .dll-file?

The DLL file is loaded at run-time, so its location needs to be added to the system PATH environment variable.

I tried this, but it doesn't work either ... do you have any further ideas? Thanks for helping me!

Log in to comment.

1 Answer

Answer by Oliver
on 20 Sep 2013
 Accepted Answer

Ok, I did the setup of the whole Visual project once again from scratch and for some reasons it works now ... why ever. Thanks though.


Log in to comment.

Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!