DEV Community

Cover image for How to create a dynamic link library (DLL) and use it in .Net and Python
Tawanda Nyahuye
Tawanda Nyahuye

Posted on

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:

  1. Code reuse
  2. Efficient memory usage
  3. Reduced disk space
  4. 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
  1. Create new project
  2. Select C# Class Library (.Net Standard)
  3. 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;         
        }
    }
}
Enter fullscreen mode Exit fullscreen mode
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:
  1. File -> New -> Project
  2. Select: C# Console App
  3. Project name: MyProject
Importing our calculator library
  1. Copy MyCalculator.dll from: Your library project folder -> MyCalculator -> MyCalculator -> bin -> debug
  2. Paste MyCalculator.dll to Your project folder -> MyProject -> MyProject -> bin -> debug
  3. Add reference to your dll file: In solution explorer: Under MyProject -> Right click MyProject -> add -> Project reference
  4. Browse: Go to (Your project folder -> MyCalculator -> MyCalculator -> bin -> debug -> MyCalculator.dll
  5. 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));
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Using the DLL file in Python

Install pythonnet

pip install pythonnet
Enter fullscreen mode Exit fullscreen mode

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)))
Enter fullscreen mode Exit fullscreen mode

Top comments (5)

Collapse
 
shaijut profile image
Shaiju T

Cool 😄 C# inside python, I never knew this was possible. What is the use case of pythonnet ?

Collapse
 
frikishaan profile image
Ishaan Sheikh • Edited

Nicely Explained!👏
Does the vice-versa is possible, for example, we create a class in python, make a dll and then import in C# code?

Collapse
 
ashwinsharmap profile image
Ashwin Sharma P

Nice one Tawanda 🙂👏👏

Collapse
 
barbosa profile image
Wilson

is it possible to import this dll in a VBScript - classic asp file? Any tip about?

Collapse
 
zedavid profile image
José David Lopes • Edited

I followed this recipe, but I persistently get 'ModuleNotFoundError: No module named 'MyCalculator'. Has anyone had a similar problem?