MathGroup Archive 2009

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

Search the Archive

Re: A little C program calling MathLink

  • To: mathgroup at smc.vnet.net
  • Subject: [mg105167] Re: A little C program calling MathLink
  • From: 董理 <dongli2020 at gmail.com>
  • Date: Mon, 23 Nov 2009 06:51:26 -0500 (EST)
  • References: <4B0A0438.3000104@gmail.com>

¶­Àí дµÀ:
> Hi, all,
>
> I am learning how to call MathLink in C program, and I wrote a little
> one as following:
>
> "
> #include <stdio.h>
> #include <string.h>
> #include "mathlink.h"
>
> int main(void)
> {
> MLINK link;
> MLEnvironment env;
> int packet;
>
> int argc = 4;
> char * argv[5] = {
> "-linkname",
> "math -mathlink",
> "-linkmode",
> "launch",
> NULL
> };
> char user_msg[10];
> const char * math_msg;
>
> printf("What do you want to say to Mathematica? (10 characters only!)\n> ");
> scanf("%s", user_msg);
>
> /* setup connection with MathLink */
> env = MLInitialize(NULL);
> if(env == NULL) {
> printf("MLInitialize: internal error\n");
> return -1;
> }
> link = MLOpen(argc, argv);
> if(link == NULL) {
> printf("MLOpen: internal error\n");
> return -1;
> }
>
> /* check validity of connection */
> while(!MLReady(link)) continue;
> if(MLConnect(link) == 0) {
> printf("MathLink connection failed\n");
> return -1;
> }
>
> /* sending packet to MathLink */
> MLPutFunction(link, "EvaluatePacket", 1);
> MLPutFunction(link, "Print", 1);
> MLPutString(link, user_msg);
> MLEndPacket(link);
>
> /* receiving packets from MathLink */
> while((packet = MLNextPacket(link)) && packet != RETURNPKT)
> MLNewPacket(link);
> if(!packet) {
> printf("packets receiving error\n");
> MLClearError(link);
> } else {
> if(!MLGetSymbol(link, &math_msg)) {
> printf("MLGetSymbol: internal error\n");
> return -1;
> }
> printf("Echo message from Mathematica:\n");
> printf("%s\n", math_msg);
> MLReleaseSymbol(link, math_msg);
> }
>
> /* disconnect with MathLink */
> MLClose(link);
> MLDeinitialize(env);
>
> return 0;
> }
> "
>
> the "math_msg" is NULL when it is printed. What's wrong with it? Thanks
> for help? : )
>
> Best regards,
>
> DONG Li
>   
Hi, all,

I found what the problem is. The output of "Print" is Null. I do not
quite understand why it is this. Where is the symbol output going?

Best regards,

DONG Li


  • Prev by Date: Re: compositions with the restricted set of integers
  • Next by Date: Re: compositions with the restricted set of integers
  • Previous by thread: Re: A little C program calling MathLink
  • Next by thread: Kernel crashes in ReplaceAll - with or without Maximize failure