Many times, we need to compile the. cs file individually into a. dll file, which requires the CSC command to compile the. cs file into a. dll dynamic link library file. The specific steps are as follows:
Open the Command Window-> enter CMD to the console->CD c:windowsmicrosoft.netframeworkv1.1.4322
Go to the directory where the vs.net is installed-> execute the CSC command csc/target:library file.cs-> A. dll file that produces a corresponding name in the directory (if: put the. cs file into C: windowsmicrosoft.netframeworkv1.1.4322 directory)
csc command A lot of ways, please refer to the following
Compile 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 File.cs compiles all C # files in the current directory by using the optimize and define the DEBUG symbol. 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 (one DLL)
CSC/TARGET:LIBRARY/OUT:SOMETHING.XYZ *.cs compiles File.cs to produce File.dll
Csc/target:library File.cs This is the most we use a command, in fact, can be written as a simple csc/t:library File.cs, the other one is csc/out:mycodebehind.dll/t: Library Mycodebehind.cs, this file name that you can specify the output yourself.
Csc/out:mycodebehind.dll/t:library Mycodebehind.cs Mycodebehind2.cs, the role of this is to put two CS files into a. dll file
Overview of Dynamic Link libraries:
What is a dynamic link library? DLL three letters are abbreviated forms of the dynamic link library, which is the executable file that is used as a shared function library. Dynamic linking provides a way for a process to invoke a function that is not part of its executable code. The executable code for a function is in a DLL that contains one or more functions that have been compiled, linked, and stored separately from processes that use them. DLLs also help you to share data and resources. Multiple applications can access the contents of a single DLL copy in memory at the same time.
Like most programmers, you must have used DLLs very well. I have also felt that it brings you the benefits of programming and coding! Here's a topic today: How to create and invoke DLLs in C # (dynamic-link libraries), but in a big sense, DLLs make it easier for me to organize our applications, as software designers, to achieve a high code reuse effect. Let me explain how to create and invoke DLLs in C #.
Ii. preparatory work
We need to make a simple introduction to what we're going to do next, in this article we will use the C # language to create a dynamic link library called MyDLL.DLL, in this dynamic-link library file We will provide two features one is to exchange their values for two parameters, the other is to find two parameters of the GCD. Then create an application to use this DLL. Run and output the results.
Third, create a 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 other text editors when making these two files, even Notepad. Although these two files are not in the same file, they belong to the same namespace (namespace) which is convenient for us to use these two methods later. Of course they can also belong to different namespaces, this is perfectly OK, but only when we apply them to the need to refer to two different namespaces, so the author suggested that it is better to write under a namespace.
The next task is to turn the two CS files into the DLL files we need. The approach is this: on the operating system where the Microsoft.NET framework is installed, we can find the Microsoft.NET directory in the directory where Windows resides. The C # compiler, CSC, is provided below this directory. EXE run: Csc/target:library/out:mydll.dll MySwap.cs MyMaxCD.cs, after the completion of this directory can be found in the MyDLL.DLL file we just generated/target:library compiler option notifies the compiler to output DLL files instead of EXE files. The/out compiler option followed by the filename is used to specify the DLL file name. If you do not use the first file (MySwap.cs) as the DLL file name with the filename compiler after/out. The generated file is a MySwap.DLL file.
ok! we have completed the task of creating a dynamic link library file, now is the time to enjoy the fruits of our labor, and I'll explain how to use the Dynamic link library file we created.
Iv. use of DLLs
Let's simply write a small program to test the correctness of the two methods we just wrote:
The MyClient.cs code is as follows:
Using System;
Using Mymethods; Here we reference the namespace just defined, if just the two files we 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 the using directive at the beginning of the file allows you to use the unqualified class name at compile time to refer to the DLL method
Console.WriteLine ("The result of the ' swap is NUM1 = {0} and Num2 ={1}", Nu M1, num2);
Long Maxcd = Maxcdclass.maxcd (num1,num2);
Console.WriteLine ("The Maxcd of {0} and {1} is {2}", Num1, num2, Maxcd);
}
To build the executable file 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 file name (MyClient.exe). The/reference compiler option specifies the DLL file that the program references.
V. Implementation
To run the program, enter the name of the EXE file followed by two digits, for example: MyClient 123 456
Six, output
The result of ' NUM1 = 456 and num2 = 123 the
Maxcd of 456 and 123 is 3
Vii. Summary of:
Dynamic links have the following benefits:
1, saving memory, and reducing exchange operations. Many processes can use a single DLL to share a copy of the DLL in memory. Instead, for each application built with a static link library, Windows must load a copy of the library code in memory.
2, saving disk space. Many applications can share a copy of a DLL on disk. Instead, each application built with a static link library has a library code that is linked as a separate copy to its executable image.
3, it is easier to upgrade to DLLs. DLL, you do not need to recompile or relink the application that uses them as long as the function's arguments and return values do not change. In contrast, statically linked object code requires that the application be relink when the function changes.
4, provide after-sale support. For example, the display driver DLL can be modified to support a monitor that was not available when the application was originally delivered.
5, multilingual programs are supported. Programs written in different programming languages can call the same DLL function as long as the program follows the calling convention of the function. Programs and DLL functions must be compatible in the order in which the function expects their arguments to be pushed onto the stack, whether the function or application is responsible for cleaning up the stack, and if any parameters are passed in the register.
6, provides a mechanism to extend the MFC library classes. You can derive classes from existing MFC classes and place them in an MFC extension DLL for use by MFC applications.
7, make the international version of the creation easy to complete. It is much easier to create an international version of an application by putting resources into a DLL. Strings for each language version of the application can be placed in a separate DLL resource file and the appropriate resources are loaded in different language versions. A potential disadvantage of the
using a DLL is that the application is not independent, depending on whether there is a separate DLL module.