Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. C++ DLL question

C++ DLL question

Scheduled Pinned Locked Moved C / C++ / MFC
questioncsharpc++help
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Keith Vitali
    wrote on last edited by
    #1

    Hello, I have a small DLL written in C++ that exports 4 functions. I call this DLL from my C# application and can call all these functions without any problem. I want to arrange for a DLL function to be called exactly once (sort of like a static constructor for the DLL). What is the best way to do something like this? Thanks, Keith

    H A 2 Replies Last reply
    0
    • K Keith Vitali

      Hello, I have a small DLL written in C++ that exports 4 functions. I call this DLL from my C# application and can call all these functions without any problem. I want to arrange for a DLL function to be called exactly once (sort of like a static constructor for the DLL). What is the best way to do something like this? Thanks, Keith

      H Offline
      H Offline
      hanq_38910130
      wrote on last edited by
      #2

      What do you mean by "called exactly once"? You wanna prevent from "calling the dll function for a second time", or just make no effect when "calling the dll function for a second time"?

      1 Reply Last reply
      0
      • K Keith Vitali

        Hello, I have a small DLL written in C++ that exports 4 functions. I call this DLL from my C# application and can call all these functions without any problem. I want to arrange for a DLL function to be called exactly once (sort of like a static constructor for the DLL). What is the best way to do something like this? Thanks, Keith

        A Offline
        A Offline
        Aescleal
        wrote on last edited by
        #3

        You could try sticking the code you want executed exactly once in the DllMain for the DLL. Unfortunately there are some restrictions as to what can go in DllMain - don't do anything that might trigger another DLL load or it could go horribly wrong. Another option is to create an initialisation function and either just call it once or use a language feature of C# to make sure it's only called once. I don't know C# and it's idioms enough to suggest a relevant feature. You can make sure your initialisation function is only called once in the DLL using something like:

        extern "C"
        bool FOO_EXPORT initialise_foo_lib()
        try
        {
        static bool initialised = false;
        if( !initialised )
        {
        // Do rest of initialisation

            //...
        
            initialised = true;
        }
        
        return initialised;
        

        }
        catch( std::exception & )
        {
        return false;
        }

        Cheers, Ash

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups