S60 Open C
Open C Tips and Tricks

Open C Tips and Tricks

Table of Contents

How to initialize a library
Export functions for initializing and closing the library
Exported functions check whether the library is initialized
Library defines a static object

 


How to initialize a library

Sometimes, a library needs to be initialized before it can provide any services to client of the library. This can be done in several ways.

 


Export functions for initializing and closing the library

  • The library can export more than one function for initializing and closing the library.
  • The client of the library calls the initialization function before requesting any services and calls close when the client does not use the library.

Example:

We have a DLL called xxx.dll. Two functions InitXXX() and CloseXXX() are exported from this DLL. The library code can look like the following:

Library header file xxx_init.h:
....
IMPORT_C void InitXXX();
IMPORT_C CloseXXX();
....
Library source file xxx_init.cpp:
static int _InitCount = 0;
EXPORT_C void InitXXX()
{
   if(_InitCount++ <= 0)
   {
   // initialize xxx.dll
   }
}
EXPORT_C CloseXXX()
{
   if(--_InitCount <= 0)
   {
   // free all the resources
   }
}
User code looks like the following:
#include <xxx_init.h>
...
int main()
{
   InitXXX();
   ....
   ....
   CloseXXX();
   return 0;
}

 


Exported functions check whether the library is initialized

Every exported function can check whether the library has been initialized. The library can export one function (for example,CloseLib()) and this can be called before exiting main( ).

Example:

Library header file xxx.h:
....
IMPORT_C void X_Export_Api1();
IMPORT_C void X_Export_Api2();
....
Library source file xxx.cpp:
static int _InitCount = 0;
#define INIT_LIB if(_InitCount == 0) \
           {\
               _InitCount++; \
               InitXXX(); \
           }
LOCAL_C void InitXXX()
{
   // initialize xxx.dll
}
LOCAL_C CloseXXX()
{
   // free all the resources
}
EXPORT_C void X_Export_Api1()
{
   INIT_LIB;
   ...
   ...
}

User code looks like the following:

#include <xxx.h>
...
int main()
{
   ....
   ....
   X_Export_Api1();
   ....
   ....
   X_Export_Api2();
   ....
   ....
   return 0;
}

 


Library defines a static object

The library can define static objects and call the initialization code from the constructor of these static objects so that before calling, the application entry point library will be initialized.

Example:

Library header file xxx.h:
....
IMPORT_C void X_Export_Api1();
IMPORT_C void X_Export_Api2();
....
Library source file xxx.cpp:
LOCAL_C void InitXXX()
{
   // initialize xxx.dll
}
LOCAL_C CloseXXX()
{
   // free all the resources
}
class LibInit
{
   LibInit();
   ~LibInit();
};
LibInit::LibInit()
{
   // put all library initialization code here.
   InitXXX();
}
LibInit::~LibInit()
{
   // free all resources
   CloseXXX();
}
static LibInit _libInit;
User code looks like the following:
#include <xxx.h>
...
int main()
{
   ....
   ....
   X_Export_Api1();
   ....
   ....
   X_Export_Api2();
   ....
   ....
   return 0;
}

Give feedback of this section


©Nokia 2007

Back to top


This material, including documentation and any related computer programs, is protected by copyright controlled by Nokia. All rights are reserved. Copying, including reproducing, storing, adapting or translating, any or all of this material requires the prior written consent of Nokia. This material also contains confidential information, which may not be disclosed to others without the prior written consent of Nokia.

Nokia is a registered trademark of Nokia Corporation. S60 and logo is a trademark of Nokia Corporation. Java and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. Other company and product names mentioned herein may be trademarks or tradenames of their respective owners.