Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8211153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:20:00+00:00 2026-06-07T10:20:00+00:00

Is it possible to create a function in a shared library (.dll on Windows,

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T10:20:03+00:00Added an answer on June 7, 2026 at 10:20 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a 'debug' shared library (i.e., .so or .dll file) that
Is it possible to create a template function that takes a variable number of
I would like to make a function that will create objects that have a
I like to create an extension method to Image.FromStream Public Shared Function FromStream(ByVal stream
In MATLAB it is possible to create function handles with something like myfun=@(arglist)body This
Is it possible to create a function that can be called by multiple different
is it possible to create a function to shorten casts, like (pseudocode): MyFragment fragment
Is it possible to create a function out of a class method? ie. class
Is it possible to create a function which takes a pointer to another function?
It's possible to call C functions through CREATE FUNCTION but how can .NET functions

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.