Symbian
Symbian OS Library

SYMBIAN OS V9.3

[Index] [Spacer] [Previous] [Next]



Dynamically loading link libraries example code


PolymorphicDLL1: polymorphic interface DLL 1


Example code

These files are found in: examples\Base\DLLs\PolymorphicDLL1

The files reproduced here are the main files contained in the examples directory. Some extra files may be needed to run the examples, and these will be found in the appropriate examples directory.

// PolymorphicDLL1.cpp
//
// Copyright (C) Symbian Software Ltd 2000-2005.  All rights reserved.

// This program creates a polymorphic interface DLL which
// executable "eulibdru" dynamically linkes to.
//

#include "PolymorphicDLL1.h"
#include <e32def.h>
#include <e32uid.h>



// Function to construct a CMessenger object. Note that this function
// is exported at ordinal 1 and is not a member of any class.

EXPORT_C CMessenger* NewMessenger()
    {
    return new (ELeave) CFrenchMessenger;
    }

// Class member functions

// second-phase constructor
void CFrenchMessenger::ConstructL(CConsoleBase* aConsole, const TDesC& aName)
    {
    iConsole=aConsole;    // remember console
    iName=aName.AllocL(); // copy given string into own descriptor
    }

// destructor
CFrenchMessenger::~CFrenchMessenger()
        {
        delete iName;
        }

// show message
void CFrenchMessenger::ShowMessage()
    {
    _LIT(KFormat1,"Bonjour, Je m'appelle %S \n");
    iConsole->Printf(KNullDesC);
    iConsole->Printf(KFormat1, iName);
    }

// PolymorphicDLL1.h
//
// Copyright (C) Symbian Software Ltd 2000-2005.  All rights reserved.

#ifndef __PolymorphicDLL1_H
#define __PolymorphicDLL1_H

// get definition of base class
#include "UsingDLLs.h"

class CFrenchMessenger : public CMessenger
      {
public:
    // constructor support
    virtual void ConstructL(CConsoleBase* aConsole, const TDesC& aName);
    // destructor
    virtual ~CFrenchMessenger();
    // useful functions
    virtual void ShowMessage();
    };

#endif

// PolymorphicDLL1.mmp
//
// Copyright (C) Symbian Software Ltd 2000-2005.  All rights reserved.

// using relative paths for source and userinclude directories


TARGET        PolymorphicDLL1.dll
TARGETTYPE    dll
UID           0x10004262 0x10004264
VENDORID 0x70000001

SOURCEPATH    .
SOURCE        PolymorphicDLL1.cpp

USERINCLUDE   .
USERINCLUDE   ..\UsingDLLs
SYSTEMINCLUDE \Epoc32\include

LIBRARY       euser.lib

#if defined(WINS)
    deffile .\POLYMORPHICDLL1WINS.def
#else if defined(ARM)
    deffile .\POLYMORPHICDLL1ARM.def
#endif
nostrictdef

Part of Using polymorphic interface DLLs.

[Top]


PolymorphicDLL2: polymorphic interface DLL 2


Example code

These files are found in: examples\Base\DLLs\PolymorphicDLL2

The files reproduced here are the main files contained in the examples directory. Some extra files may be needed to run the examples, and these will be found in the appropriate examples directory.

// PolymorphicDLL2.cpp
//
// Copyright (C) Symbian Software Ltd 2000-2005.  All rights reserved.

// This program creates a polymorphic interface DLL which
// executable "UsingDLLs" dynamically links to.
//


#include "PolymorphicDLL2.h"
#include <e32def.h>
#include <e32uid.h>


// Function to construct a CMessenger object. Note that this function
// is exported at ordinal 1 and is not a member of any class.

EXPORT_C CMessenger* NewMessenger()
    {
    return new (ELeave) CGermanMessenger;
    }

// Class member functions

// second-phase constructor
void CGermanMessenger::ConstructL(CConsoleBase* aConsole, const TDesC& aName)
    {
    iConsole=aConsole;    // remember console
    iName=aName.AllocL(); // copy given string into own descriptor
    }

// destructor
CGermanMessenger::~CGermanMessenger()
        {
        delete iName;
        }

// show message
void CGermanMessenger::ShowMessage()
    {
    _LIT(KFormat1,"Guten Tag, Ich heisse %S \n");
    iConsole->Printf(KNullDesC);
    iConsole->Printf(KFormat1, iName);
    }

// PolymorphicDLL2.h
//
// Copyright (C) Symbian Software Ltd 2000-2005.  All rights reserved.

#ifndef __PolymorphicDLL2_H
#define __PolymorphicDLL2_H

// get definition of base class
#include "UsingDLLS.h"

class CGermanMessenger : public CMessenger
      {
public:
    // constructor support
    virtual void ConstructL(CConsoleBase* aConsole, const TDesC& aName);
    // destructor
    virtual ~CGermanMessenger();
    // useful functions
    virtual void ShowMessage();
    };

#endif

// PolymorphicDLL2.mmp
//
// Copyright (C) Symbian Software Ltd 2000-2005.  All rights reserved.
// using relative paths for source and userinclude directories



TARGET        PolymorphicDLL2.dll
TARGETTYPE    dll
UID           0x10004262 0x10004266
VENDORID 0x70000001

SOURCEPATH    .
SOURCE        PolymorphicDLL2.cpp

USERINCLUDE   .
USERINCLUDE   ..\UsingDLLs
SYSTEMINCLUDE \Epoc32\include

LIBRARY       euser.lib

START WINS
  BASEADDRESS   0x50000000
END

#if defined(WINS)
    deffile .\POLYMORPHICDLL2WINS.def
#else if defined(ARM)
    deffile .\POLYMORPHICDLL2ARM.def
#endif
nostrictdef

Part of Using polymorphic interface DLLs.

[Top]


UsingDLLs: using polymorphic interface DLLs


Example code

These files are found in: examples\Base\DLLs\UsingDLLs

The files reproduced here are the main files contained in the examples directory. Some extra files may be needed to run the examples, and these will be found in the appropriate examples directory.

// UsingDLLs.cpp
//
// Copyright (C) Symbian Software Ltd 2000-2005.  All rights reserved.
// A program which uses dynamically linked DLLs.

// standard example header
#include "CommonFramework.h"
// f32 header needed for loading dlls.
#include <f32file.h>

#include "UsingDLLs.h"

_LIT(KTxtHarry,"Harry");
_LIT(KTxtSally,"Sally");
_LIT(KTxtDr1,"PolymorphicDLL1.DLL");
_LIT(KTxtDr2,"PolymorphicDLL2.DLL");

_LIT(KTxt1,"dynamically linked DLL example \n\n");
_LIT(KTxt2,"checking UID\n");
_LIT(KTxt3,"DLL has incorrect UID... \n");

void UseDllL(const TFileName& aDllName, const TDesC& aName);

LOCAL_C void doExampleL()
    {
    // open file server session.
    RFs fs;
    User::LeaveIfError(fs.Connect());
    console->Printf(KTxt1);
    // use each DLL in turn
    TFileName  dll;
    dll = KTxtDr1;
    UseDllL(dll, KTxtHarry);
    dll = KTxtDr2;
    UseDllL(dll, KTxtSally);
    // close the file server session.
    fs.Close();
    }

// how to use a DLL

void UseDllL(const TFileName& aLibraryName, const TDesC& aName)
    {
        // Use RLibrary object to interface to the DLL
    RLibrary library;
        // Dynamically load DLL
    User::LeaveIfError(library.Load(aLibraryName));
        // Check second UID is as expected; leave if not
    console->Printf(KTxt2);
    
    if (library.Type()[1] != KMessengerUid)
        {
        console->Printf(KTxt3);
        User::Leave(KErrGeneral);
        }
        // Function at ordinal 1 creates  new CMessenger
    TLibraryFunction entry=library.Lookup(1);
        // Call the function to create new CMessenger
    CMessenger* messenger=(CMessenger*) entry();
        // Push pointer to CMessenger onto the cleanup stack
    CleanupStack::PushL(messenger);
        // Second-phase constructor for CMessenger
    messenger->ConstructL(console, aName);
        // Use CMessenger object to issue greeting
    messenger->ShowMessage();
        // Pop CMessenger object off cleanup stack and destroy
    CleanupStack::PopAndDestroy();
        // Finished with the DLL
    library.Close();
    }

// UsingDLLs.h
//
// Copyright (C) Symbian Software Ltd 2000-2005.  All rights reserved.
// A program which uses dynamically linked DLLs.

    // standard example header
#include <e32cons.h>

#ifndef __UsingDLLs_H
#define __UsingDLLs_H

    // The UID for Messenger DLLs.
    // The client imposes this on DLLs which are required
    // to satisfy the protocol 

const TInt KMessengerUidValue=0x10004262;
const TUid KMessengerUid={KMessengerUidValue};

class CMessenger : public CBase
      {
public:
    virtual void ConstructL(CConsoleBase* aConsole, const TDesC& aName)=0;
    virtual void ShowMessage()=0;
protected:
    CConsoleBase* iConsole;
    HBufC*        iName;
    };

#endif

// UsingDLLs.mmp
//
// Copyright (C) Symbian Software Ltd 2000-2005.  All rights reserved.
// using relative paths for source and userinclude directories

TARGET        UsingDLLs.exe
TARGETTYPE    exe
UID           0
VENDORID 0x70000001

SOURCEPATH    .
SOURCE        UsingDLLs.cpp

USERINCLUDE   .
USERINCLUDE   ..\..\CommonFramework
SYSTEMINCLUDE \Epoc32\include

LIBRARY       euser.lib  efsrv.lib


Description

UsingDLLs is an example which shows how to load and use the two DLLs implemented in the two examples PolymorphicDLL1 and PolymorphicDLL2. Both DLLs implement the same interface in different ways.

Note that this example just shows basic principles. In practice, applications will use the ECOM mechanism to load DLLs.


Build Notes

PolymorphicDLL1 and PolymorphicDLL2 must be built before UsingDLLs.

// BLD.INF
// Component description file
//
// Copyright (c) 2000 Symbian Ltd.  All rights reserved.

PRJ_MMPFILES

PolymorphicDLL1\PolymorphicDLL1.mmp
PolymorphicDLL2\PolymorphicDLL2.mmp
UsingDLLs\UsingDLLs.mmp


Classes used