(Note: If you'd prefer to watch a video on the topic, click here for the youtube video.
What Are DLL Files?
DLL (Dynamic Link Library) files are shared libraries used on Microsoft platforms (read as: Windows operating system). Think of it as a supporting file that contains useful information (data, or functions) that can be used by an application (or other DLL file) during run-time.
Why are DLL Files Used?
So why would we want to use DLL files? There are a few reasons:
1. Our application file is smaller since we can hold data/code in a separate file.
2. Because the DLL file is separate from the running application, the RAM that would normally be required by the DLL file isn't used until the applications loads the DLL file.
3. Since our reusable code is held in a DLL file, if we use that code in many projects, but find a mistake in the DLL code and need to change it, we can simply recompile the DLL file and replace the old DLL file from all of our projects, as opposed to needing to edit raw code in all our projects and recompiling all of those projects.
Let's Make Our Own DLL File
So now that we know what a DLL file is, let's go through an example of making one.
Our example is going to be in C#. We're going to create a Class Library (.NET Standard) project in Visual Studio, and add some functions to a class for library.
In our example, we're going to create a math library that happens to have a Calculator class. This Calculator class has three functions, one for adding, one for multiplying, and one for saying hi.
When we build our project, a .dll file is generated. And that's it! That's our DLL file that contains the code for our shared library.
Now Let's Use the DLL File
Now that we've created a DLL file, let's actually use it in a different project.
Go ahead and create a basic C# Console App in Visual Studio. We need to do a few things:
1. Right click on the project name and Add a Reference to the DLL file we created. (Browse for it on your computer.)
2. To our main file in our current project, use the namespace that we used in our DLL file.
3. Inside of our Main function, instantiate the class that we want from that namespace, and use the functions for that class.
And just like that, we've used the DLL file that we created.
If that explanation was a bit vague to you and you want to see it actually done, then feel free to watch the youtube video embedded below. In it, I'll walk through the entire project with you.
Like this content and want more? Feel free to look around and find another blog post that interests you. You can also contact me through one of the various social media channels.
Hi, I'm srcmake. I play video games and develop software.
Pro-tip: Click the "DIRECTORY" button in the menu to find a list of blog posts.
License: All code and instructions are provided under the MIT License.