Skip to main content
1 of 5
Shredderroy
  • 5.4k
  • 18
  • 28

MathLink error - MLGet out of sequence

I am trying to call Mathematica 10 from F#. Following the C# sample code found in the Mathematica directory, I have written the following F# code:

type TestClass() =
    member this.AddNumsWithWM (m : int) (n : int) : int =
        let ml = MathLinkFactory.CreateKernelLink()
        ml.WaitAndDiscardAnswer()
        ml.PutFunction("EvaluatePacket", 1)
        ml.PutFunction("testWMFunc", 2)
        ml.Put(m)
        ml.Put(n)
        ml.EndPacket()
        ml.WaitForAnswer() |> ignore
        let res = ml.GetInteger() // THE ERROR OCCURS HERE!
        ml.Close()
        res

The Mathematica function testFunc is simply:

testFunc[m_Integer, n_Integer] := m + 2 * n;

But when I call the function AddNumsWithWM 2 3, I get the following error at the line indicated above:

Wolfram.NETLink.MathLinkException was unhandled
Message: An unhandled exception of type 'Wolfram.NETLink.MathLinkException' occurred in Wolfram.NETLink.dll
Additional information: Error code: 3.

I don't understand, because the sample C#, from which this has been translated literally, works. Any idea why I would get the above error?

Thanks in advance for your help.

Shredderroy
  • 5.4k
  • 18
  • 28