Avoid adding MATLAB to the PATH for Engine applications

1 view (last 30 days)
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
Szabolcs on 6 Apr 2013
Edited: Szabolcs on 6 Apr 2013
Here's a bit more detailed description on StackOverflow. I'll update this one tomorrow.

Sign in to comment.

Accepted Answer

Friedrich
Friedrich on 6 Apr 2013
Edited: Friedrich on 6 Apr 2013
Hi,
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:
  2 Comments
Szabolcs
Szabolcs on 7 Apr 2013
Edited: Szabolcs 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
Friedrich on 8 Apr 2013
Edited: Friedrich 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 };
HINSTANCE hInstLibEng;
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\\");
printf("%s\n",oldPath);
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");
return EXIT_FAILURE;
}
_engEvalString(ep, "surf(peaks);");
_engEvalString(ep, "disp('Hello World');");
printf("Hit return to continue\n\n");
fgetc(stdin);
_engEvalString(ep, "close;");
/*
* We're done! Free memory, close MATLAB engine and exit.
*/
printf("Done!\n");
_engClose(ep);
FreeLibrary(hInstLibEng);
return EXIT_SUCCESS;
}
else
{
fprintf(stderr, "\nFailed to Load DLL\n");
return EXIT_FAILURE;
}
}

Sign in to comment.

More Answers (0)

Categories

Find more on Introduction to Installation and Licensing in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!