.NET/Link API Version 1.6 USE FRAMES

IKernelLink Interface

The link interface that most programmers will use.

For a list of all members of this type, see IKernelLink Members.

[Visual Basic]
Public Interface IKernelLink
    Implements IMathLink
[C#]
public interface IKernelLink : IMathLink

Remarks

The IMathLink interface contains low-level methods for reading and writing data. IKernelLink extends the IMathLink interface, adding some higher-level methods that are appropriate on the assumption that the program on the other side of the link is a Mathematica kernel.

An example is the WaitAndDiscardAnswer method, which reads and discards the sequence of packets the kernel will send in the course of a single evaluation.

Do not forget, however, that all the methods in the IMathLink interface are also available for IKernelLinks.

Most programmers will use links that implement this interface. The actual implementation classes are not documented and are of no concern. You will always interact with link objects via an interface type. Use the MathLinkFactory.CreateKernelLink method to create an IKernelLink.

Here is a simple example program in C#:
using System;
using Wolfram.NETLink;

public class LinkTest {
    public static void Main(String[] args) {
    
        // This launches the Mathematica kernel:
        IKernelLink ml = MathLinkFactory.CreateKernelLink();
        
        // Discard the initial InputNamePacket the kernel will send when launched.
        ml.WaitAndDiscardAnswer();
        
        // Now compute 2+2 in several different ways.
        
        // The easiest way. Send the computation as a string and get the result in a single call:
        string result = ml.EvaluateToOutputForm("2+2", 0);
        Console.WriteLine("2 + 2 = " + result);
        
        // Use Evaluate() instead of EvaluateToXXX() if you want to read the result as a native type
        // instead of a string.
        ml.Evaluate("2+2");
        ml.WaitForAnswer();
        int intResult = ml.GetInteger();
        Console.WriteLine("2 + 2 = " + intResult);
        
        // You can also get down to the metal by using methods from IMathLink:
        ml.PutFunction("EvaluatePacket", 1);
        ml.PutFunction("Plus", 2);
        ml.Put(2);
        ml.Put(2);
        ml.EndPacket();
        ml.WaitForAnswer();
        intResult = ml.GetInteger();
        Console.WriteLine("2 + 2 = " + intResult);
        
        // Always Close when done:
        ml.Close();
    }
}
Here is the same program in Visual Basic .NET:
Imports Wolfram.NETLink

Public Class LinkTest
    Public Shared Sub Main(ByVal args As String())

        ' This launches the Mathematica kernel:
        Dim ml As IKernelLink = MathLinkFactory.CreateKernelLink()

        ' Discard the initial InputNamePacket the kernel will send when launched.
         ml.WaitAndDiscardAnswer()

        ' Now compute 2+2 in several different ways.

        ' The easiest way. Send the computation as a string and get the result in a single call:
        Dim result As String = ml.EvaluateToOutputForm("2+2", 0)
        Console.WriteLine("2 + 2 = " & result)

        ' Use Evaluate() instead of EvaluateToXXX() if you want to read the result
        ' as a native type instead of a string.
        ml.Evaluate("2+2")
        ml.WaitForAnswer()
        Dim intResult As Integer = ml.GetInteger()
        Console.WriteLine("2 + 2 = " & intResult)

        ' You can also get down to the metal by using methods from IMathLink:
        ml.PutFunction("EvaluatePacket", 1)
        ml.PutFunction("Plus", 2)
        ml.Put(2)
        ml.Put(2)
        ml.EndPacket()
        ml.WaitForAnswer()
        intResult = ml.GetInteger()
        Console.WriteLine("2 + 2 = " & intResult)

        'Always Close when done:
        ml.Close()
     End Sub
End Class

Requirements

Namespace: Wolfram.NETLink

Assembly: Wolfram.NETLink (in Wolfram.NETLink.dll)

See Also

IKernelLink Members | Wolfram.NETLink Namespace | MathLinkFactory | IMathLink