How can I get HINSTANCE from .mex?
7 views (last 30 days)
Show older comments
Good day.
My GUI contains the main .m file and .dll, which is used via .mex files, each one corresponding to a separate function from library.
Before now, each .mex contained LoadLibrary and Freelibrary functions, thus library was loaded and freed each time I called any of the dll functions.
Now I want to write 2 separate .mex files, first one loading the library and returning its HINSTANCE as output parameter so that I could use one loaded library for all functions. Second .mex must free .dll. Is it possible?
How can I return HINSTANCE form .mex into my .m file? What about type conversions?
This code returns the following error: "loadlib.cpp(45) : error C2664: 'mxCreateDoubleScalar' : cannot convert parameter 1 from 'HINSTANCE' to 'double' There is no context in which this conversion is possible "
void mexFunction(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[])
{
const mxArray* dll_filename = prhs[0];
HINSTANCE hinst = LoadLibrary((LPCSTR)mxArrayToString(dll_filename));
plhs[0] = mxCreateDoubleScalar(hinst);
}
0 Comments
Answers (1)
James Tursa
on 24 Jan 2012
It would easier if you use just one mex routine and passed in a flag that determined the behavior. However, if you insist on passing a HINSTANCE value between mex routines, you can do this by creating a mxINT64_CLASS variable and converting your HINSTANCE from an address to a long long (e.g., via a union) and stuffing it into the mxINT64_CLASS variable in one routine and vice-versa in the other routine. Regardless of how you do it, you will have to be careful to manage all of the cleanup required (if any) via a mexAtExit function.
0 Comments
See Also
Categories
Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!