Need Help! Mathlink C++ problems
- To: mathgroup at smc.vnet.net
- Subject: [mg35914] Need Help! Mathlink C++ problems
- From: Mike Stenzler <mstenzler at ssaris.com>
- Date: Thu, 8 Aug 2002 06:06:05 -0400 (EDT)
- Organization: SSARIS Advisors LLC
- Sender: owner-wri-mathgroup at wolfram.com
I'm trying to call the Mathematica kernel from within a C++ app.
I can open a connection but can't keep it open.
When I step thru my code, and reach the MLOpenString() call, an instance
of the mathematica kernel is created on my machine.
I can see it in the task bar and task mgr shows it running.
However, when I attempt to do anything with that link, the instance goes
away.
here's a code snippet (edited for clarity):
#include <mathlink.h>
/*
*********************************************
*********************************************
*/
void CMainFrame::OnMLTest()
{
long mlerr = 0L;
// this seems OK (env is !NULL upon return)
MLENV env = MLInitialize(0);
// function call below returns a NOT-NULL link and no error - also
seems OK
MLINK link = MLOpenString(env, "-linkname 'c:\\mlink\\mathkernel
-mathlink'", &mlerr);
// at this point kernel instance (link) has been created...
int mstat = MLActivate(link);
// anything past this point is useless as link closes as soon as the
call above is made
// even though the value in mstat implies the call succeeded - ie;
non-zero
// this holds true regardless of what call is made after MLOpenString()
// call could be MLConnect() or MLPutFunction() or MLFlush() or
MLReady()
// as soon as the newly created link is accessed
// it closes - oddly enough without error for all subsequent calls
// try to send 2+2 to the kernel for evaluation
if(mstat = MLPutFunction(link, "EvaluatePacket", 1L))
{
if(mstat = MLPutFunction(link, "Plus", 2))
{
mstat = MLPutInteger(link, 2);
mstat = MLPutInteger(link, 2);
}
mstat = MLEndPacket(link);
}
// all of the above function calls return w/o error
// dispose of unwanted packets - there's only one (a #17 SUSPENDPKT)
and no errors
while((pkt = MLGetNextPacket(link),pkt) && pkt != RETURNPKT)
{
mstat = MLNewPacket(link);
}
// when we get here pkt = 0 - we go to default and get error msg
saying:
// "the other side closed the link, you may still get undelivered data"
switch(MLGetNext(link))
{
case MLTKSYM:
.
.
.
default:
s.Format("%s", MLErrorMessage(link));
AfxMessageBox(s);
}
}
I'm linking with ml32i2m.lib using Visual C++ 6.0 under WinXPpro
Any thoughts?
Michael Stenzler
Chief Technology Officer
SSARIS Advisors LLC
Stamford CT
mstenzler at ssaris.com