6
$\begingroup$

I have read several posts at SE and googled a lot, but I still have difficulties to create a CDF via .NET.

My first try was the Wolfram example MathKernelApp.exe under FileNameJoin[{$InstallationDirectory, "SystemFiles", "Links", "NETLink", "Examples", "Part2", "MathKernelApp", "bin", "Release"}]

I was able to create a CDF via input:

  • UsingFrontEnd@ExportString[Table[2^i,{i,16}],"CDF"]
  • Export[FileNameJoin[{$TemporaryDirectory,"test.cdf"}],Table[2^i,{i, 16}]]

My second try was to reproduce it to compile a .NET DLL, which I can link to my SQL server. I have tested two different scripts, based on MathKernelApp and SimpleLink. The first one also captures Print and Message.

First Script:

using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.Xml;
using Wolfram.NETLink;
using System.Collections;
using System.ComponentModel;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Text;
using Microsoft.SqlServer.Server;

public class MathLinkCLR
{
    [SqlProcedure]
    public static int Call(SqlString MLInput, out SqlString MLOutResult, out SqlString MLOutMessage, out SqlString MLOutPrint)
    {
        string strInput = (string)MLInput;
        string strMLOutResult = null;
        string strMLOutMessage = null;
        string strMLOutPrint = null;
        MLOutResult = null;
        MLOutMessage = null;
        MLOutPrint = null;
        int iRV = -1;

        if (strInput != null) {

            /* Init MathLink */
            IKernelLink ml = MathLinkFactory.CreateKernelLink();
            MathKernel mathKernel;
            mathKernel = new MathKernel(ml);
            mathKernel.AutoCloseLink = true;
            mathKernel.CaptureMessages = true;
            mathKernel.CapturePrint = true;
            mathKernel.Input = null;
            mathKernel.LinkArguments = null;
            mathKernel.ResultFormat = MathKernel.ResultFormatType.InputForm;
            mathKernel.UseFrontEnd = true;

            /* Run */
            mathKernel.Compute(strInput);

            /* Results */
            strMLOutResult = (string) mathKernel.Result;
            foreach (string msg in mathKernel.Messages) strMLOutMessage += msg;
            foreach (string p in mathKernel.PrintOutput) strMLOutPrint += p;
            iRV = 0;

            /* Close MathLink */
            mathKernel.Dispose();
        }
        MLOutResult = strMLOutResult;
        MLOutMessage = strMLOutMessage;
        MLOutPrint = strMLOutPrint;
        return iRV;
    }
}

Second script:

using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.Xml;
using Wolfram.NETLink;
using System.Collections;
using System.ComponentModel;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Text;
using Microsoft.SqlServer.Server;

public class MathLinkCLR
{
    [SqlProcedure]
    public static int Call(SqlString MLInput, out SqlString MLOutResult)
    {
        string strInput = (string)MLInput;
        string strMLOutResult = null;
        MLOutResult = null;
        int iRV = -1;

        if (strInput != null) {

            /* Init MathLink */
            IKernelLink ml = MathLinkFactory.CreateKernelLink();
            ml.WaitAndDiscardAnswer();

            /* Run */
            strMLOutResult = ml.EvaluateToInputForm(strInput, 0);
            iRV = 0;

            /* Close MathLink */
            ml.Close();
        }
        MLOutResult = strMLOutResult;
        return iRV;
    }
}

I can compile both scripts to a DLL, which I can link to SQL via CREATE ASSEMBLY (and other Windows system DLLs as ASSEMBLYs). I can also execute various Mathematica code,

BUT

I cannot create any CDF output (directly, or via ExportString). The SQL proc either hangs or returns $Failed as standard Mathematica return value, without any hint.

I have tried .NET Framework v2.0.50727 and v4.0.30319. I always had to use UsingFrontEnd to prevent error message

FrontEndObject::notavail: A front end is not available; certain operations require a front end.Export::errelem: The Export element NotebookObject contains a malformed data structure and could not be exported to CDF format.

My questions are:

  • I have used the console tool csc.exe to compile. Do I need to use the Visual Studio directly? Do I need to add other references and special compiling options?
  • Do I need to load other Mathematica packages in my script, which are usually loaded by the front-end automatically?
  • I have, more or less, used the MathKernelApp example. Why it is working and my dll is not?

Any help or hint is greatly appreciated!


UPDATE

I made some more tests and also traced the system with the Sysinternals ProcMon. The MathKernel.exe is executing a CDF.exe and a CDF.jar in one of Mathematica's system folders. Both traces look similar, but the call from within the SQL Management Studio is not working.

Maybe someone could help me with the ProcMon trace files?

I have also tried to execute the compiled program from within the SQL Management Studio, but it's not working, too.

Any ideas how to proceed?

cmd

sql

$\endgroup$

1 Answer 1

1
$\begingroup$

SOLVED!

After struggeling and trying many different things, I got thre solution; thanks to Additive GmbH, Germany for helping!

The SQL server needs to interact with the desktop. Therefore, you need to modify the SQL server service on your machine:

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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