Use C # to create and use Dynamic Link Libraries

Source: Internet
Author: User

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

Related Article

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.