C # create and call a DLL

Source: Internet
Author: User

Transfer from-clear moon
I. Preface
C # The language is simple but powerful.Programming LanguageUsed to compile enterprise applicationsProgram.

C # Language evolved from C and C ++, and many c ++ functions are used in statements, expressions, and operators.

C # language has made great improvements and innovations in terms of type security, version conversion, events, and garbage collection.

C # provides access to common API styles (such as. NET Framework, COM, automation, and C style APIs.

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. A Dynamic Link provides a way for a process to be called but not 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

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 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, you can find the Microsoft. NET directory in the directory where windows is located. The C # compiler, CSC. EXE, is provided under this directory.
Run: CSC/Target: Library/out: mydll. dll myswap. CS mymaxcd. CS
After that, you can find the mydll. dll file we just generated in the directory.
/Target: the Library compiler option notifies the compiler to output the DLL file instead of the 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 myswap. dll.
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. Use DLL

Let's simply write a small program to test whether the two methods we just wrote are correct. Okay, come with me:
Myclient. CS
Using system;

Using mymethods;

// Here we reference the namespace just defined. If the two files are written 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:

Saves memory and reduces 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.
Saves 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.
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.
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.
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.
Provides a mechanism to expand 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.
This makes 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.

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/21aspnet/archive/2007/03/24/1539937.aspx

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.