|
||
CreateStaticDLL
and UseStaticDLL
: using
a statically linked DLL
These are the main files contained in the examples. Some extra files may be needed to run the examples, and these will be found in the appropriate examples directory.
// CreateStaticDLL.cpp
//
// Copyright (c) 2000-2005 Symbian Software Ltd. All rights reserved.
// This program creates a dll.
#include "CreateStaticDLL.h"
#include <e32uid.h>
// construct/destruct
EXPORT_C CMessenger* CMessenger::NewLC(CConsoleBase& aConsole, const TDesC& aString)
{
CMessenger* self=new (ELeave) CMessenger(aConsole);
CleanupStack::PushL(self);
self->ConstructL(aString);
return self;
}
CMessenger::~CMessenger() // destruct - virtual, so no export
{
delete iString;
}
EXPORT_C void CMessenger::ShowMessage()
{
_LIT(KFormat1,"%S\n");
iConsole.Printf(KFormat1, iString); // notify completion
}
// constructor support
// don't export these, because used only by functions in this DLL, eg our NewLC()
CMessenger::CMessenger(CConsoleBase& aConsole) // first-phase C++ constructor
: iConsole(aConsole)
{
}
void CMessenger::ConstructL(const TDesC& aString) // second-phase constructor
{
iString=aString.AllocL(); // copy given string into own descriptor
}
// UseStaticDLL.cpp
//
// Copyright (c) 2000 Symbian Ltd. All rights reserved.
// A simple program which uses the statically linked dll "eulibd1b.dll"
// standard example header
#include "CommonFramework.h"
#include "CreateStaticDLL.h"
_LIT(KTxt1,"statically linked DLL example \n\n");
_LIT(KTxt2,"Hello!");
_LIT(KTxtNewLine,"\n");
LOCAL_C void doExampleL()
{
console->Printf(KTxt1);
CMessenger* myMessage = CMessenger::NewLC(*console, KTxt2);
myMessage->ShowMessage();
console->Printf(KTxtNewLine);
CleanupStack::PopAndDestroy();
}
// CreateStaticDLL.h
//
// Copyright (c) 2000 Symbian Ltd. All rights reserved.
// Statically linked dll example
#include <e32cons.h>
class CMessenger : public CBase
{
public:
// Construction
IMPORT_C static CMessenger* NewLC(CConsoleBase& aConsole, const TDesC& aString);
// Destructor - virtual and class not intended
// for derivation, so not exported
~CMessenger();
// general functions - exported
IMPORT_C void ShowMessage();
private:
// C++ constructor - not exported;
// implicitly called from NewLC()
CMessenger(CConsoleBase& aConsole);
// 2nd phase construction, called by NewLC()
void ConstructL(const TDesC& aString); // second-phase constructor
private:
CConsoleBase& iConsole; // Use the console (but not owned)
HBufC* iString; // Allocated container for string data (destructor destroys)
};
// BLD.INF
// Component description file
//
// Copyright (c) 2000 Symbian Ltd. All rights reserved.
PRJ_MMPFILES
CreateStaticDLL.mmp
UseStaticDLL.mmp
// CreateStaticDLL.mmp
//
// Copyright (c) 2000-2005 Symbian Software Ltd. All rights reserved.
// using relative paths for sourcepath and user includes
// exports are unfrozen
TARGET CreateStaticDLL.dll
TARGETTYPE dll
UID 0x10004268
CAPABILITY All -TCB
VENDORID 0x70000001
SOURCEPATH .
SOURCE CreateStaticDLL.cpp
USERINCLUDE .
SYSTEMINCLUDE \Epoc32\include
LIBRARY euser.lib
#if defined(WINS)
deffile .\CreateStaticDLLWINS.def
#else if defined(ARM)
deffile .\CreateStaticDLLARM.def
#endif
nostrictdef
// UseStaticDLL.mmp
//
// Copyright (c) 2000 Symbian Ltd. All rights reserved.
// using relative paths for sourcepath and user includes
TARGET UseStaticDLL.exe
TARGETTYPE exe
UID 0
VENDORID 0x70000001
SOURCEPATH .
SOURCE UseStaticDLL.cpp
USERINCLUDE .
USERINCLUDE ..\CommonFramework
SYSTEMINCLUDE \Epoc32\include
LIBRARY euser.lib CreateStaticDLL.lib
The example shows how a class implemented in a statically linked DLL is
used. The DLL is built by CreateStaticDLL
and is used by
UseStaticDLL
.
The source code for this example can be found in the directory:
examples\Basics\StaticDLL
It may be in the directory in which you installed Symbian OS, or it may
be in src\common\developerlibrary\
. It includes the project files
needed for building: bld.inf
and two .mmp
files.
The Symbian OS build process describes how to build Symbian programs. To run this example:
Build CreateStaticDLL
project, which results in the
following:
\epoc32\release\<target>\<urel or
udeb>\CREATESTATICDLL.DLL
.
\epoc32\release\<target>\<urel or
udeb>\CREATESTATICDLL.lib
.
They are needed by the UseStaticDLL
project.
Build UseStaticDLL
, which results in:
\epoc32\release\<target>\<urel or
udeb>\USESTATICDLL.EXE
.
This project use the DLL created in the previous steps.
Run the executable USESTATICDLL.EXE
.
Executables for the winscw
target can be run on your
PC. Executables for ARM targets must be copied to a phone before being run.