asp. net source program compiled to dll file and called by the implementation process

  • 2020-12-16 05:55:33
  • OfStack

Most of the time, we need to compile.cs files separately into.dll files, so we need to use the csc command to compile.cs files into.dll dynamic link library files. The specific steps are as follows:

Open the command window - > Enter cmd into the console - > cd C:WINDOWSMicrosoft.NETFrameworkv1.1.4322

Go to the directory where vs. net is installed - > Execute the csc command csc /target:library File.cs- > In that directory to generate a corresponding name. dll file (the premise: the. cs files in C: WINDOWSMicrosoft. NETFrameworkv1. 1.4322 directory)

There are many ways to use the csc command, see below

Compile ES34en.cs to produce ES36en.exe
csc ES40en.cs compiles File.cs to produce ES44en.dll
csc /target:library ES50en.cs compile File.cs and create ES54en.exe
csc /out: My.exe File.cs compiles all C# files in the current directory by using optimization and defining DEBUG symbols. The output for File2 exe
csc /define:DEBUG /optimize /out: File2.exe *.cs compiles all C# files in the current directory to produce a debug version of File2.dll. Does not display any logos or warnings
csc /target:library /out:File2.dll /warn:0 /nologo /debug *.cs Compiles all C# files in the current directory to ES91en.xyz (1 DLL)
csc /target:library /out: Something.xyz *.cs compile File.cs to produce ES104en.dll
csc /target:library File.cs this is the one command we use most. In fact, it can be written as csc /t:library File.cs and the other one is csc /out:mycodebehind.dll /t mycodebehind.
csc /out:mycodebehind/ES139en :library mycodebehind 2.cs. This is to load two cs files into one

1. Overview of dynamic Link Library:

What is a dynamic link library? DLL3 is the abbreviation for Dynamic Link Library, and the dynamic link library (DLL) is an executable that acts as a shared function library. Dynamic linking provides a way for a process to call functions that are not part of its executable code. The executable code for the function is located in an DLL containing one or more functions that have been compiled, linked, and stored separately from the process that uses them. DLL also facilitates the sharing of data and resources. Multiple applications can simultaneously access the contents of a single copy of DLL in memory.

Like most programmers, you must have used DLL before. Have you ever felt its benefits in programming and coding? Today I discuss a topic: how to create and call DLL(dynamic linking library) in C#. In a big sense, DLL gives me the flexibility to organize and write our applications so that, as a software designer, I can use it to achieve high code reuse. Let me show you how to create and call DLL in C# 1.

2. Preparation

We need to do we need to do a simple introduction, in this article we will use the C # language to create a named MyDLL. DLL dynamic link library, in the dynamic link library files, we will provide two functions is one of two parameters to exchange their values, the other one function is to find the greatest common divisor of two parameters. Then create an application that uses this DLL. Run and output the results.

3. Create DLL

Let's create the following three C# code files:
1, MySwap. cs


using System; 
namespace MyMethods 
{ 
  public class SwapClass 
  { 
   public static bool Swap(ref long i,ref long j) 
   { 
    i = i+j; 
    j = i-j; 
    i = i-j; 
    return true; 
   } 
  } 
} 

2, MyMaxCD cs


using System; 
namespace MyMethods 
{ 
  public class MaxCDClass 
  { 
   public static long MaxCD(long i, long j) 
   { 
    long a,b,temp; 
    if(i>j) 
    { 
     a = i; 
     b = j; 
    } 
    else 
    { 
     b = i; 
     a = j; 
    } 
    temp = a % b; 
    while(temp!=0) 
    { 
     a = b; 
     b = temp; 
     temp = a % b; 
    } 
    return b; 
   } 
  } 
}

Note that we can use Visual Studio.NET or some other text editor, even notepad, for these two files. These two files are not in the same file, but they belong to the same namespace (namespace), which makes it easier for us to use these two methods in the future. Of course, they can belong to different namespaces, which is perfectly fine, but we only need to refer to two different namespaces when we apply them, so the authors suggest writing under one namespace instead.

The next task is to turn these two cs files into the DLL files we need. Here's how: On an operating system with ES193en. NET Framework installed, you can find the Microsoft. NET directory. The compiler for C# is provided under this directory, and CSC. EXE runs: csc /target:library /out:MyDLL.DLL MySwap.cs MyMaxCD.cs. When finished, you can find the MyDLL.DLL file /target:library compiler option to tell the compiler to output the DLL file instead of the EXE file. The /out compiler option followed by the filename is used to specify the DLL filename. If /out is not followed by the filename compiler uses the first file (MySwap.cs) as the DLL filename. The generated file is the ES224en.DLL file.

OK! Now that we've finished creating the dynamic link library file, it's time to enjoy the fruits of our labor, and I'll show you how to use the dynamic link library file we've created.

4. Use DLL

Let's simply write a small program to test the correctness of the two methods we just wrote:

MyClient.cs code is as follows:


using System; 
using MyMethods; // Here we refer to the namespace we just defined, if we just wrote the two files in two different namespaces  
class MyClient 
{ 
  public static void Main(string[] args) 
  { 
   if (args.Length != 2) 
   { 
    Console.WriteLine("Usage: MyClient <num1> <num2>"); 
    return; 
   } 
   long num1 = long.Parse(args[0]); 
   long num2 = long.Parse(args[1]); 
   SwapClass.Swap(ref num1,ref num2); 
   //  Notice at the beginning of the file  using  The directive enables you to reference classes at compile time with unqualified class names  DLL  methods  
   Console.WriteLine("The result of swap is num1 = {0} and num2 ={1}",num1, num2); 
   long maxcd = MaxCDClass.MaxCD(num1,num2); 
   Console.WriteLine("The MaxCD of {0} and {1} is {2}",num1, num2, maxcd); 
  } 
} 

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


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

The /out compiler option notifies the compiler to output the EXE file and specifies the output filename (MyClient.exe). The /reference compiler option specifies the DLL file referenced by the program.

5. Perform

To run the program, enter the name of the EXE file followed by two numbers, such as MyClient 123456

6. The output


The result of swap is num1 = 456 and num2 = 123 
The MaxCD of 456 and 123 is 3 

7. Summary:

Dynamic linking has the following advantages:

1. Save memory and reduce swap operations. Many processes can simultaneously use 1 DLL and share 1 copy of that DLL in memory. Instead, Windows must load 1 copy of the library code in memory for each application generated with a statically linked library.
2. Save disk space. Many applications can share 1 copy of DLL on disk. Instead, each application generated with a statically linked library has library code linked to its executable image as a separate copy.
3. Upgrading to DLL is easier. When functions in DLL change, there is no need to recompile or re-link the applications that use them, as long as the arguments and return values of the functions do not change. In contrast, statically linked object code requires the application to be re-linked when the function changes.
4. Provide after-sales support. For example, the display driver DLL can be modified to support displays that were not available when the application was first delivered.
5. Support multilingual programs. Programs written in different programming languages can call the same DLL function as long as the program follows the calling convention of the function. The program must be compatible with DLL functions in terms of the order in which the function expects its arguments to be pushed onto the stack, whether the function or the application is responsible for cleaning the stack, and whether any arguments are passed in registers.
6, provides the mechanism to extend the MFC library class. You can derive classes from existing MFC classes and place them in the MFC extension DLL for use by MFC applications.
7. Make it easy to create the international version. By putting resources in DLL, it is much easier to create an international version of your application. You can put strings for each language version of your application into a separate DLL resource file and have different language versions load the appropriate resources.
One potential disadvantage of using DLL is that the application is not standalone; It depends on the existence of a separate DLL module.


Related articles: