How To Create And Use A Dynamic-link library (Dll) In C# Visual Studio 2010 Step By Step

How To Create And Use A Dll In C# Visual Studio 2010 Step By Step

How To Create And Use A Dynamic-link library (Dll) In C# Visual Studio 2010
How To Create And Use A Dynamic-link library (Dll) In C# Visual Studio 2010
How To Create And Use A Dll In C# Visual Studio 2010

Creating C# Class Library (DLL) Using Visual Studio .NET

Creating C# Form:

Step 1 – Open Visual Studio then select “File” -> “New” -> “Project”
Step 2 – Select “Visual C#” -> “Windows Forms Application”.

How To Create And Use A Dynamic-link library (Dll) In C#
How To Create And Use A Dynamic-link library (Dll) In C#


Then Add A Button For Creating Dll Class Object And Calling DLL Method.

Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DynamicLinkLibraryDll;

namespace CreateAndUseDynamicLinkLibraryDLL
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //Call DLL Function When You Click A Button
            Class1 fromDLL = new Class1();
            fromDLL.dllFunc();

        }
    }
}

Add reference to dll in c# project Visual Studio 2010

  1. Right Click References Under CreateAndUseDynamicLinkLibraryDLL (Windows Form) Project .
  2. Choose “Add Reference”.
  3. Click Browse Button.
  4. Choose Dll From The Dll Project Debug Folder.
    My Folder Is – C:\Users\Authentic\Documents\visual studio 2010\Projects\CreateAndUseDynamicLinkLibraryDLL\DynamicLinkLibraryDll\bin\Debug
Add reference to dll in c# project Visual Studio 2010
Add reference to dll in c# project Visual Studio 2010

After clicking open button on the Dialog box, The Reference to the External DLL gets added to the References, As Show Below:

How To Add C# External Dll Reference
How To Add C# External Dll Reference

Creating DLL File

Step 1 – Open Visual Studio then select “File” -> “New” -> “Project”
Step 2 – Select “Visual C#” -> “Class library”.

How To Create And Use A Dynamic-link library (Dll)
How To Create And Use A Dynamic-link library (Dll)

Step 3 – Add The Project To Existing Solution

Add The Project To Existing Solution
Add The Project To Existing Solution
How To Create A Dynamic-link library In Visual Studio 2010 - Add Project To Existing Solution
How To Create A Dynamic-link library In Visual Studio 2010 – Add Project To Existing Solution

DynamicLinkLibraryDll class – Class1.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DynamicLinkLibraryDll
{
    public class Class1
    {
        public void dllFunc() {

            MessageBox.Show("Message Coming From Dll...................");
        
        }
    }
}

Step 3 – Compile Project to create Dll File (Build the solution)

READ MORE  How To Print DataGridView In C# Windows Application

Navigate to the class library folder
Go into the debug folder of your project. If the build is successful then you will see a .dll file. In this case DynamicLinkLibraryDll.dll

C:\Users\Authentic\Documents\visual studio 2010\Projects\CreateAndUseDynamicLinkLibraryDLL\DynamicLinkLibraryDll\bin\Debug

Form1.Designer.cs

namespace CreateAndUseDynamicLinkLibraryDLL
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.button1 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // button1
            // 
            this.button1.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.button1.Location = new System.Drawing.Point(64, 110);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(369, 73);
            this.button1.TabIndex = 0;
            this.button1.Text = "CALL DLL FUNCTION";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(505, 308);
            this.Controls.Add(this.button1);
            this.Name = "Form1";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Form1";
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.Button button1;
    }
}


Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace CreateAndUseDynamicLinkLibraryDLL
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *