Dynamic Link Library DLL

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome this tutorial is all about dynamic link library I will explain what is a dll how to create a dll file using Visual Studio how to export the functions from a dll file how to use the exported functions into my application and how to link the DLL application using Visual Studio so let's see what is a DLL DLL is a dynamic link library dynamic link library is a collection of code and data which can be used by application this application is used by the end user let's say a vendor has written a code to calculate the greatest of two numbers but he doesn't want to sell you his lines of code then how can you use that function into your application the vendor is not ready to give his code but he is ready to give you a library file and in the library file or the code what he has written will calculate the greatest of two numbers so you will get a library file from the vendor and that library file you can use it in your application you can link it to your application and when whenever you pass two values a and B or suppose 10 and 20 this library will contain the function how to calculate the greatest of 10 and 20 and he will return you the greatest number that is 20 then the end user will see what is the greatest of two number 10 and 20 so this is a very small example what I have given so the basic concept of DLL is the same now let us go to an example how to create a dynamic link library so here I will write a function to calculate the factorial of a given number this number I will take as a parameter and this function will calculate the factorial of the number and return back to the client to the application let's say it returns the value after calculating the factorial control KF is used to format it will align the functions just select and say control KF so this function will create it will calculate the factorial of the given number item and this is the value what is the result this is the resultant value this is the factorial it will return to the calling function one most important thing what we have to do here that we have to export this function calculate factorial unless and until we export this function we cannot make use of this function in the library using our client project so to export a function we use a dll export that is Microsoft specific keyword suppose you have n number of functions other than this calculate factorial you have some four or five more functions and you want to export all the functions whatever is in the idea is in the my class then no need to write one by one declaration a specific darrel export before every function you can just directly export the class itself so this thing you can declare here and whatever functions public functions will be there in the class all the functions will be exported and a Lib file and a dot dll file will be generated let us compile this and see so at this location the dll is generated we can see that we have got two files basically a dll file and a lip file both are having the same name the Lib file contains the address of the functions which are in there in the DLL and this Lib file we can link in our client project that is in our application project let's see how we can link it for that we are going to create one application there we have our main file okay so to link the DLL the Lib file what is generated here from this project we have to do some kind of settings in the visual studio we can load the DLL in two ways we have something called implicit linking and explicit linking I will explain the other way in my next tutorial that is explicit linking we can use a function called load library to get the address of the exported function that I will explain later right now I will go with a simple technique how to link using visual studio so in the properties we have to do some kind of settings just right-click on the application because this application you are going to link it to your DLL so that the application can use all the exported functions from the main function so go to the properties here in the V C++ directories you need to add the include directories so we are going to include the header file what we created in the DLL so we can give relative path just go up okay so this is the place where my header file and CPP is there if we say ok we will be able to include the header file of the DLL so basically this is what we have created here in this path we have created a function that will calculate the factorial of the given number and it will return the result so I am going to use this function this function from our main then let's see how to link the library in the library directories we have to give which directory is containing the library so here is my ally be let's see Darrell call debug we have that I be here so the same path we have set in the V C++ library directories okay and here in the linker input we need to give the library name so the library name is DLL coil dot L IB now let's create an instance of this class and here you can call the function suppose we want to create the factorial of 5 and we are going to print it out or let's say we will get it from the user itself find them very clear now okay so let's see what is happening it says the program cannot start because the dll is missing from the computer it means my application expects the dll to be in the same place as the Exe so if we copy this dll to the exe location to this location my work will be done but what if I change some code again in my DLL project every time I need to go and manually copy it so let us give a post build event in the DLL project itself you need to understand how it works what is the output directory and what is the configuration so the place where it generates the DLL depends on these settings output directory solution directory configuration solution directory means where the dot Ursuline file is there so dot Ursuline file solution file will be created here where your vcx project is there so inside this solution directory we will have a configuration folder that is the debug configuration right now I have created the project in the debug mode so solution directory configuration means configuration is active debug so inside this folder he will create debug folder and inside this here is Esalen so in this is the place where is a solution located and inside this he will create a debug folder and here my dot DLL and dot lav will be generated and the name is decided by configuration project name dot DLL so project name is DLL call and dot dl l dot dl is my DLL so we have got DLL called or DLL here so we need to copy this DLL whenever we complete the build this DLL should be copied to my dot exe folder whichever is my application so how to do that let's see so this is the source place and so this dll will be copied to the target location I want to copy it to my dot exe this folder we can give the relative path also right now I'm just giving the absolute path so to this folder this dll will be copied now when I build it it will say one file copied when we go to our dot exe path we will see that the DLL is copied here now when I will run my application it will load the DLL and it will run let's see what is happening it created an instance of my DLL so this is the new what is happening there okay so this is an assembly code no need to check it out now it asks the user to enter a number for which he wants to calculate the factorial so I want to get the factorial of suppose six and I will say enter now it will calculate the factorial the number is six we'll go inside this function so six comes here and it calculates the factorial finally the factorial is 720 so the factorial of six is seven to 0 this will be returned here and the same will be printed on the console the factorial of six is 720 like this we can check something more some more numbers let's say three so the factorial three is six so this is the way how a dll works so whatever the functionality is there how to calculate the factorial is inside the dot dll file the client does not know how we are creating how the function is implemented client just knows that we have an exported function that is calculate factorial and it it requires a parameter number and it will return the factorial so client is supposed to do only this and the dll is doing the main job so suppose if we don't want to sell the line of code what exactly is happening in my function I will just give the dot dll file to the client and he can make use of the function and my work is done so this is the basic concept of how to use a dll how to link a dll how to export the function from a DLL file how we can use exported function into my application and how to link the DLL to an application using Visual Studio in my next tutorial I will show you how to use a DLL how to load a DLL using load library function and using function pointer till then thanks a lot please leave comments
Info
Channel: Ankit Anudeep
Views: 65,340
Rating: 4.5037594 out of 5
Keywords: Library (Software Genre), Dynamic Linker, Dynamic-link Library (File Format), Software (Industry), c++, dll, Educational Film (Film Genre)
Id: Ws5AhdN0hqQ
Channel Id: undefined
Length: 20min 43sec (1243 seconds)
Published: Sat Feb 28 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.