Before I learned programming and when I was a beginner in Visual Basic, I didn't know anything about DLLs, what kind of code is in them, or what do they do. Now, I know that they are very useful. Because, we can put our functions in DLLs, and make our code simpler, smoother, and also faster. We can update our code only by upgrading the DLL which we want. We can put our resources in DLLs and use them. So by using DLLs, our applications will be upgradeable in a simple way.
In this article, we'll learn how to create an ATL DLL, initialize it, and then use it - a very simple thing.
1. Creating the DLL
First, we must create a DLL. So follow these steps:
- Open Microsoft Visual Studio, click on the File menu, and then select New, and select Project....
- In Project Types, select Visual C++ Projects, and click on ATL.
- In the Templates Window (at the right), select ATL project, give it a name, and click OK.
- The "ATL Project Wizard" appears. Then, left click on Application Settings:
- If you want to use MFC in your DLL, uncheck the Attributed checkbox. MSDN says:
"This attribute automatically implements
- If you want to use MFC in your DLL, unckeck the Attributed checkbox and check the Support MFC checkbox.
- Select other options like the picture, and click Finish.
- In the Solution Explorer window, click on Class View, right click on your project name, point to Add, and select Add class....
- In the "Add class" window, select ATL Simple Object at the Templates window, and click Open.
- In the window that appears (ATL simple object wizard), in the Shortname textbox, give a name for your object (without the C letter before it).
- Leave other options as they are, and click on the Finish button.
- Now, we have our DLL. We should now add a function to it.
- In the Solution Explorer, right-click on your object name, point to Add, and select one of the items (the one that serves your purpose).
- I have selected the Add Method item.
- Select the yourobjectname.cpp tab at the top.
- Insert your code in the block, and select Release... from top.
- At the Debug menu, select Start (or press F5), click Yes.
- Now that the DLL is released properly, you can get it from the project address under the release folder.
2. Initializing the DLL (importing and using)
Now, to test your DLL (or to use it), create a MFC project. Follow these steps to initialize the DLL:
using namespace namespacename;
- Declare a public pointer variable from yourobjectname.
- Initialize your DLL:
HRESULT hr=::CoCreateInstance(__uuidof(yourobjectname), NULL,
- Now, we are ready to use our simple DLL and our simple method.
Just don't forget to register it!
3. Why my DLL won't work in another computer?
When you run your application that uses the DLL in other computer or a newly installed OS, it will give you an error. So what should I do? When you create your DLL using Microsoft Visual Studio, it automatically registers your DLL in Windows. So you (or your app) must do these simple things:
- Open the Start menu and open the Run window (or press Win logo key +R).
- In the Open field, type REGSVR32 DllAddress (exactly where it is and the app. uses it).
- Click OK (or press the Enter key).
- The message "DlRegisterServer in pathname succeeded" appears.
Have a great coding!