C #, how to write and call your own DLL

This article is reproduced connection:  https://blog.csdn.net/iloli/article/details/7726967

First, what is the DLL and the benefits of using it

1, DLL namely: Dynamic Link Library
  DLL is an abbreviated form of Dynamic Link Library, a DLL is library that contains code and data used by multiple programs at the same time, not an executable DLL file. Dynamic Link provides a way to make the process does not belong to its function can be called executable code. Function executable code in a DLL, the DLL comprising one or more has been compiled, and linked with the process using them separately stored function. DLL also facilitate the sharing of data and resources. Multiple applications can simultaneously access the contents of memory in a single copy of the DLL. DLL is a library of code and data that can be used comprises a plurality of programs simultaneously. (From Baidu)
2, the benefits of the DLL are:
  1) When the program is large and growing, DLL avoid excessive EXE file. After divided into DLL, do not put all functions are concentrated in the EXE, into memory only when needed.
  2) DLL modular production process. DLL is the product, EXE is a container. Therefore, updates to the DLL module only need to replace this DLL file, do not update the entire program.
  3) compile EXE, DLL compiled without content, speed up the compilation speed.
  4) DLL reusable. When a DLL mode design, and can be referenced by other projects or programs.
  5) DLL can play the role of secrecy. That DLL source code can be hidden function, the caller can not see can only use its internal code.

"Dynamic Link" these words indicates how DLLs work. For conventional libraries, the linker from which to copy all library functions it needs, and transfers the exact address of the function to program calls these functions. For DLLs, the function is stored in a separate dynamic link library file. When you create a Windows program, the process is not linked to DLLs files linked to the program. Until the program is run and call a function in DLLs, the program only requires the address of the function. At this time, Windows was looking for the called function in DLLs, the address and transmits it to the calling program. With this method, DLLs reached the limit code reuse.
 

Second, how to create and write the DLL in VS2010
is very simple, is to add a project in Solution program, and then select the "library." Note, be sure to add an item on the "solution" to do so, the project can only add class files. But add a class library project, the program will automatically compile time generated as DLL files.
As for writing this DLL and writing a class file code is the same. You can write in multiple CS class files, you can also put together the whole CS class file written. Of course, the recommended approach is based on the functional classification would libraries are written in different CS files. (Although in different files, but the files belong to a class project will be compiled into a DLL library file)

 

Third, how to use the DLL in VS2010 I have written the
method is very simple, in your application solutions manager, find the "Reference" and then right click to add, in the pop-up dialog box, select "Browse" and select you have just created and compiled DLL files. Then use the file using the program to introduce your namespace DLL. Note: If the DLL project in your current program solutions, then you can add directly in the "Reference", select "Project" instead of "browsing." So that you can refer directly to this solution written DLL library.

 

Fourth, you can still see the comment by IntelliSense code (contains only those comments \\ \\\ and \ ** \ notes in the new program code in VS2010 If you let the DLL files added to the new program is not incorporated by reference have witnessed)

The method is: After you create a DLL class library, right-click this property DLL class library project, and then click "Generate" tab in the "output" area check the XML document file. Each time there will be more to generate an XML file of the same name when you build the DLL project, open the file you can see which are all among the footnotes DLL (only contains comments \\\'s). Can also be seen from here, IntelliSense to see the code comments is actually read from the corresponding XML content is then displayed to the programmer. As long as XML and DLL in the same folder, VS when you import the DLL will automatically have the same name as the XML import in, users do not have to manually copy.

 

V. Other

When using a DLL Another way is to use [DLLImport ( "yourdll.dll")]. This is the use of com components, in VS Microsoft does not recommend using this way DLL.
 

Released six original articles · won praise 189 · views 280 000 +

Guess you like

Origin blog.csdn.net/newbie_xymt/article/details/103713993