Select to view content in your preferred language

Call Wolfram Mathematica code from ArcGIS Pro add-in with C#

452
2
04-20-2023 11:57 PM
MattiasWesterberg
New Contributor III

Hi,

I am trying to make a Wolfram Mathematica call from an ArcGIS Pro add-in with C# like this:

using ArcGIS.Desktop.Framework.Contracts;
using System;
using System.Diagnostics;
using Wolfram.NETLink;

namespace MyAddIn
{
    internal class MathematicaButton : Button
    {
        protected override void OnClick()
        {
            try
            {
                IKernelLink ml = MathLinkFactory.CreateKernelLink();
                ml.WaitAndDiscardAnswer();

                ml.Evaluate("2+2");
                ml.WaitForAnswer();
                int intResult = ml.GetInteger();
                
                ml.Close();

                ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("2 + 2 = " + intResult);
            }
            catch (Exception ex) 
            {
                Debug.WriteLine(ex.StackTrace);
                ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show(ex.StackTrace);
            }
        }
    }
}

I have included ml64i4.dll and Wolfram.NETLink.dll in my project.

When I run my add-in code I get this exception:

 

Exception thrown: 'System.TypeInitializationException' in Wolfram.NETLink.dll
at Wolfram.NETLink.Internal.NativeLink..ctor(String cmdLine)
at Wolfram.NETLink.MathLinkFactory.CreateKernelLink()
at MyAddIn.MathematicaButton.OnClick() in C:\Users\matti\Desktop\ArcGIS_Pro_add_in\MyAddIn\MathematicaButton.cs:line 14

Line 14 is:
IKernelLink ml = MathLinkFactory.CreateKernelLink();

If I run the same code in a console application it is working.
So my question is, what am I missing or doing wrong in the ArcGIS Pro add-in project?

Regards,
Mattias

0 Kudos
2 Replies
mahj
by
New Contributor II

Hi Mathias,

Have you tried right-clicking on the included ml64i4.dll file in Visual Studio and set Build Action to Content and Copy to Output Directory to Copy always (or Copy if newer)?

mahj_0-1683214776553.png

Best Regards,

/Markus

0 Kudos
MattiasWesterberg
New Contributor III

Yes. But it is still not working.

0 Kudos