This is documentation for Mathematica 3, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.2)
 Documentation / Mathematica / Add-ons / MathLink Library / MathLink Introduction  /

2.12.9 Two-Way Communication with External Programs

When you install a MathLink-compatible external program using Install, the program is set up to behave somewhat like a simplified Mathematica kernel. Every time you call a function in the external program, a CallPacket is sent to the program, and the program responds by sending back a result wrapped in a ReturnPacket.

  • This installs an external program, returning the LinkObject used for the connection to that program.
  • In[1]:= link = Install["bitsprog"]


  • The function ExternalCall sends a CallPacket to the external program.
  • In[2]:= ?bits


    bits[i_Integer] := ExternalCall[LinkObject["./bitsprog", 2, 2], CallPacket[0, {i}]]

  • You can send the CallPacket explicitly using LinkWrite. The first argument of the CallPacket specifies which function in the external program to call.
  • In[3]:= LinkWrite[link, CallPacket[0, {67}]]

  • Here is the response to the CallPacket from the external program.
  • In[4]:= LinkRead[link]


    If you use Install several times on a single external program, Mathematica will open several MathLink connections to the program. Each connection will however always correspond to a unique LinkObject. Note that on some computer systems, you may need to make an explicit copy of the file containing the external program in order to be able to call it multiple times.

    Identifying different instances of a single external program.

    :Function: addto

  • This gives $CurrentLink as an argument to addto.
  • :Pattern: addto[$CurrentLink, n_Integer]

    :Arguments: {n}
    :ArgumentTypes: {Integer}
    :ReturnType: Integer

  • This zeros the global variable counter every time the program is started.
  • int counter = 0;

    int addto(int n) {
    counter += n;
    return counter;

  • This installs one instance of the external program containing addto.
  • In[5]:= ct1 = Install["addtoprog"]


  • This installs another instance.
  • In[6]:= ct2 = Install["addtoprog"]


  • This adds 10 to the counter in the first instance of the external program.
  • In[7]:= addto[ct1, 10]


  • This adds 15 to the counter in the second instance of the external program.
  • In[8]:= addto[ct2, 15]


  • This operates on the first instance of the program again.
  • In[9]:= addto[ct1, 20]


    If an external program maintains information about its state then you can use different instances of the program to represent different states. $CurrentLink then provides a way to refer to each instance of the program.
    The value of $CurrentLink is temporarily set every time a particular instance of the program is called, as well as when each instance of the program is first installed.

    Sending a string for evaluation by Mathematica.

    The two-way nature of MathLink connections allows you not only to have Mathematica call an external program, but also to have that external program call back to Mathematica.
    In the simplest case, you can use the MathLink function MLEvaluateString() to send a string to Mathematica. Mathematica will evaluate this string, producing whatever effects the string specifies, but it will not return any results from the evaluation back to the external program.
    To get results back you need explicitly to send an EvaluatePacket to Mathematica, and then read the contents of the ReturnPacket that comes back.


  • This starts an EvaluatePacket.
  • MLPutFunction(stdlink, "EvaluatePacket", 1);

  • This constructs the expression Factorial[7] or 7!.
  • MLPutFunction(stdlink, "Factorial", 1);
    MLPutInteger(stdlink, 7);

  • This specifies that the packet you are constructing is finished.
  • MLEndPacket(stdlink);

  • This checks the ReturnPacket that comes back.
  • MLCheckFunction(stdlink, "ReturnPacket", &n);

  • This extracts the integer result for 7! from the packet.
  • MLGetInteger(stdlink, &ans);


    Sending a packet to Mathematica.

    At any stage in the execution of an external program, you can send EvaluatePacket[input] to Mathematica. In general, Mathematica may produce many packets in response, but the final packet should be ReturnPacket[output]. Section 2.12.12 will discuss how to handle sequences of packets and expressions whose structure you do not know in advance.