NETLINK TUTORIAL

Introduction

.NET/Link

Welcome to .NET/Link, a product that integrates Mathematica and Microsoft's .NET platform. .NET/Link lets you call .NET from Mathematica in a completely transparent way, and allows you to use and control the Mathematica kernel from a .NET program. For Mathematica users, .NET/Link makes the entire .NET world an automatic extension to the Mathematica environment. For .NET programmers, .NET/Link turns Mathematica into a scripting shell that lets you experiment with, build, and test .NET classes a line at a time. It also makes .NET an ideal environment for writing programs that use the computational services of Mathematica.

.NET/Link's most unique feature is that it lets you load arbitrary .NET types into Mathematica and then create .NET objects and call methods, properties, and so on, directly from the Mathematica language. Thus, you can use Mathematica to "script" the functionality of an arbitrary .NET program—in effect, write a .NET program in Mathematica. Essentially anything you can do from .NET, you can now do from Mathematica, perhaps even more easily because you are working in a true interpreted environment.

.NET/Link also lets you do some very useful things that do not appear to directly involve the .NET runtime. These include calling C-style DLL functions directly from Mathematica, and creating and scripting COM objects, much like Visual Basic can do.

.NET/Link is designed for end-users and developers alike. The same features that let Mathematica users transparently call any .NET method also let developers create sophisticated commercial add-ons to Mathematica. Programmers who want to write custom front ends for Mathematica or use Mathematica as a computational engine for another program will find using .NET with .NET/Link is easier than using the traditional MathLink interface from C or C++.

Finally, .NET/Link comes with full source code. You can examine the code to supplement the documentation, get tips for your own programs, better understand how to use advanced features, or just see how it works.

Some familiarity with both the .NET Framework and Mathematica is assumed in this tutorial. In Part 2, which covers writing .NET programs that call Mathematica, major examples are generally provided in both C# and Visual Basic .NET versions, although overall the documentation is perhaps slightly more C#-centric. Naturally, when writing .NET programs that use .NET/Link, you can use any .NET-aware language, not just C# and Visual Basic .NET.

"Calling .NET from Mathematica" shows how you use .NET/Link to call .NET from Mathematica and "Calling Mathematica from .NET" shows how to call Mathematica from .NET.

What Is .NET?

.NET is a new development platform for Windows programming. It replaces essentially everything that came before it, including an entire alphabet soup of programming technologies such as MFC, COM, ActiveX, ATL, ASP, ADO, and many others. Although Microsoft emphasizes XML Web Services in conjunction with .NET, XML Web Services are only a small part of the .NET platform, and the success of .NET is not dependent on the widespread adoption of XML Web Services.

.NET represents the future of Windows programming, and Microsoft is rapidly shifting more and more of its technology and products to a .NET foundation.

At the core of .NET is a runtime engine, similar to that used by Java, that loads and executes programs compiled into special bytecodes that the runtime understands. This runtime is called the Common Language Runtime (CLR), but we will often refer to it as the .NET runtime. A key feature of this system is that many languages can be compiled into CLR bytecodes and executed by the runtime. This means that .NET is language neutral, supporting any programming language for which a .NET compiler is available. Microsoft provides compilers for C#, Visual Basic .NET, JScript, Visual J# .NET, and C++ With Managed Extensions. Many other compilers exist, including ones for Fortran, Perl, Python, Eiffel, and COBOL. You can even create a class in one .NET language, say Visual Basic .NET, and subclass it in another language.

Although .NET is language neutral, probably the two most important .NET languages are Visual Basic .NET, a modification of the Visual Basic language, and C#, a new language that is similar in many ways to Java.

What Is MathLink?

MathLink is Wolfram Research's protocol for sending data and commands back and forth between Mathematica and other programs. MathLink is the underlying glue that lets .NET and Mathematica talk to each other. When calling .NET from Mathematica, .NET/Link completely hides the low-level details of the MathLink communication, allowing Mathematica programmers to load and use .NET classes as if they were part of the Mathematica environment itself. When writing .NET programs that call Mathematica, .NET/Link provides a higher-level layer of functionality than the traditional C MathLink programming interface.

How Does .NET/Link Compare to J/Link?

J/Link is an existing Wolfram Research product that integrates Java and Mathematica in almost exactly the same way that .NET/Link integrates .NET and Mathematica. You can use J/Link to do many of the same things you can do with .NET/Link and vice versa. Because it is based on Java, J/Link has the advantage of being cross platform. If you want to write programs that run on every Mathematica platform, you should use J/Link. On the other hand, .NET integrates more tightly with the Windows operating system than Java does, so if you want to do Windows-specific things, or you want a very native Windows look and feel, you should use .NET/Link. On Windows, .NET/Link also does some things that J/Link cannot, such as allowing you to call C-style DLL functions directly from Mathematica, or controlling COM objects.

.NET/Link and J/Link provide a very similar programming model. Familiarity with one will be very helpful when working with the other.

New to Mathematica? Find your learning path »
Have a question? Ask support »