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
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;
}