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

New to MATLAB?

Avoid adding MATLAB to the PATH for Engine applications

Asked by Szabolcs

Szabolcs (view profile)

on 5 Apr 2013

On Windows, when a MATLAB installation is registered as a COM server, it writes its location into the registry, so I can retrieve this location.

Using this information, is it possible to write a MATLAB Engine application which will work even if the required MATLAB DLL's are not in the PATH environment variable? Can it read their location from the registry and load them dynamically?

1 Comment


Szabolcs (view profile)

on 6 Apr 2013

Here's a bit more detailed description on StackOverflow. I'll update this one tomorrow.


Szabolcs (view profile)


1 Answer

Answer by Friedrich

Friedrich (view profile)

on 6 Apr 2013
Edited by Friedrich

Friedrich (view profile)

on 6 Apr 2013
Accepted answer


yes that possible but more a C/C++ question rather than MATLAB. You would need to look up HKEY_CLASSES_ROOT\MATLAB.Application registry entry:

Fetch the information from there and then built up the path to the MATLAB installation folder in order to use:

And maybe you need this also:



Szabolcs (view profile)

on 7 Apr 2013

Thanks for the reply, and yes, you are right that it's not as much a MATLAB question as a Windows/linking question ... I have no problems getting the value from the registry, and I guess I could manage the LoadLibrary function too. But the way the program is linked also has to be changed (so I can load the libraries "manually", with LoadLibrary, and not automatically on startup), and I'm really not sure how to get that right. I was hoping that someone has already figured this out for MATLAB, as I expected it to be a common problem for engine applications.

Have you ever done this?


Friedrich (view profile)

on 8 Apr 2013

A small example which needs some post processing to make it nice and stable can be found below (hardcoded path to 13a, but you say you can get the folder of MATLAB already, so I skip that part). You don't need to link against anything. You need to include the engine.h thats all:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "engine.h"
#include <Windows.h>
#define  BUFSIZE 256
int main()
typedef Engine* (*engOpen)(const char*);
typedef int (*engEvalString)(Engine*, const char*);
typedef int (*engClose)(Engine*);
char* oldPath = NULL;
DWORD buffSize = 65535;
//declare function pointers
engOpen _engOpen;
engEvalString _engEvalString;
engClose _engClose;
Engine *ep;
double time[10] = { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 };
oldPath = (char*) malloc(buffSize * sizeof(char));
//first modify the PATH, needed because the libeng.dll cant be loaded
//correctly because of a missing dependency
//a lot of proper error checking (Index out of bounds etc) needed here to make a stable code
//i leave that too you
if (!GetEnvironmentVariableA("PATH", oldPath, buffSize))
    fprintf(stderr, "\nCan't get PATH\n");
strcat(oldPath,";C:\\Program Files\\MATLAB\\R2013a\\bin\\win64\\");
SetEnvironmentVariableA( "PATH", oldPath );
//Load the libraries
hInstLibEng = LoadLibrary("C:\\Program Files\\MATLAB\\R2013a\\bin\\win64\\libeng.dll");
if (hInstLibEng){
//Get the procedure addresses
_engOpen = (engOpen)GetProcAddress(hInstLibEng, "engOpen");
_engEvalString = (engEvalString)GetProcAddress(hInstLibEng, "engEvalString");
_engClose = (engClose)GetProcAddress(hInstLibEng, "engClose");
if (!(ep = _engOpen(""))) {
fprintf(stderr, "\nCan't start MATLAB engine\n");
_engEvalString(ep, "surf(peaks);");
_engEvalString(ep, "disp('Hello World');");
printf("Hit return to continue\n\n");
_engEvalString(ep, "close;");
* We're done! Free memory, close MATLAB engine and exit.
fprintf(stderr, "\nFailed to Load DLL\n");

Friedrich (view profile)

Contact us