How to create a dynamic link library (DLL) and use it in .Net and Python
What is a DLL file?
A library is a collection of non-volatile resources that are used for software development. In this tutorial, we are going to create our own dynamic link library(DLL) using c#. A DLL is a library used on Microsoft platforms that contains code and data that can be used by more than one program at the same time. If you open the program information of most applications installed on Microsoft Windows you will notice DLL files. These DLL files store different functionalities for the application. Some of the advantages of DLL files are:
Code reuse
Efficient memory usage
Reduced disk space
Fast loading of applications
Making a DLL file
In this tutorial, we are going to make a simple calculator library that does addition, subtraction, multiplication, and division using C# in Visual Studio.
Create the project (library) in Visual Studio as follows
Create new project
Select C# Class Library (.Net Standard)
Project name -> MyCalculator
The code for your library
Add the following code:
using System;
namespace MyCalculator
{
public class Calculator
{
//Our addition function
public double add(double num_one, double num_two)
{
return num_one + num_two;
}
//Our subtraction function
public double subtract(double num_one, double num_two)
{
return num_one - num_two;
}
//Our multiplication function
public double multiply(double num_one, double num_two)
{
return num_one * num_two;
}
//Our division function
public double divide(double num_one, double num_two)
{
return num_one / num_two;
}
}
}
Building your library project
Click on: Build -> Build Solution
Locating your Calculator Library
Your MyCalculator.dll
is located in:
Your project folder -> MyCalculator -> MyCalculator -> bin -> debug
Using the DLL file in .Net
We are going to use our Calculator library MyCalculator.dll
in a simple C# application.
Create a c# console application in Visual Studio as follows:
File -> New -> Project
Select: C# Console App
Project name: MyProject
Importing our calculator library
Copy
MyCalculator.dll
from: Your library project folder -> MyCalculator -> MyCalculator -> bin -> debugPaste
MyCalculator.dll
to Your project folder -> MyProject -> MyProject -> bin -> debugAdd reference to your dll file: In solution explorer: Under MyProject -> Right click MyProject
-> add -> Project referenceBrowse: Go to (Your project folder -> MyCalculator -> MyCalculator -> bin -> debug ->
MyCalculator.dll
Click add
The code for your project (MyProject)
Edit your code as follows:
using System;
using MyCalculator; // importing our calculator library
namespace MyProject
{
class Program
{
static void Main(string[] args)
{
Calculator calc = new Calculator(); //Our Acalculator class object
Console.WriteLine("Addition: "+calc.add(3, 2));
Console.WriteLine("Subtraction: " + calc.subtract(3, 2));
Console.WriteLine("Multiplication: " + calc.multiply(3, 2));
Console.WriteLine("Division: " + calc.divide(3, 2));
}
}
}
Using the DLL file in Python
Install pythonnet
pip install pythonnet
Python code
import clr #import clr from pythonnet
#load our dll file(mine is in my C:\\ folder)
clr.AddReference("C:\\MyCalculator.dll")
#import our calculator class from Our C# namespace MyCalculator
from MyCalculator import Calculator
calc = Calculator() #create our Calculator object
#calling our methoths and printing
print("Addition: "+str(calc.add(3, 2)))
print("Subtraction: "+str(calc.subtract(3, 2)))
print("Multiplication: "+str(calc.multiply(3, 2)))
print("Division: "+str(calc.divide(3, 2)))