NGWS SDK Documentation  

This is preliminary documentation and subject to change.
To comment on this topic, please send us email at ngwssdk@microsoft.com. Thanks!

Creating and Using C# DLLs

A dynamic linking library (DLL) is linked to your program at run time. To demonstrate building and using a DLL, consider the following scenario:

Source files

File: Add.cs

// Add two numbers
using System; 
namespace MyMethods { 
   public class AddClass { 
      public static long Add(long i, long j) { 
         return(i+j); 
      }
   }
}

File: Mult.cs

// Multiply two numbers
using System; 
namespace MyMethods { 
   public class MultiplyClass { 
      public static long Multiply(long x, long y) { 
         return (x*y); 
      }
   }
}

File: MyClient.cs

// Calling methods from a DLL file
using System; 
using MyMethods;
class MyClient { 
   public static void Main(string[] args) { 
      Console.WriteLine("Calling methods from MyLibrary.DLL:"); 
      if (args.Length != 2) {
         Console.WriteLine("Usage: MyClient <num1> <num2>"); 
         return; 
      }
      long num1 = long.Parse(args[0]); 
      long num2 = long.Parse(args[1]); 
      long sum = AddClass.Add(num1, num2);
      long product = MultiplyClass.Multiply(num1, num2);
      Console.WriteLine("The sum of {0} and {1} is {2}",
                   num1, num2, sum); 
      Console.WriteLine("The product of {0} and {1} is {2}", 
                  num1, num2, product); 
   }
}

This file contains the algorithm that uses the DLL methods, Add and Multiply. It starts with parsing the arguments entered from the command line, num1 and num2. Then it calculates the sum by using the Add method on the AddClass class, and the product by using the Multiply method on the MultiplyClass class.

Notice that the using directive at the beginning of the file makes MyMethods namespace available to the file and, consequently, the unqualified class names can reference the DLL at compile time.

Compilation

To build the file MyLibrary.DLL, compile the two files Add.cs and Mult.cs using the following command line:

csc /target:library /out:MyLibrary.DLL Add.cs Mult.cs

The /target:library compiler option tells the compiler to output a DLL instead of an EXE file. The /out compiler option followed by a file name is used to specify the DLL file name. Otherwise, the compiler uses the first file (Add.cs) as the name of the DLL.

To build the executable file, MyClient.exe, use the following command line:

csc /out:MyClient.exe /reference:MyLibrary.DLL MyClient.cs

The /out compiler option tells the compiler to output an EXE file and specifies the name of the output file (MyClient.exe). This compiler option is optional. The /reference compiler option specifies the DLL file(s) that this program uses.

Execution

To run the program, enter the name of the EXE file, followed by two numbers, for example:

MyClient 1234 5678

Output

Calling methods from MyLibrary.DLL:
The sum of 1234 and 5678 is 6912
The product of 1234 and 5678 is 7006652

See Also

C# Language Tour