C # create DLL dynamic link library and C # Use Dynamic Link Library

Source: Internet
Author: User

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

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.