MathGroup Archive 2005

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

Search the Archive

Re: simplified C API call in MathLink

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

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

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

#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
-mathlink\" -linkmode launch", &err);
	return theLink;

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

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

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

void KernelStop(MLINK theLink)
	if(theLink != NULL)

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);

		fprintf(stderr, "Couldn't start kernel");
	return 0;

Then to build (on OSX):

cc evaluate.c -I"/Applications/Mathematica"
-L"/Applications/Mathematica" -lML

> 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


  • 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