Introduction
This section describes how to use C # to create and use dynamic link libraries and how to use dynamic link library pairs.CodeWell-encapsulated and well-protectedSource code. Use a complete example of the legend.
Use software
Both vs2005 and later versions are supported.
Step and method to create a class library.
Compile a simple class library instance, for example, dlltest
Write code in the default name: calss1.cs
The following is a simple example: "You have successfully called a dynamic connection!" is displayed on the console !"
Sing system;
Using system. Collections. Generic;
Using system. text;
Namespace dlltest
{
Public class class1
{
Public void showmessage ()
{
Console. writeline ("You successfully called the dynamic connection! ");
Console. Readline ();
}
}
}
Configuration output, right-click the solution -- properties, and then click ApplicationProgramYou need to pay attention to the three items in card selection.
1. Assembly name 2. default namespace 3. Output type
1. dlltest 2. dlltest 3. Class Library
These are useful for my calls!
Generate DLL, click "generate dlltest" in the generate menu bar, or press shfit + F6
The generated DLL is displayed in the directory:
Call the DLL and create a console application to call the DLL.
Create a new dllexample.
Add a reference to the new project to reference the DLL.
Find the generated DLL in "search range" and click "OK. The following content is displayed in the solution, indicating that you have successfully added it!
now I can call it in the program.
in program. add
the following code to CS:
using system;
using system. collections. generic;
using system. text;
// required
using system. runtime. interopservices;
using dlltest;
namespace dllexample
{< br> class Program
{< br> // dlltest, our Dynamic Link Library
[dllimport ("dlltest. DLL ")]
//
Public static extern void showmessage ();
Static void main (string [] ARGs)
{
// Instantiate
Dlltest. class1 I = new class1 ();
// Call the dynamic link library Method
I. showmessage ();
}
}
}
Running result:
Note: Other applications can call the same method!
Read the full text
Category:C # view comments