Path: news.mathworks.com!not-for-mail
From: "Ashish Uthama" <first.last@mathworks.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: mex file wrapper for dll needing function handle
Date: Tue, 03 Mar 2009 15:48:33 -0500
Organization: TMW
Lines: 56
Message-ID: <op.up8he7c6a5ziv5@uthamaa.dhcp.mathworks.com>
References: <goimue$6j6$1@fred.mathworks.com>
NNTP-Posting-Host: uthamaa.dhcp.mathworks.com
Mime-Version: 1.0
Content-Type: text/plain; format=flowed; delsp=yes; charset=iso-8859-15
Content-Transfer-Encoding: 7bit
X-Trace: fred.mathworks.com 1236113314 865 172.31.57.126 (3 Mar 2009 20:48:34 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Tue, 3 Mar 2009 20:48:34 +0000 (UTC)
User-Agent: Opera Mail/9.63 (Win32)
Xref: news.mathworks.com comp.soft-sys.matlab:522227


On Tue, 03 Mar 2009 02:41:02 -0500, Todd Welti <twelti@harman.com> wrote:

>
> I'm trying to implement this idea from tech note 1-6C8PEQ, where a mex  
> file wrapper is used to get around the limitation in matlab that you  
> can't pass a function handle to a loaded dll library (which would be  
> used as a callback).  In my case the dll would return three doubles, x,  
> y, and z into "mycallback", which I woule like to return to Matlab  
> ("myMatCallback").  It would look like shown below.  I cant for the life  
> of me figure out how to get x,y and z into the format of prhs for hte  
> mexCallMatlab command.  Any suggetions appreciated.  I can compile the  
> code in general, except for anything that I have tried to get pointers  
> to x,y, and z and make the prhs array, generates errors like: 'cannot  
> convert parameter 1 from 'double' to 'const mxArray *'
>
> #include "mex.h"
> #include "myAPI.h"
>
> // Callback function
> void myCallback(double x, double y, double z)
> {
>
>
> /* THIS IS THE PART THAT HAS ME STUMPED*/
>
>
> mexCallMATLAB(0, NULL, 1, prhs,"myMatCallback");
> }
>
> // MEX Gateway
> void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray  
> *prhs[])
> {
>
> void (*cbPtr)(double x, double y, double z) = NULL;
> cbPtr = myCallback;
>
> // Assume that myAPIRoutine is implemented in myAPI.dll
> myAPIRoutine(cbPtr);
> }
>
>

mxCreateScalar will help you convert a double to a mxArray.

//air code!
mxArray* prhs[3];
mwSize dims[2]={ 1 1};
prhs[0]=mxCreateNumericArray(2,&dims, mxDOUBLE_CLASS,mxREAL);
mexGetPr(prhs[0])=x;
//..so on for y and z