MATLAB: Is it possible to get a handle to the current instance of MATLAB in a dll that I have loaded using LOADLIBRARY in MATLAB 7.7 (R2008b)

dllinstanceloadlibraryMATLAB

I am loading a DLL in MATLAB using the LOADLIBRARY function. I want to be able to use this same instance of MATLAB for function calls from within the DLL. I wish to pass a handle to the current running instance of MATLAB as an input to the function of the DLL.

Best Answer

It is not possible to pass the handle of the current instance of MATLAB to a DLL loaded from MATLAB.
To work around the issue, use mexCallMATLAB function from the C MEX API to call the current MATLAB instance. It can be done as follows:
1. Create a DLL with the appropriate function (containing a call to mexCallMATLAB).
2. To compile this DLL, include the required header files
mex.h, matrix.h and tmwtypes.h
from the include directory located at:
$MATLABROOT\extern\include\
where $MATLABROOT is the directory where MATLAB is installed
3. When linking, link against the following additional libraries :
libmx.lib and libmex.lib
located at:
$MATLABROOT\extern\lib\win32\microsoft (if using the Visual Studio for compiling).