Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Mathlink and C#

92 views
Skip to first unread message

Thomas Manz

unread,
Feb 20, 2004, 7:01:50 AM2/20/04
to
Hi!

Some time ago I programmed a own procedure in C and used it in Mathematica, what worked very well.
Now I would be interested in using C# but I can´t find a simple example neither for the Mathematca worksheet (which should be simple?) nor for a C# source code. I would expect something like adding two numbers, i. e. calling a procedure "mysum[2,3]" and getting 5.
The examples in the Mathematica installation (changing the process priority of the Mathkernel.exe) isn´t that what I would call simple!!

Does anyone have more information or even an example!

Best regards
Thomas Manz

Werner Schuster

unread,
Feb 20, 2004, 11:03:04 PM2/20/04
to
> C# but I can´t find a simple example neither for the Mathematca
> worksheet (which should be simple?) nor for a C# source code.

There are samples provided with .NETLink, for calling .NET from
Mathematica anf for evaluating Mathematica expressions from C#;

>I would
> expect something like adding two numbers, i. e. calling a procedure
> "mysum[2,3]" and getting 5. The examples in the Mathematica installation
> (changing the process priority of the Mathkernel.exe) isn´t that what I
> would call simple!!

Well, it actually is pretty simple. Have you read the .NETUserguide provided
with Mathematica or .NETLink?

Basically, what you have to do these steps:
With C#
- Write your code in a class and compile it into an Assembly;

In Mathematica:
- invoke Needs["NETLink`"] and InstallNET[] to get .NETLink up and running;
- Load the assembly (either by specifying the full pathname or by
using its name in the .NET installation (see the Userguide for details));
- LoadNETType with the name of your C# class;
- invoke the method;

For invoking methods, you have to distinguish between member and static
methods; read the Userguide for details on that (there is a section
named "Contexts and Visibility of Static Type Members" for that);


murphee
--
Werner Schuster (murphee)
Student of SoftwareEngineering and KnowledgeManagement
Maintainer of the OGO-JOGI Project @ http://ogo-jogi.sourceforge.net/
Blog @ http://www.jroller.com/page/murphee


Hans Michel

unread,
Feb 20, 2004, 11:18:20 PM2/20/04
to
Thomas:

If you have Mathematica 5.0 installed you should have the .NETLink
installed. If you do look in the source directory for C# samples.

"\Wolfram Research\Mathematica\5.0\AddOns\NETLink\Source"

If you don't have the .NETLink you can download it here

http://www.wolfram.com/solutions/mathlink/netlink/downloading.html

Good luck

Hans
"Thomas Manz" <thoma...@web.de> wrote in message
news:c14sve$roa$1...@smc.vnet.net...

Todd Gayley

unread,
Feb 22, 2004, 11:30:28 AM2/22/04
to
At 05:53 AM 2/20/2004, Thomas Manz wrote:
>Hi!
>
>Some time ago I programmed a own procedure in C and used it in
>Mathematica, what worked very well.
>Now I would be interested in using C# but I can´t find a simple example
>neither for the Mathematca worksheet (which should be simple?) nor for a
>C# source code. I would expect something like adding two numbers, i. e.
>calling a procedure "mysum[2,3]" and getting 5.
>The examples in the Mathematica installation (changing the process
>priority of the Mathkernel.exe) isn´t that what I would call simple!!
>
>Does anyone have more information or even an example!
>
>Best regards
>Thomas Manz


Thomas,

If you look in the Help Browser under the Add-ons & Links tab, you will
find the .NET/Link User Guide, which contains some simple examples. The
example notebooks you found are intended to be more significant
"real-world" examples that go beyond the fragments that are used in the
User Guide.

If you have a C# class "Foo" with a static method like this:

public static int mysum(int i, int j) { return i + j; }

Here is how you call it from Mathematica. First make sure .NET/Link is
loaded and running:

<<NETLink`
InstallNET[]

Now load your assembly and the Foo class:

LoadNETAssembly["c:\\path\\to\\fooassembly.dll"]
LoadNETType["Foo"]

Call the mysum function:

Foo`mysum[2, 3]

That's it.

If the method isn't static, use NETNew to create a new instance of the Foo
class, and call the mysum method on the object:

foo = NETNew["Foo"];
foo@mysum[2, 3]

Todd Gayley
Wolfram Research


0 new messages