0

I am working on a MATLAB Engine application. In order for it to work, MATLAB needs to be added to the PATH environment variable so that the Engine application can find certain DLLs (libeng and libmx).

When MATLAB is registered as a COM server, it writes its location into the registry and I can access it. Using this information, is there a simple way to allow the Engine application to work without having to add MATLAB to the PATH? The Engine application could theoretically read the location of these DLLs from the registry, but the problem is that it won't even start up without those DLLs (when compiled the standard way, according to MATLAB Engine compilation instructions).

I am not very familiar with winapi and not at all familiar with COM.

Is there an easy solution to let the problem find its DLLs based on the information from the registry?

Szabolcs
  • 24,728
  • 9
  • 85
  • 174
  • Do you have more information of the environment you are developing in? What instructions are you using for MATLAB Engine compilation? – Guarita Apr 05 '13 at 21:14
  • If you have the CLSID of the component you are using, you may find the path to its DLL or EXE in //HKEY_CLASSES_ROOT/CLSID/{com server clsid}/InProcServer32/(Default) – Guarita Apr 05 '13 at 21:17
  • @Guarita I know where to find the path in the registry, the question is how to use this path so the program can find its DLLs. About compilation instructions, I have to admit that I do not fully understand the command line I use. I am mainly developing the program on OSX/Linux and when trying to figure out the correct compilation flags for Windows, I gave up, I ran the `mex` command in MATLAB in verbose mode for a simpler Engine application, and copied the flags it used. This is my command line: – Szabolcs Apr 06 '13 at 01:27
  • @Guarita `cl /O2 /EHsc /I$(CADDSDIR)\mldev64\include /I$(MATLABDIR)\extern\include /DMX_COMPAT_32 /DWIN32 /D_WINDOWS main.cpp /Fprogram.exe /link /MACHINE:X64 /NOLOGO /INCREMENTAL:no /PDB:NONE /SUBSYSTEM:windows /LIBPATH:$(MATLABDIR)\extern\lib\win64\microsoft libmx.lib libmat.lib libeng.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib` Actually it's a bit longer, but I removed the extra .cpp files and some other libraries from this. The DLLs that the program needs are – Szabolcs Apr 06 '13 at 01:28
  • @Guarita `libmx.dll`, `libmat.dll` and `libeng.dll`. These are different from the DLL which defines the MATLAB component and I can get from the registry, but they reside in the same directory. It is not MATLAB that needs these three DLLS, but the program I compile ("program.exe"). I assume that with these compilation flags the executable that is generated will load these DLLs automatically, while I should probably load them manually somehow (so I'd have the chance to figure out the path where I need to look for them first). – Szabolcs Apr 06 '13 at 01:30
  • @Guarita In the command line I posted, forget about `/I$(CADDSDIR)\mldev64\include`. That's for another library and I should have removed it. – Szabolcs Apr 06 '13 at 01:43
  • Oh, sorry, by 'instructions' I meant the documentation you were using to guide you. Why do you need to use it without adding the MATLAB root to the path? – Guarita Apr 08 '13 at 15:06
  • Btw, are you using these instructions? http://www.mathworks.com/support/solutions/en/data/1-78077S/?solution=1-78077S – Guarita Apr 08 '13 at 15:06
  • @Guarita Thanks for looking at this again. I found a solution finally, see my answer below. – Szabolcs Apr 08 '13 at 15:32

1 Answers1

2

I found the following solution:

If we use delayed DLL loading then we get a chance to set the DLL search path before the program attempts to load the DLLs.

To do this, the following needed to be added to the linker options: /DELAYLOAD:libmx.dll /DELAYLOAD:libeng.dll and it's necessary to link against delayimp.lib. After doing this, we can add a call to SetDllDirectory() with the path to the location of the libmx.dll and libeng.dll to the beginning of the main/WinMain function.

Szabolcs
  • 24,728
  • 9
  • 85
  • 174