Open C Tips and Tricks |
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.
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:
.... IMPORT_C void InitXXX(); IMPORT_C CloseXXX(); ....
static int _InitCount = 0; EXPORT_C void InitXXX() { if(_InitCount++ <= 0) { // initialize xxx.dll } } EXPORT_C CloseXXX() { if(--_InitCount <= 0) { // free all the resources } }
#include <xxx_init.h> ... int main() { InitXXX(); .... .... CloseXXX(); return 0; }
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:
.... IMPORT_C void X_Export_Api1(); IMPORT_C void X_Export_Api2(); ....
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; }
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:
.... IMPORT_C void X_Export_Api1(); IMPORT_C void X_Export_Api2(); ....
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;
#include <xxx.h> ... int main() { .... .... X_Export_Api1(); .... .... X_Export_Api2(); .... .... return 0; }
©Nokia 2007 |