Re: Need Help! Mathlink C++ problems - solution
- To: mathgroup at smc.vnet.net
- Subject: [mg35960] Re: Need Help! Mathlink C++ problems - solution
- From: Mike Stenzler <mstenzler at ssaris.com>
- Date: Fri, 9 Aug 2002 05:18:09 -0400 (EDT)
- References: <aitfuk$co8$1@smc.vnet.net>
- Reply-to: mstenzler at ssaris.com
- Sender: owner-wri-mathgroup at wolfram.com
OK, I've solved the problem! It appears that I created the problem by attempting a shorthand way to address the mathematica kernel.exe itself. Rather than follow the whole long path down through C:\Program Files\... I created a subdirectory under my root: C:\mathlink. Into this directory I copied everything found in C:\Program Files\Wolfram Research\Mathematica\4.1\AddOns\MathLink\DeveloperKits\Windows, as well as math.exe, Mathematica.exe, MathKernel.exe and mathdll.dll. I then referenced the copy of mathkernel at this location in my call to MLOpenString(). This appears to be sufficient enough to instantiate a kernel process but not to actually do anything with it. I'm guessing (due to the small size of the executables) that mathematica.exe and mathkernel.exe are in reality only small "stub" programs that act as loaders for routines kept in separate executables or libraries and that the "pathing" the stub uses to find it's component parts is established (probably in the Windows registry) upon installation of Mathematica. In addition, I'm guessing that at least some of that pathing is implicitly relational (based upon following a chain downwards from where you currently "are") rather than explicit (chained off the root or "home" regardless current position). So, even though my call could get the kernel to wake up, it couldn't find it's subroutines and exited as soon as it needed to use them. I experiemnted by switching between the 2 MLOpenString() calls - 1 w/ the complete path (worky), the other with the short version (no worky). Now on to the real task at hand - passing lists and arrays to Mathematica for calculation of EigenValues and EigenVectors using EigenSystem. Any tips you can pass along on how to accomplish this would be appreciated. Mike Mike Stenzler wrote: > 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