Home  >  Article  >  Backend Development  >  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#

黄舟
黄舟Original
2017-03-07 11:24:163631browse

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!");
      }
    }
  }
}

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:
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