Calling the Library from Another Managed Language

Because the Arithmetic class in this sample is a garbage-collected (managed) class, it can be used by any managed language. It's just a matter of adding the reference (by browsing to the file that holds the assembly) and using the class. Here's how to do it in a C# console application:

  1. In Visual Studio, create a C# console application named CSUseLibrary .

  2. Add a reference to ManagedMathLibrary.dll (use the .NET tab and browse to C:\MAthLibrary , and then select the file).

  3. Enter the code for the test harness.

Some suitable testing code in C# follows :

 
 using System; namespace CSUseLibrary {    /// <summary>    /// Summary description for Class1.    /// </summary>    class Class1    {       /// <summary>       /// The main entry point for the application.       /// </summary>       [STAThread]       static void Main(string[] args)       {          ManagedMathLibrary.Arithmetic a = new ManagedMathLibrary.Arithmetic();          Console.Write("4.1 + 5.9 is ");          Console.WriteLine(a.Add(4.1, 5.9));          Console.ReadLine();       }    } } 

Some things to notice about C# in contrast to Managed C++:

  • The Main() function has a different signature than the _tmain() of a C++ console application. Because the code is generated, you don't need to remember the signature (or the attribute on the method), but you do need to remember that the C# Main() does not return a value.

  • C# uses . to separate a class name from a static method name , whereas C++ uses :: .

  • C# uses references to access managed data, and reaches members with the . operator, whereas C++ uses pointers and the -> operator.

This code compiles and runs with no further work on your part: Adding the reference takes care of everything. Binary compatibility, even across languages, is simple on the .NET runtime. It's just as simple from Visual Basic, assuming you're comfortable with the syntax.



Microsoft Visual C++. NET 2003 Kick Start
Microsoft Visual C++ .NET 2003 Kick Start
ISBN: 0672326000
EAN: 2147483647
Year: 2002
Pages: 141
Authors: Kate Gregory

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net