Visual C # system to make DLLs

Source: Internet
Author: User
Tags switches

As software designers and developers have experienced the use of DLL (dynamic Connection library), the production of DLLs makes our applications in maintainability, code reuse and so on have greatly improved. Previously used DLLs are commonly used in Visual C + +, Delphi or VB programming languages, such as the writing and use of the DLL, we have been more accustomed to. As a new generation of program development language--visual C #, in the end is how to write and use DLLs! This article will try to make a simple introduction to this aspect of the problem.
A DLL file written as a Visual C + +, Delphi, or VB programming language, and the resulting DLL file is already a binary file that can be used directly by the computer after the compilation is complete. However, the managed code (managed code) generated with the Visual C # compiler is also a binary file, but is not an original code (machine language code) that can be used directly by the computer. He is essentially an intermediate language (IL) code, which translates into raw code that can be used directly by the computer, and needs to go through the next window service (next Generation Windows Services, abbreviated NGWS) The runtime Just-in-time compiler (JIT) is compiled.
Through the above comparisons, we can see that the DLL files generated with Visual C # are essentially different from the previous DLL files. DLL files that are generated in Visual C # are more represented in programming as a class or class library (class libraries). This article tries to pass a specific procedure example, follow the following steps to specifically introduce:
(1). Create a DLL source code.
(2). Compile this DLL source code to generate the DLL file.
(3). Use this DLL to create a simple client program.
I. Program design development and operating environment:
(1). Microsoft Windows 2000 Professional Edition
(2). Net FrameWork SDK Beta 2
Two. Create a DLL source code (Dll.cs)
Because the DLL created with Visual C #, this DLL is an interface that does not need to be executed, it is not necessary to define the main () function as an entry for application execution in the DLL file. The source code for the Dll.cs is as follows:
Dll.cs:
The namespace DLL file://defines a namespace, which is imported when the DLL is invoked.
{
public class show file://defines a class that is to be inherited in a program.
{
public string Messages ()
file://defines a method that is used to return the string below.
{
Return "Welcome to the DLL file made with Visual C #!" " ;
}
}
}
As you can see from the source program for this DLL, this DLL behaves as a small class library because it encapsulates the namespace of the name DLL in this DLL, and a show class is defined in this namespace, and one method in this class is messages. Although the definition is relatively small, it is quite complete.
Three. Compile this DLL source code, build DLL file
To compile the DLL source code into a DLL file, you need to configure the compiler Csc.exe parameters and switches. We know that the compiler Csc.exe can compile the source code into four different files, namely the control application, the code base, the Windows application, the module program. The compilation commands are as follows:
Csc/target:exe MyProj.cs//Create a MyProj.exe control program
Csc/target:winexe MyProject.cs file://to create a myProj.exe Windows program
Csc/target:library MyProject.cs file://Create a MyProj.dll code base
Csc/target:module MyProject.cs file://Create a MyProj.dll module
For how to configure the compiler Csc.exe other parameters and switches, you can refer to the earlier published article "How to use CSC.exe to compile Visual C # code files," in this article has a more detailed introduction. The MyDLL.DLL file can be obtained by using the following compilation command:
Csc/r:system.dll/t:library/out:mydll.dll Dll.cs
Four Use this DLL to create a simple client program (test.exe)
From the above source code can see that the generated MyDLL.DLL file, although a DLL as an extension, is actually a class library, and we often use the System.dll, System.Windows.Forms.dll and other files similar. Also, creating programs with MyDLL.DLL is similar to creating programs with these class libraries. The first step is to import this namespace--dll. Then go to inherit the encapsulated class--show, and then invoke the defined method in the class--messages. The following is the process of creating the source code (Test.cs) of the client as described above:
Test.cs:
A using Dll; Import this namespace
Using System;
public class GetMessage
{
public static void Main ()
{
Show hi = new Show (); Inherit the show class defined in the namespace
Console.WriteLine (hi. Messages ()); Calling methods in this class
}
}
In compiling the Test.cs into an execution program, you add a reference to the compilation command to refer to the MyDLL.DLL file. The specific compilation commands are as follows:
Csc/r:mydll.dll/r:system.dll Test.cs

Five. Summary:
This article shows that using Visual C # to make a DLL is not a very complex event, but the invocation of a DLL differs from the previous one, the most important of which is the use of Visual C # or other. Net The DLL produced by the program development language differs substantially from the previous DLL. It is not a dynamic connection library in the strict sense, but a class or class library.

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.