Home Backend Development C#.Net Tutorial Detailed graphic explanation of the implementation method of loading dll and calling its functions in C#

Detailed graphic explanation of the implementation method of loading dll and calling its functions in C#

Mar 07, 2017 am 11:24 AM

The following editor will bring you an implementation method of loading dll and calling its functions in C#. The editor thinks it is quite good, so I will share it with you now and give it as a reference for everyone. Let’s follow the editor and take a look.

In C# programming, calling functions in packaged dlls is frequently used. So, how to load the dll in the program and call the functions in it? Furthermore, how to debug the functions in the dll encapsulated by yourself in the main program?

Load dll - add reference

Adding reference means to load the corresponding dll according to the configured path when the program is generated. The reference steps are shown in the figure below:

Solution->Reference-> Add reference-> Browse-> Select the path where the dll is located ->Confirm

Import namespace, instantiate object, call function

Only by importing the namespace of the dll, can Use the classes under this space. Therefore, the first step after referencing the dll is to import the namespace; the second step is to instantiate the class object; and the last step is to use the class object to call its member functions. The code shown below calls the initialization function in the packaged DMC3000.dll.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using nsLTDMC;

//(1)引入命名空间
using nsDMC3000;


namespace DalsaConfig
{
  public partial class Form1 : Form
  {
    //(2)声明类对象
    DMC3000 m_f1DMC3000;
    public Form1()
    {
      //(3)实例化类对象
      m_f1DMC3000 = new DMC3000();
      InitializeComponent();

      //(4)调用dll中的函数
      bool bIniResult = m_f1DMC3000.Init();
      if (true == bIniResult)
      {
        MessageBox.Show("Init OK");
      }
      else
      {
        MessageBox.Show("Init Failed!");
      }
    }
  }
}
Copy after login

Steps to debug dll functions

The first two basic steps are how to call functions in dll, and This step is to debug your own encapsulated dll.

Add existing project

Add existing project means loading the packaged dll project into the main program, so that you can step into the dll during debugging function called.

Set dependencies

The dependency relationship means that the main program is the startup project and the dll program is the dependent project.

The above is the detailed graphic and text explanation of the implementation method of loading dll and calling its functions in C#. For more related content, please pay attention to PHP Chinese website (www.php.cn)!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Active Directory with C# Active Directory with C# Sep 03, 2024 pm 03:33 PM

Active Directory with C#

Access Modifiers in C# Access Modifiers in C# Sep 03, 2024 pm 03:24 PM

Access Modifiers in C#

Random Number Generator in C# Random Number Generator in C# Sep 03, 2024 pm 03:34 PM

Random Number Generator in C#

C# Data Grid View C# Data Grid View Sep 03, 2024 pm 03:32 PM

C# Data Grid View

C# StringReader C# StringReader Sep 03, 2024 pm 03:23 PM

C# StringReader

Patterns in C# Patterns in C# Sep 03, 2024 pm 03:33 PM

Patterns in C#

C# StringWriter C# StringWriter Sep 03, 2024 pm 03:23 PM

C# StringWriter

BinaryWriter in C# BinaryWriter in C# Sep 03, 2024 pm 03:22 PM

BinaryWriter in C#

See all articles