C # generate a DLL file

Source: Internet
Author: User
C # generate a DLL file

Use the CSC command to compile the. CS file into. dll

Most of the time, we need to compile the. CS file separately into A. dll file. The operations are as follows:

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

Go to the directory where vs.net is installed and execute the CSC command CSC/Target: library file. CS-> Generate a corresponding name under this directory. DLL file (premise: Put. put the CS file in C: \ WINDOWS \ Microsoft. net \ framework \ v1.1.4322 directory)

CSC commands are used in many ways. For more information, see the following.

Translate file. CS to generate file.exe

CSC file. CS compile file. CS to generate 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 optimizing and defining the debug symbol. Output as file2.exe

CSC/define: Debug/optimize/out: file2.exe *. CS compile all the C # files in the current directory to generate the debug version of file2.dll. No logo or warning is displayed

CSC/Target: Library/out: file2.dll/warn: 0/nologo/debug *. CS compile all the C # files in the current directory as something. XYZ (a DLL)

CSC/Target: Library/out: Something. XYZ *. CS compile file. CS to generate file. dll

CSC/Target: library file. CS is the most widely used command. In fact, it can be simply written as CSC/T: library file. CS. Another method is CSC/out: mycodebehind. dll/T: Library mycodebehind. CS. You can specify the output file name.

CSC/out: mycodebehind. dll/T: Library mycodebehind. CS mycodebehind2.cs. This function is to install two Cs files into A. dll file.

CSC is neither an internal or external command nor a workable program Solution
For visualstudio2005
1: Right-click "my computer" -- "properties" -- "advanced" -- "environment variable" -- "system variable"
Add the path c: \ windows \ Microsoft. NET \ framework \ v2.0.50727 \ to the path \
2: run the command directly in the CS folder directory in the DOS environment
Path = c: \ windows \ Microsoft. NET \ framework \ v2.0.50727 \
3: visualstudio2005 Command Prompt
Start -- program --- Microsoft Visual studio2005 ----> Visual Studio Tools ---> visual studio2005 Command Prompt
Copy the CS file to c: \ Program Files \ Microsoft Visual Studio 8 \ Vc \
4: C: \ autoexec. bat
Add: C: \ WINDOWS \ Microsoft. NET \ framework \ v2.0.50727 \

 

Under vs2008
C: \ windows \ microsoft. Net \ framework \ v2.0.50727 \ The CSC. EXE version is 2.0.
Compiled. if the CS file contains using system. however, if you do not need the LINQ syntax to delete the using system. LINQ;. Otherwise, c: \ windows \ Microsoft. CSC in net \ framework \ v3.5. EXE

The class in the same SLN directly accesses the class library.
Package to DLL. Only using namespace workers can access the imported DLL.

I. Dynamic Link Library

What is a dynamic link library? You must be familiar with the three DLL letters, which are short for dynamic link library. dynamic link library (DLL) is an executable file used as a shared function library. Dynamic links provide a way for a process to call a function that does not belong to its executable code. The executable code of a function is located in a DLL, which contains one or more functions that have been compiled, linked, and stored separately from the processes that use them. DLL also helps to share data and resources. Multiple applications can simultaneously access the content of a single DLL copy in the memory.

Like most programmers, you must have used DLL. I have also felt that it has brought you a good mistake in programming and coding. Today I want to discuss a topic: how to create and Call DLL (Dynamic Link Library) in C ), in fact, in a big sense, dll allows me to organize and write our applications more flexibly. As a software designer, we can achieve high code reuse based on it. The following describes how to create and call a DLL in C.

2. Preparations

We need to make a brief introduction to what we will do next. In this article, we will use the C # language to create a file named mydll. DLL dynamic link library. In this dynamic link library file, we provide two functions: one is to exchange their values for the two parameters, and the other is to find the maximum public approx of the two parameters. Then create an application to use this DLL. Run and output the result.

3. Create a DLL

Let's create the following three C # code files:

1. myswap. CS
View plaincopy to clipboardprint?
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;
}
}
}

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

View plaincopy to clipboardprint?
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;
}
}
}

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 when creating these two files, you can use Visual Studio. NET or other text editors, even notepad. Although these two files are not in the same file, they belong to the same namespace, which is convenient for us to use these two methods in the future. Of course, they can also belong to different namespaces. This is completely acceptable, but we only need to reference two different namespaces when we apply them, therefore, it is recommended that you write it below a namespace.

The next task is to convert the two Cs files into the DLL files we need. The method is as follows: on the operating system where Microsoft. NET Framework is installed, we can find the Microsoft. NET directory in the directory where windows is located. The C # compiler is provided under this directory, CSC. EXE run: CSC/Target: Library/out: mydll. DLL myswap. CS mymaxcd. CS, you can find the generated mydll in the directory below. DLL file/Target: Library compiler option notifies the compiler to output the DLL file instead
EXE file. The/out compiler option followed by the file name is used to specify the DLL file name. If the/out file is not followed by the file name compiler, use the first file (myswap. CS) as the DLL file name. The generated file is the myswap. dll file.

OK! The task of creating a dynamic link library file is complete. Now we are enjoying the fruits of our work. Next I will introduce how to use the dynamic link library file we created. 4. Using DLL, we can simply write a small program to test whether the two methods we just wrote are correct. Okay, come with me:

Myclient. CS

View plaincopy to clipboardprint?
Using system;

Using mymethods; // here we reference the namespace 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 the using command at the beginning of the file enables you to reference the DLL method using an undefined class name during compilation.
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 );

}
}

Using system;
Using mymethods; // here we reference the namespace 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 the using command at the beginning of the file enables you to reference the DLL method using an undefined class name during compilation.
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 an executable file myclient.exe, use the following command line:
CSC/out: myclient.exe/reference: mylibrary. dll myclient. CS
The/out compiler option notifies the compiler to output the EXE file and specify the output file name (myclient.exe ). The/reference compiler option specifies the DLL file referenced by the program.

5. Execution

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

Vi. Output

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

The maxcd of 456 and 123 is 3

VII. Summary

Dynamic Links have the following advantages:

1. save memory and reduce swap operations. Many processes can use a DLL at the same time and share a copy of the DLL in the memory. On the contrary, for each application generated using a static Link Library, windows must load a copy of the library code in the memory.

2. save disk space. Many applications can share a copy of the DLL on the disk. Instead, each application generated with a static Link Library has the library code linked to its executable image as a separate copy. 3. It is easier to upgrade to DLL. When a function in DLL is changed, as long as the parameters and return values of the function are not changed, you do not need to re-compile or re-link their applications. On the contrary, static linked Object Code requires that the application be relinked when the function is changed.

4. provide after-sales support. For example, you can modify the display driver DLL to support displays that are unavailable when the application was originally delivered.

5. multi-language programs are supported. As long as the program complies with the function call conventions, the program written in different programming languages can call the same DLL function. Programs and DLL functions must be compatible in the following aspects: the order in which the function expects its parameters to be pushed to the stack, whether the function or application is responsible for clearing the stack, and whether any parameters are passed in the register.

6. provides a mechanism for extending the MFC Library Class. You can use the existing MFC class derived classes and place them in the MFC extension DLL for the MFC application.

7. Make it easy to create international versions. By putting resources in the DLL, it is much easier to create an international version of the application. Strings of each language version used for applications can be placed in a separate DLL resource file, and appropriate resources can be loaded for different language versions.

One potential disadvantage of using DLL is that the application is not independent; it depends on whether there is a separate DLL module

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.