3

Is it possible to create a function in a shared library (.dll on Windows, and .so on linux) that is executed right when the library is loaded (or unloaded)?

Just like the main() function is the entry point for an executable, can I define a function to execute when the DLL is loaded, or unloaded?

E.g.:

void _atstart()
{
    // Initialize some stuff needed by the library
}

void _atexit()
{
    // Release some allocated resources
}

I think I've seen such an example somewhere, but I couldn't find it any more, and couldn't find anything on the internet about this.

If it is of any use, I'm compiling the code with MinGW.

Tibi
  • 4,015
  • 8
  • 40
  • 64
  • See also: [Automatically executed functions when loading shared libraries](https://stackoverflow.com/questions/9759880/automatically-executed-functions-when-loading-shared-libraries) – David Tonhofer Apr 20 '21 at 17:32
  • See also: [How exactly does __attribute__((constructor)) work?](https://stackoverflow.com/questions/2053029/how-exactly-does-attribute-constructor-work) – David Tonhofer Apr 20 '21 at 17:32

4 Answers4

6

In C++ you can at least create a global instance of some class

class ResourceHolder {
public:
    ResourceHolder() {
        // at start
    }

    ~ResourceHolder() {
        // at exit
    }
};

ResourceHolder theHolder;

Some awareness is required though if you use another global variables in your library.

unkulunkulu
  • 11,576
  • 2
  • 31
  • 49
3

For windows you can use DllMain:

BOOL WINAPI DllMain(
  __in  HINSTANCE hinstDLL,
  __in  DWORD fdwReason,
  __in  LPVOID lpvReserved
);

The second parameter fdwReason specifies if the library is loaded or unloaded. Full reference: http://msdn.microsoft.com/en-us/library/windows/desktop/ms682583(v=vs.85).aspx

BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved)
{
    switch (fdwReason)
    {
    case DLL_PROCESS_ATTACH:
        // code for library load
        break;
    case DLL_PROCESS_DETACH:
        // code for library unload
        break;
    }
    return (TRUE);
}

For Linux you might be able to use:

__attribute__ ((constructor))
__attribute__ ((destructor)) 

but this only came up after a google search, so you have to investigate by yourself - http://tdistler.com/2007/10/05/implementing-dllmain-in-a-linux-shared-library

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
1

As it has been said, under Window you can work from DllMain. But be careful what you will do since there is a lot of restrictions (use of COM CoInitialize function is forbidden, for instance). One thing you can't rely on is that there is no guaranty in what order dll will be loaded/unloaded, so you must not call functions from your DllMain that resides in an other of your dll : it can works today, but not tomorrow :)

More online on the MSDN : [http://msdn.microsoft.com/en-us/library/windows/desktop/ms682583(v=vs.85).aspx]

Patrice

Lpat
  • 11
  • 2
0

Under Windows you can write your own version of DllMain().

Constantine Georgiou
  • 2,412
  • 1
  • 13
  • 17