MathGroup Archive 2005

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: simplified C API call in MathLink

  • To: mathgroup at smc.vnet.net
  • Subject: [mg55918] Re: simplified C API call in MathLink
  • From: "ragfield" <ragfield at gmail.com>
  • Date: Sun, 10 Apr 2005 06:54:49 -0400 (EDT)
  • References: <d3835t$cej$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

> I was wondering if Wolfram Research had plans for a simpler API for
mathlink.

The functionality of which you speak is very straightforward to
implement with MathLink by wrapping ToString[ToExpression[...],
InputForm] around the text you wish to evaluate.  In fact, I just did
it:

#include <stdio.h>
#include "mathlink.h"

MLEnvironment gEnvironment = NULL;

MLINK KernelStart()
{
	MLINK theLink = NULL;
	long err;

	if(gEnvironment == NULL)
	{
		gEnvironment = MLInitialize(0);
		if(gEnvironment == NULL)
			return theLink;
	}

	theLink = MLOpenString(gEnvironment, "-linkname
\"'/Applications/Mathematica 5.1.app/Contents/MacOS/MathKernel'
-mathlink\" -linkmode launch", &err);
	return theLink;
}

const char* KernelEvaluate(MLINK theLink, const char* theExpression)
{
	int thePacket;

	MLNewPacket(theLink);
	MLPutFunction(theLink, "EvaluatePacket", 1);
		MLPutFunction(theLink, "ToString", 2);
			MLPutFunction(theLink, "ToExpression", 1);
				MLPutString(theLink, theExpression);
			MLPutSymbol(theLink, "InputForm");
	MLEndPacket(theLink);

	while((thePacket = MLNextPacket(theLink)) != 0)
	{
		if(thePacket == RETURNPKT)
		{
			const char* theResult;
			if(!MLGetString(theLink, &theResult))
				theResult = NULL;
			return theResult;
		}
		MLNewPacket(theLink);
	}
	return NULL;
}

void KernelStop(MLINK theLink)
{
	if(theLink != NULL)
		MLClose(theLink);
}

int main(int argc, char* argv[])
{
	MLINK theLink = KernelStart();
	if(theLink != NULL)
	{
		int i;
		for(i = 1; i < argc; i++)
		{
			const char* theResult = KernelEvaluate(theLink, argv[i]);
			if(theResult != NULL)
			{
				printf("%s\n%s\n\n", argv[i], theResult);
				MLDisownString(theLink, theResult);
			}
		}

		KernelStop(theLink);
	}
	else
		fprintf(stderr, "Couldn't start kernel");
	return 0;
}

Then to build (on OSX):

cc evaluate.c -I"/Applications/Mathematica
5.1.app/AddOns/MathLink/DeveloperKit/Darwin/CompilerAdditions"
-L"/Applications/Mathematica
5.1.app/AddOns/MathLink/DeveloperKit/Darwin/CompilerAdditions" -lML
-Wno-long-double

> ps - thanks much for this mailing list. I just stumbled across it,
and it looks
> like its going to be invaluable going forward. Is it available in
digest mode?

I don't know about the regular mailing list, but you can get the posts
in digest mode through Google Groups
<http://groups-beta.google.com/group/comp.soft-sys.math.mathematica/>.

-Rob


  • Prev by Date: Re: Internet links/URLs/browser activation from with an Mathematica notebook?
  • Next by Date: Re: Re: immediate abort at error messages
  • Previous by thread: simplified C API call in MathLink
  • Next by thread: Re: Re: simplified C API call in MathLink