Introduction
Here we talk about Dynamic Link Libraries (DLLs). Some people also called them C# class libraries. DLLs play a very effective role in Windows programming. In a DLL file there is very much information present, like the library functions, methods and many other information that are used in a Windows program. When we make a simple program and build this program then automatically in your bin folder the DLL of that class file is created. There are two types of DLLs, one is Static DLL and another one is the Dynamic DLL. The Static DLL is in existence until and unless our program is in an active state but a Dynamic DLL is only in existence when it is required. Usually a Dynamic Link Library is used for the resources for the program. We can use a single DLL file in more than one program.
Now here we discuss how to create a DLL file in Visual Studio 2015 Preview. It is a very easy process. This article first shows how to create a DLL and then shows how to call the DLL file in our other program. So let's go with the given procedure.
Creating a DLL (Class Library file) in Visual Studio 2015 preview
Step 1
Open your Visual Studio 2015 preview, go to the file menu, go to the new, go with the project, from the template field select C#, select Class Library then name it as you want. Save it at an appropriate place. So from the browse button you can select the appropriate location for saving the DLL.
Step 2
After creating this Class Library (DLL) in the Solution Explorer you will see the class. In this class there is no method and no property at the initial level. This class is totally empty. We will now add to this class.
Step 3
As I said, there is one class that was generated named Class1. When we double-click on this class then we see the namespace rizDLL. We use this namespace for the further implementation, in other words when we create our Windows based program we use this namespace in our program. When you double-click on the class file you will see something like this.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace rizDLL
- {
-
-
-
- public class Class1
- {
- public Class1()
- {
-
-
-
- }
- }
- }
Step 4
After doing this we will check whether everything in our project is okay. So for checking this we simply go to the tab menu then select build and click on it.
Step5
When you click on the build solution then in the output window you will see a message that the build is successful.
Now go to the location where you saved your rizDLL program. Open it, go to the bin folder, go to the debug folder in the debug folder you will see the DLL file named as you chose when creating the project. Here my DLL File name is rizDLL.dll.
Step 6
Now for adding the method and properties in the class file we simply go to the Solution Explorer then right-click on it and click on view Class Diagram.
When you click you have seen a Class diagram and in that diagram you will see an empty class.
Step 7
Now here we add a method to our class. For adding the method to our class we simply right-click on the class diagram and then we have an option, Add. Click on Add, now there are many options such as Add Method, Property, Field, Events and many more options.
When we click on the Add method then there is a method will Add to our class. Now in the bottom of our Visual Studio we have options to change the property of this method. Here we add two methods, one method name is mcTestMethod. In mcTestMethod we do not use any parameters. Another method name is Add Method, this method has three numbers and returns the sum of those numbers.
The overall process adds two methods to our class file and the methods look as in the given code.
- public void mcTestMethod()
- {
- throw new System.NotImplementedException();
- }
-
- public long Add(long val1, long val2, long val3)
- {
- throw new System.NotImplementedException();
- }
Step 8
Now we add a property to our class file. To add the property to our class file we simply follow the same procedure we followed to create the method. Simply go to the class diagram then right-click on it, click on Add, then click on the property. Here we add a property and name it Extra and typed it bool and used the public modifier with the get and the set properties.
Step 9
After Adding the property with the set and get method, add the following code to the class file. Here I change my class and constructor name, both for convenient implementation. Here we make the class name and constructor name usedll.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace rizDLL
- {
-
-
-
- public class usedll
- {
- private bool bTest = false;
- public usedll()
- {
-
-
-
- }
-
-
-
-
- public void mcTestMethod()
- {
-
- }
-
-
-
- public bool Extra
- {
- get
- {
- return bTest;
- }
- set
- {
- bTest = Extra;
- }
- }
-
- public long Add(long val1, long val2, long val3)
- {
- return val1 + val2 + val3;
- }
- }
- }
Step 10
After adding the code successfully we again build our solution and have a message in the output window that you have successfully built it. Now you can see your DLL code in your bin/debug folder.
Add DLL file in to the Another Program
Now I will explain how to use the rizDLL in your project. Use the following procedure.
Step 1
Again open Visual Studio 2015 Preview, go to the file menu, go to the project, add a new project then select the template for Visual C# then add a new console application.
Step 2
After that add the references for the library. So for this we go to the tab menu of Visual Studio then go to the tab project then go to the Add Reference command.
Step 3
When you click on Add References you will see a window like this.
Now simply click on the browse button and add the DLL file from the specific position where you saved it.
Step 4After adding the DLL file in your console application, please open the Solution Explorer of the your console application. Explore the references in the references. You will see that DLL file.
Step 5
Now open the class file of the Console Application and add the namespace
"using rizDLL".
Then add the following code to your console application file.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using rizDLL;
-
- namespace usingrizDll
- {
- class Class1
- {
-
-
-
- [STAThread]
- static void Main(string[] args)
- {
- usedll cls = new usedll();
- long lRes = cls.Add(23, 40,30);
- cls.Extra = false;
- Console.WriteLine("The sum of your three no is=" +lRes.ToString());
- Console.Read();
- }
- }
- }
Step 6
Now build your Console Application and Run it. After a successful build you will see the output as in the following picture.
Output
Summary
So this is the overall structure for making a DLL file and using the DLL file in another program in Visual Studio 2015 Preview. I think the procedure I told you is very easy for everyone to follow and make a DLL file and use the DLL file in another program. DLL files always play a very important role in Windows applications. Basically it works on the inheritance feature. I hope this article will be useful for the readers for creating a DLL and using the DLL in another program.