3
$\begingroup$

I have three versions of Mathematica installed: 7.0.1, 11.0.1 and 11.1.1 but my test .NET/Link application will only use version 7. In Visual Studio I have referenced the Wolfram.NETLink.dll and included the ml32i4.dll from version 11.1.1 but still the MathKernel uses version 7. Is there some way I can control which version MathKernel uses?

using System;
using System.Linq;
using System.Windows.Forms;
using Wolfram.NETLink;

namespace NetLink_test_application
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MathKernel k = null;
            try
            {
                k = CreateKernel();
                k.Compute("Export[\"C:\\\\Users\\\\chrisd\\\\Documents\\\\test.txt\",$VersionNumber]");
                if (k.Messages.Any())
                {
                    string allMessages = "";
                    foreach (string msg in k.Messages)
                        allMessages += msg + Environment.NewLine;
                    foreach (string msg in k.PrintOutput)
                        allMessages += msg + Environment.NewLine;
                    MessageBox.Show(allMessages, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                //return k.Result.ToString();
            }
            catch
            {
                MessageBox.Show("Error connecting to Mathematica - please lose all open Mathematica windows, and then try again",
                                "Mathematica Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            finally
            {
                if (k != null)
                    k.Dispose();
            }
            //return "";
        }

        public static MathKernel CreateKernel()
        {
            MathKernel mathKernel = new MathKernel
            {
                AutoCloseLink = true,
                CaptureGraphics = true,
                CaptureMessages = true,
                CapturePrint = true,
                GraphicsFormat = "Metafile",
                GraphicsHeight = 0,
                GraphicsResolution = 80,
                GraphicsWidth = 0,
                HandleEvents = true,
                Input = null,
                Link = null,
                LinkArguments = null,
                PageWidth = 0,
                ResultFormat = MathKernel.ResultFormatType.OutputForm,
                UseFrontEnd = true
            };
            return mathKernel;
        }
    }
}

The program above runs the Mathematica code

Export["C:\\Users\\chrisd\\Documents\\test.txt", $VersionNumber]

producing a text file containing 7. confirming the version used.

Details of the .NET/Link reference and included ml32i4.dll

enter image description here

Solution courtesy of Szabolcs

Specify the link in the CreateKernel method as shown below:-

        public static MathKernel CreateKernel()
        {
            string mlArgs = "-linkmode launch -linkname \"C:\\\\Program Files\\\\Wolfram Research\\\\Mathematica\\\\11.1\\\\MathKernel.exe\"";
            IKernelLink ml = MathLinkFactory.CreateKernelLink(mlArgs);

            MathKernel mathKernel = new MathKernel
            {
                AutoCloseLink = true,
                CaptureGraphics = true,
                CaptureMessages = true,
                CapturePrint = true,
                GraphicsFormat = "Metafile",
                GraphicsHeight = 0,
                GraphicsResolution = 80,
                GraphicsWidth = 0,
                HandleEvents = true,
                Input = null,
                Link = ml,
                LinkArguments = null,
                PageWidth = 0,
                ResultFormat = MathKernel.ResultFormatType.OutputForm,
                UseFrontEnd = true
            };
            return mathKernel;
        }
$\endgroup$
4
$\begingroup$

I do not know any C#, but this seems to be answered in the documentation. Since I am reading the local copy, not an online version, I cannot post links. Here are screenshots instead:

enter image description here

enter image description here

Thus if you need to specify the location of the Mathematica kernel to use, pass the appropriate string to CreateKernelLink(). This is not simply the path to the kernel, but a set of MathLink arguments. See the example in the documentation.

You can create a MathKernel object from an IKernelLink.

$\endgroup$
  • $\begingroup$ Fantastic, thanks. I was looking at the Link property but couldn't find enough information. The screenshot says IKernelLink looks in the registry for the latest version. I had modified this to change my default version but had changed it back. $\endgroup$ – Chris Degnen Sep 28 '17 at 10:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.