Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

How to create an ATL DLL

0.00/5 (No votes)
16 May 2006 2  
This article shows how to simply create and use an ATL DLL in VC++.

Introduction

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:

  1. Open Microsoft Visual Studio, click on the File menu, and then select New, and select Project....
  2. In Project Types, select Visual C++ Projects, and click on ATL.
  3. In the Templates Window (at the right), select ATL project, give it a name, and click OK.

  4. 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 DllMain, DllRegisterServer, DllUnregisterServer, DllGetClassObject, and DllCanUnloadNow."

    • If you want to use MFC in your DLL, unckeck the Attributed checkbox and check the Support MFC checkbox.
  5. Select other options like the picture, and click Finish.
  6. In the Solution Explorer window, click on Class View, right click on your project name, point to Add, and select Add class....

  7. In the "Add class" window, select ATL Simple Object at the Templates window, and click Open.

  8. 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).
  9. Leave other options as they are, and click on the Finish button.
  10. Now, we have our DLL. We should now add a function to it.
  11. 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).
  12. I have selected the Add Method item.
  13. Select the yourobjectname.cpp tab at the top.

  14. Insert your code in the block, and select Release... from top.

  15. At the Debug menu, select Start (or press F5), click Yes.
  16. Now that the DLL is released properly, you can get it from the project address under the release folder.

That's it!

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:

  1. Use:
    #import "drive:\\folder\\dllname.dll" 
            rename_namespace ("namespacename")
    using namespace namespacename;
  2. Declare a public pointer variable from yourobjectname.
  3. Initialize your DLL:
    ::CoInitialize(NULL); 
    HRESULT hr=::CoCreateInstance(__uuidof(yourobjectname), NULL, 
                 CLSCTX_ALL,__uuidof(Iyourobjectname), 
                 (LPVOID*)(&variablename));
  4. Now, we are ready to use our simple DLL and our simple method.
  5. Finish...

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:

  1. Open the Start menu and open the Run window (or press Win logo key +R).
  2. In the Open field, type REGSVR32 DllAddress (exactly where it is and the app. uses it).
  3. Click OK (or press the Enter key).
  4. The message "DlRegisterServer in pathname succeeded" appears.

Have a great coding!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here