Here's a bit more detailed description on StackOverflow. I'll update this one tomorrow.
Avoid adding MATLAB to the PATH for Engine applications
1 view (last 30 days)
Show older comments
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?
Accepted Answer
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
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;
}
}
More Answers (0)
See Also
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!