Summary of the Method of Generating DLL File from C

  • 2021-07-06 11:40:41
  • OfStack

The process of compiling a.cs file to a.dll using the csc command

Many times, we need to compile a.cs file separately into a.dll file as follows:

Open the command window- > Enter cmd to the console- > cd C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322

Go to this directory in the vs. net installation- > Execute the csc command csc/target: library File. cs- > Generate a. dll file with the corresponding name in this directory (Premise: Put the. cs file in the directory C:\ WINDOWS\ Microsoft. NET\ Framework\ v1.1. 4322)

The csc command comes in many ways, please refer to the following

Translate File. cs to produce File. exe

csc File. cs compiles File. cs to produce File. dll
csc/target: library File. cs compile File. cs and create My. exe
csc/out: My. exe. cs compiles all C # files in the current directory by using the optimized and defined DEBUG symbols. The output is 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. Do not display any logos and warnings
csc/target: library/out: File2.dll/warn: 0/nologo/debug *. cs compiles all C # files in the current directory to Something. xyz (1 DLL)
csc/target: library/out: Something. xyz *. cs compiles File. cs to produce File. dll
csc/target: library File. cs This is one of the commands we use most. In fact, it can be simply written as csc/t: library File. cs, and the other one is written as csc/out: mycodebehind. dll/t: library mycodebehind. cs.
csc/out: mycodebehind. dll/t: library mycodebehind. cs mycodebehind2. cs, which is used to load two cs files into one. dll file

csc is not an internal or external command, nor is it a runnable program solution

For VisualStudio 2005
1: Right-click "My Computer"-"Properties"-"Advanced"-"Environment Variables"-"System Variables"
Add the path to PATH: C:\ WINDOWS\ Microsoft. NET\ Framework\ v2.0. 50727\
2: Execute directly in the corresponding cs folder directory of the dos environment
Path=C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\
3: VisualStudio2005 Command Prompt
Start--"Program--" Microsoft Visual Studio 2005-- > Visual Studio Tools--- > VisualStudio 2005 Command Prompt
cs file copy to C:\ Program Files\ Microsoft Visual Studio 8\ VC\
4:C:\autoexec.bat
Added: C:\ WINDOWS\ Microsoft. NET\ Framework\ v2.0. 50727\

Under vs2008
C: CSC. EXE in\ WINDOWS\ Microsoft. NET\ Framework\ v2.0. 50727\ is version 2.0
Compiled. cs file with using System. Linq; Compilation does not pass, if using System. Linq is deleted without LINQ syntax; Or call C: CSC.EXE in\ WINDOWS\ Microsoft.NET\ Framework\ v3.5

Same as 1SLN, class directly accesses the class library.
Encapsulated to DLL. Import DLL add using namespace workers can access.


1. Dynamic Link Library

What is a dynamic link library? DLL 3 letters for you must be familiar with it, it is Dynamic Link Library abbreviation, dynamic link library (DLL) as a shared function library executable. Dynamic linking provides a way for a process to call functions that are not part of its executable code. The executable code for a function is in an DLL that contains one or more functions that have been compiled, linked, and stored separately from the process that uses them. DLL also helps to share 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. I have also felt that it brings you good mistakes in programming and coding. Today, I want to discuss a topic with you: How to create and call DLL (Dynamic Link Library) in C #. In fact, in a great sense, DLL allows me to organize and write our applications more flexibly. As a software designer, I can achieve high code reuse effect according to it. Let me show you how to create and invoke DLL in C # under 1.

Step 2: Preparations

We need a brief introduction to what we will do next. In this article, we will use C # language to create a dynamic link library named MyDLL. DLL. In this dynamic link library file, we will provide two functions: one is to exchange their values for two parameters, and the other is to find the greatest common divisor of two parameters. Then create an application to use 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; 
      } 
    } 
} 

It should be noted that we can use Visual Studio. NET or other text editors, even Notepad, when making these two files. Although these two files are not in the same file, they belong to the same namespace (namespace), which provides convenience for us to use these two methods in the future. Of course, they can also belong to different namespaces, which is entirely possible, but only when we apply them, we need to refer to two different namespaces, so the author suggests that it is better to write under one namespace.

The next task is to turn these two cs files into the DLL files we need. The method is this: On the operating system where Microsoft. NET Framework is installed, we can find the Microsoft. NET directory under the directory where Windows is located. The compiler for C # is provided under this directory. CSC. EXE runs: csc/target: library/out: MyDLL. DLL MySwap. cs MyMaxCD. cs. When finished, you can find the MyDLL. DLL file we just generated under this directory. target: library compiler option tells the compiler to output DLL file instead of EXE file. The/out compiler option followed by the filename specifies the DLL filename. If/out is not followed by a filename, the compiler uses the first file (MySwap. cs) as the DLL filename. The generated file is the MySwap. DLL file.

OK! Our task of creating the DLL file is complete, and now it's time for us to enjoy the fruits of our labor. I'll show you how to use the DLL file we created. 4. Using DLL, let's simply write a small program to test whether the two methods we just wrote are correct. OK, follow me:

MyClient.cs


using System;  

using MyMethods; // Here we refer to the namespace we just defined. If the two files we just wrote 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); 
     //  Note that at the beginning of the file  using  Directive enables you to use an unqualified class name to reference  DLL  Method  
     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:MyLibrary.DLL MyClient.cs
The/out compiler option tells the compiler to output the EXE file and specifies the output file name (MyClient. exe). The/reference compiler option specifies the DLL file referenced by the program.

5. Implementation

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

6. 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 exchange operations. Many processes can use one DLL at a time, sharing one copy of that DLL in memory. Instead, for each application built with a static link library, Windows must load one copy of the library code in memory.
2. Save disk space. Many applications can share one copy of DLL on disk. Instead, each application generated with a static link library has library code linked as a separate copy to its executable image. 3. It is easier to upgrade to DLL. When functions in DLL change, there is no need to recompile or relink the applications that use them as long as the parameters and return values of the functions have not changed. On the contrary, statically linked object code requires relinking the application 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 originally 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 the DLL function in 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 up the stack, and whether any arguments are passed in the register.
6. Provides a mechanism for extending MFC library classes. You can derive classes from existing MFC classes and place them in the MFC extension DLL for use by MFC applications.
7. Make the creation of international version easy. By putting resources into DLL, it is much easier to create an international version of an 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 independent; It depends on whether there is a separate DLL module


Related articles: