Re: Mathlink: How do I pass arbitrary data from Mathematica to C?
- To: mathgroup at smc.vnet.net
 - Subject: [mg86883] Re: Mathlink: How do I pass arbitrary data from Mathematica to C?
 - From: ragfield <ragfield at gmail.com>
 - Date: Tue, 25 Mar 2008 01:15:37 -0500 (EST)
 - References: <fs4rmb$p3s$1@smc.vnet.net>
 
On Mar 23, 1:00 am, Karen Bindash <KarenBind... at googlemail.com> wrote:
> Does that make sense? Any ideas how to pass arbitrary data to a C
> program?
You won't be able to do this directly.  You will have to write your
own C function that converts the data from a Mathematica type to the
raw data your function is expecting.  This is not very difficult.  Try
something like this (not tested):
/* MyFile.tm */
:Begin:
:Function: myFunc
:Pattern: MyFunc[reals:(_Real...)]
:Arguments: { reals }
:ArgumentTypes: Manual
:ReturnType: Manual
:End:
/* library function to call */
extern int g(const double* theData, int theLength);
void myFunc(void)
{
	double* theData;
	int* theDims;
	char** theHeads;
	int theDepth;
	if(MLGetReal64Array(stdlink, &theData, theDims, &theHeads,
&theDepth))
	{
		int theLength = theDims[0], i, theResult;
		for(i = 1; i < theDepth; i++)
			theLength *= theDims[i];
		theResult = g(theData, theLength);
		MLReleaseReal64Array(stdlink, theData, theDims, theHeads, theDepth);
		MLPutInteger(stdlink, theResult);
	}
	else
		MLPutSymbol(stdlink, "$Failed");
}