Clear Filters
Clear Filters

unable to mex a dll library to matlab code.

2 views (last 30 days)
Kaushik
Kaushik on 8 Nov 2012
i have the following simple code:
extern "C" __declspec(dllexport) double __cdecl MLtest(double arg1,double arg2)
{
return arg1+arg2;
}
i create a dll from it resulting in Test1.dll and Test1.lib.
Then i write a simple mex functions testML.c
include<mex.h>
#include<math.h>
#include<matrix.h>
#include<stdio.h>
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
double z =0.;
double *a = mxGetPr(prhs[0]);
double *b = mxGetPr(prhs[1]);
double *c = mxGetPr(prhs[2]);
*a = MLtest(*b,*c);
}
in matlab i mex it using:
mex testML.c Test1.lib
this result in testML.mex32 and testML.mex32.pdb files.
Finally i try to use the function as follows
xxx5=zeros(1);
xxx6=zeros(1);
xxx7=zeros(1);
xxx5(1) =2;
xxx6(1) =5;
testML(xxx7,xxx5,xxx6);
and i get en error message:
Invalid MEX-file 'C:\Work\testML\testML.mexw32': The specified module could
not be found.
I checked with dependency walker and se nothing missing (well wer.dll mpr.dll ieshims.dll is missing but those are delay load functions which dependency walker catches and should not cause problem). can someone help please.
  2 Comments
Walter Roberson
Walter Roberson on 8 Nov 2012
You are missing the # of #include<mex.h> but perhaps that was just a copy-and-paste problem.
Kaushik
Kaushik on 8 Nov 2012
i am not missing the "#". I have it in my original program. i missed it while copying. best

Sign in to comment.

Answers (2)

Walter Roberson
Walter Roberson on 8 Nov 2012
You declare MLtest rather than testML
  1 Comment
Kaushik
Kaushik on 8 Nov 2012
do not think this is the issue , after your comment i changed as follows:
made the original function TestDLL
extern "C" __declspec(dllexport) double __cdecl TestDll(double arg1,double arg2)
changed the c file name to TestDll.c which now contains:
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
double z =0.;
double *a = mxGetPr(prhs[0]);
double *b = mxGetPr(prhs[1]);
double *c = mxGetPr(prhs[2]);
*a = TestDll(*b,*c);
}
finally in the matlab code:
xxx5=zeros(1); xxx6=zeros(1); xxx7=zeros(1); xxx5(1) =2; xxx6(1) =5; TestDll(xxx7,xxx5,xxx6); xx8=9;
and i stll get the same error:
Invalid MEX-file 'C:\Work\AC QM - MOVE\AAA\v7_0_3\testML\TestDll.mexw32': The specified module could not be found.

Sign in to comment.


Kaustubha Govind
Kaustubha Govind on 8 Nov 2012
The error "The specified module could not be found." usually occurs when the MEX-file cannot find a dependency. In this case, your MEX-file needs Test1.dll, but is unable to find it. You either have not added it to the System path, or the DLL architecture does not match that of the MEX-file. For instance, your MATLAB appears to be 32-bit, so the DLL should also be 32-bit - perhaps it is 64-bit?
  3 Comments
James Tursa
James Tursa on 8 Nov 2012
Can you try putting the Test1.dll file in the current directory you are executing the TestDll routine from?
Kaustubha Govind
Kaustubha Govind on 12 Nov 2012
What James suggested should work. If you don't want to put the DLL in the same folder as the MEX-file, perhaps MATLAB is not picking up the right system PATH? Make sure that the paths returned by getenv('PATH') contain the folder containing the DLL.

Sign in to comment.

Categories

Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!