How to: create and use C # DLL (C # programming guide)

Source: Internet
Author: User
 

The dynamic link library (DLL) is linked to the program at runtime. For instructions on how to generate and use DLL, see the following solution:

  • Mathlibrary. dll: Indicates the library file, which contains the methods to be called during running. In this example, dll contains two methods:AddAndMultiply.

  • Add. CS: Is the source file, which containsAdd (long I, long J)Method. This method returns the sum of parameters. IncludeAddMethodAddclassClass is namespaceUtilitymethods.

  • Mult. CS: Is the source file, which containsMultiply (long X, long y)Method. This method returns the product of parameters. IncludeMultiplyMethodMultiplyclassClass is also a namespaceUtilitymethods.

  • Testcode. CS: IncludeMainMethod file. It uses methods in the DLL file to calculate the sum and product of runtime parameters.

 

Add. CS

 1 // File: Add.cs 
2 namespace UtilityMethods
3 {
4 public class AddClass
5 {
6 public static long Add(long i, long j)
7 {
8 return (i + j);
9 }
10 }
11 }
Mult. CS

 1 // File: Mult.cs
2 namespace UtilityMethods
3 {
4 public class MultiplyClass
5 {
6 public static long Multiply(long x, long y)
7 {
8 return (x * y);
9 }
10 }
11 }
Testcode. CS

 1 // File: TestCode.cs
2
3 using UtilityMethods;
4
5 class TestCode
6 {
7 static void Main(string[] args)
8 {
9 System.Console.WriteLine("Calling methods from MathLibrary.DLL:");
10
11 if (args.Length != 2)
12 {
13 System.Console.WriteLine("Usage: TestCode <num1> <num2>");
14 return;
15 }
16
17 long num1 = long.Parse(args[0]);
18 long num2 = long.Parse(args[1]);
19
20 long sum = AddClass.Add(num1, num2);
21 long product = MultiplyClass.Multiply(num1, num2);
22
23 System.Console.WriteLine("{0} + {1} = {2}", num1, num2, sum);
24 System.Console.WriteLine("{0} * {1} = {2}", num1, num2, product);
25 }
26 }

Run cmd.

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

Testcode 1234 5678

Output
Calling methods from MathLibrary.DLL:1234 + 5678 = 69121234 * 5678 = 7006652
Compile code

To generate a fileMathlibrary. dll, Use the following command line to compile the fileAdd. CSAnd filesMult. CS:

CSC/Target: Library/out: mathlibrary. DLL add. CS mult. CS

/Target: the Library compiler option notifies the compiler to output the DLL file instead of the EXE file. The/out compiler option followed by the file name is used to specify the DLL file name. Otherwise, the compiler uses the first file (Add. CS) As the DLL file name.

To generate an executable fileTestcode.exe, Use the following command line:

CSC/out: testcode.exe/reference: mathlibrary. dll testcode. CS

/OutThe compiler option notifies the compiler to output the EXE file and specify the output file name (Testcode.exe). This compiler option is optional. /Reference the compiler option to specify the DLL file used by the program.

Http://msdn.microsoft.com/zh-cn/library/3707x96z (vs.80). aspx
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.