Click here to Skip to main content
15,897,226 members
Articles / Programming Languages / C

HowTo: Export C++ classes from a DLL

Rate me:
Please Sign up or sign in to vote.
4.94/5 (209 votes)
13 Nov 2012CPOL17 min read 1.2M   14.9K   488  
The C++ programming language and Windows DLLs can live in peace after all.
// XyzExecutable.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "XyzLibrary.h"

#if _MSC_VER > 1200     // VC++7.0 and higher
#include "AutoClosePtr.h"
#elif _MSC_VER == 1200  // VC++6.0
#include "AutoClosePtrVC6.h"
#else                   // really old version
#error  Newer MS VC++ compiler required.
#endif // _MSC_VER

int _tmain(int /*argc*/, _TCHAR* /*argv*/[])
{
    // 1. COM-like usage.
    IXyz* pXyz = ::GetXyz();

    if(pXyz)
    {
        pXyz->Foo(42);
        pXyz->Release();
    }
    //

#if _MSC_VER >= 1600    // VC++10.0 and higher
    
    // 1a. Use smart pointers from the Standard C++ library.
    typedef std::shared_ptr<IXyz> IXyzPtr;

    IXyzPtr ptrXyz(::GetXyz(), std::mem_fn(&IXyz::Release));

    if(ptrXyz)
    {
        ptrXyz->Foo(42);
    }
    //

#elif _MSC_VER >= 1300 && _MSC_VER < 1600   // VC++7.0 - VC++9.0

    // 1b. COM-Like usage with smart pointer.
    // No need to call `IXyz::Release'; the instance will
    // be released automatically in destructor of the smart pointer.
    typedef AutoClosePtr<IXyz, void, &IXyz::Release> IXyzPtr;

    IXyzPtr ptrXyz(::GetXyz());

    if(ptrXyz)
    {
        ptrXyz->Foo(42);
    }
    //

#else   // VC++6.0

    // 1c. COM-Like usage with smart pointer.
    // No need to call `IXyz::Release'; the instance will
    // be released automatically in destructor of the smart pointer.
    typedef AutoClosePtr<IXyz> IXyzPtr;

    IXyzPtr ptrXyz(&IXyz::Release, ::GetXyz());

    if(ptrXyz)
    {
        ptrXyz->Foo(42);
    }
    //

#endif // _MSC_VER

    // 2. Regular C++ class.
    // The class must be exported.
    CXyz xyz;
    xyz.Foo(42);
    //

    // 3. Classic C language usage.
    XYZHANDLE hXyz = GetXyz();

    if(hXyz)
    {
        XyzFoo(hXyz, 42);
        XyzRelease(hXyz);
        hXyz = NULL;
    }
    //

	return 0;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Australia Australia
More than ten years of C++ native development, and counting.

Smile | :)

Comments and Discussions