Open C Tips and Tricks |
Exporting global data from a DLL to be accessed by either Open C or Symbian C++ applications is one of the typical problems that developers encounter.
NOTE! It is strongly recommended to avoid having global data in DLLs due to following reasons:
On having understood the above limitations, the following pattern can be used for exporting global data from a DLL:
See the example below:
1. Do not export global variables.
Within DLL, say there is one global variable, for example:
int globalVal;
2. Export one method that returns a pointer to that variable
extern "C" EXPORT_C int* GlbData () { return &globalVal }
3. Define a macro for the user of the DLL
#ifdef __cplusplus extern "C" #endif IMPORT_C int* GlbData (); #define globalVal (*GlbData())
And the usage is like:
#include <xxx.h> // DLL header int main() { int i = 0; globalVal = 10; globalVal++; i = globalVal; return 0; }
©Nokia 2007 |