Introduction
This section describes how to use C # to create and use dynamic link libraries. Dynamic Link Libraries can encapsulate code and protect source code. Use a complete example of the legend.
Use software
Both vs2005 and later versions are supported.
Steps and methods
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 pay attention to the three items in the application selection card.
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!
Then I can call it in the program.
Add in program. CS
The following code:
Using system;
Using system. Collections. Generic;
Using system. text;
// Must be added
Using system. runtime. interopservices;
Using dlltest;
Namespace dllexample
{
Class Program
{
// 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:
Notes
Other applications can call the same method!
From: http://jingyan.baidu.com/article/6525d4b1091539ac7d2e9407.html