Create c++ dll in visual studio 2005

Posted on 2007-08-02
Last Modified: 2009-05-03
I need directions for creating a dynamic library in visual studio 2005, instead of a static library. The library should not necessary be conforming to the "COM" sta ndard the only demand is that I can use it in other c++ projects.

The dll should be compiled from some h- and cpp files. Then I expect a .lib file and a .dll file as output.

I have the following understanding of the issue.

To use the dll in another project, you references the h files and link against the .lib file, which basically just tells that we should fetch the dll at runtime and lookup the symbols in this file, but how to do this.

Some steps or examples will be appreciated. I have found

but it does not really help me

Thanks, Niels
Question by:nielsboldt
    LVL 86

    Accepted Solution

    See the articles under - especially ("Super Easy DLL") and ("Making DLLs easy to build and use") which are both compact and will give you a jump start on this issue.
    LVL 30

    Assisted Solution

    Hi nielsboldt,

    if I understand you right you want to make a DLL which is implicitely linked, so you don't want to load/call the DLL at runtime using LoadLibrary/GetProcAddress.

    To do so first step is to create a new Win32 project of type DLL - depending on your needs you can use MFC and/or ATL there. You need to take care that you use the same C-Runtime libraries (static or dynamically linked, single ot multithreaded) as in the calling application.

    Next you need to export the functions/classes you want to use in the calling application. Therefor you can declare a macro in your header files somehow like this:

     #ifdef MY_APP_EXPORT
       #define MY_APP_API __declspec(dllexport)
       #define MY_APP_API __declspec(dllimport)

    The MY_APP_EXPORT should be declared in the comile-settings 'Preprocessor->Preprocessor Definitions'

    Then you can declare any function or class you want to export in header files like this:

    void MY_APP_API foo();

    class MY_APP_API MyClass

    This way you can use the same header both in the DLL and the calling application - in the DLL the macro expands to __declspec(dllexport) which exports the declared functions/classes, in the calling application the macro expands to __declspec(dllimport) which declares the functions/classes in a way the compiler will generate import code.

    Then you only have to link the .LIB which is built with the DLL to the application in order to use the exported functions/classes in the application.

    I hope that helps,

    LVL 86

    Expert Comment

    Also of interest: ("A Short Tutorial on Writing a WIN32 DLL") ("A Tutorial on creating DLLs with VC++") ("Creating And Using DLLs")
    LVL 1

    Expert Comment

    Forced accept.

    EE Admin

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    Recently Microsoft released a brand new function called CONCAT. It's supposed to replace its predecessor CONCATENATE. But how does it work? And what's new? In this article, we take a closer look at all of this - we even included an exercise file for…
    Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
    This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
    The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now