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. My DllMain is not getting invoked.

My DllMain is not getting invoked.

Scheduled Pinned Locked Moved C / C++ / MFC
toolsquestion
6 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.
  • M Offline
    M Offline
    Matthew Busche
    wrote on last edited by
    #1

    I'm trying to use DllMain() to take care of some library/thread initialization and cleanup stuff, but no matter what I try I cannot get DllMain invoked for any of the four events for which it is supposed to be getting invoked! (I.e., process attach, thread attach, thread detach, process detach.) I found the following sentence in the MSDN description of DllMain():

    "DllMain is a placeholder for the library-defined function name. You must specify the actual name you use when you build your DLL. For more information, see the documentation included with your development tools."

    Is there some Dev Studio project setting I should be making to identify the name of my library entry point function? Another tidbit of information: I'm using simple load-time linking for my DLL. The MSDN DllMain description makes reference to LoadLibrary and FreeLibrary (functions you use for run-time dynamic linking). I sould be interested in hearing confirmation that DllMain IS compatible with simple load-time dynamic linking. Thanks much, Matt

    P 1 Reply Last reply
    0
    • M Matthew Busche

      I'm trying to use DllMain() to take care of some library/thread initialization and cleanup stuff, but no matter what I try I cannot get DllMain invoked for any of the four events for which it is supposed to be getting invoked! (I.e., process attach, thread attach, thread detach, process detach.) I found the following sentence in the MSDN description of DllMain():

      "DllMain is a placeholder for the library-defined function name. You must specify the actual name you use when you build your DLL. For more information, see the documentation included with your development tools."

      Is there some Dev Studio project setting I should be making to identify the name of my library entry point function? Another tidbit of information: I'm using simple load-time linking for my DLL. The MSDN DllMain description makes reference to LoadLibrary and FreeLibrary (functions you use for run-time dynamic linking). I sould be interested in hearing confirmation that DllMain IS compatible with simple load-time dynamic linking. Thanks much, Matt

      P Offline
      P Offline
      Peter Molnar
      wrote on last edited by
      #2

      Matthew, further to your (previous) question: DllMain of your *.dll library gets called on 1.program startup, if your user staticly linked to the *.lib library of your *.dll project when compiling his app. (also called load time or explicit linking) 2.your user calling LoadLibrary, FreeLibrary from his code (also called runtime or implicit linking) In case 1./ the user of your library has to get from you a *.lib and a *.h file of your library in order to compile his project, and of course your *.dll to run with your *.dll attached. In case 2./ the user of your library has to get from you an *.h file of your library (i.e. function prototypes) to compile his code (by calling LoadLibrary, GetProcAddress and FreeLibrary) and your *.dll to run the library functions. Peter Molnar

      M 1 Reply Last reply
      0
      • P Peter Molnar

        Matthew, further to your (previous) question: DllMain of your *.dll library gets called on 1.program startup, if your user staticly linked to the *.lib library of your *.dll project when compiling his app. (also called load time or explicit linking) 2.your user calling LoadLibrary, FreeLibrary from his code (also called runtime or implicit linking) In case 1./ the user of your library has to get from you a *.lib and a *.h file of your library in order to compile his project, and of course your *.dll to run with your *.dll attached. In case 2./ the user of your library has to get from you an *.h file of your library (i.e. function prototypes) to compile his code (by calling LoadLibrary, GetProcAddress and FreeLibrary) and your *.dll to run the library functions. Peter Molnar

        M Offline
        M Offline
        Matthew Busche
        wrote on last edited by
        #3

        Peter, Thanks for your confirmation that this at least should work. So why then is my DllMain not getting invoked. Here's my current inplementation:

        #include "MyHeader.h"
        #include "windows.h"

        BOOL DllMain(HINSTANCE , DWORD pReason, LPVOID ) {
        try {
        printf("I'M HERE!\n");
        int *x = NULL;
        *x = 20;

        switch (pReason) {
        case DLL_PROCESS_ATTACH:
        MyProcessAttach();
        MyThreadAttach();
        break;
        case DLL_THREAD_ATTACH:
        MyThreadAttach();
        break;
        case DLL_THREAD_DETACH:
        MyThreadDetach();
        break;
        case DLL_PROCESS_DETACH:
        MyThreadDetach();
        MyProcessDetach();
        break;
        default:
        break;
        }
        }
        catch(std::exception& x) {
        std::cerr << x.what() << std::endl;
        return FALSE;
        }
        return FALSE;
        }

        where all the stuff in bold is just some garbage I've added trying to get it to print something, or crash or generate some failed load situation to show me that the function is getting called. It would certainly seem this function is never getting invoked. What the heck is going on? Again, do I need some project setting to identify this as an entry point function for the library? Matt

        P M 2 Replies Last reply
        0
        • M Matthew Busche

          Peter, Thanks for your confirmation that this at least should work. So why then is my DllMain not getting invoked. Here's my current inplementation:

          #include "MyHeader.h"
          #include "windows.h"

          BOOL DllMain(HINSTANCE , DWORD pReason, LPVOID ) {
          try {
          printf("I'M HERE!\n");
          int *x = NULL;
          *x = 20;

          switch (pReason) {
          case DLL_PROCESS_ATTACH:
          MyProcessAttach();
          MyThreadAttach();
          break;
          case DLL_THREAD_ATTACH:
          MyThreadAttach();
          break;
          case DLL_THREAD_DETACH:
          MyThreadDetach();
          break;
          case DLL_PROCESS_DETACH:
          MyThreadDetach();
          MyProcessDetach();
          break;
          default:
          break;
          }
          }
          catch(std::exception& x) {
          std::cerr << x.what() << std::endl;
          return FALSE;
          }
          return FALSE;
          }

          where all the stuff in bold is just some garbage I've added trying to get it to print something, or crash or generate some failed load situation to show me that the function is getting called. It would certainly seem this function is never getting invoked. What the heck is going on? Again, do I need some project setting to identify this as an entry point function for the library? Matt

          P Offline
          P Offline
          Peter Molnar
          wrote on last edited by
          #4

          I have created a small project which contains 4 lines of code:

          #include <windows.h>
          BOOL DllMain(HINSTANCE , DWORD , LPVOID )
          {
          ::MessageBox(NULL,"test","Caption",MB_OK);
          return TRUE;
          }

          and call it

          HINSTANCE hInstance = LoadLibrary("pathto.dll");

          works just fine. Try the same. What is hInstance when you debug? Peter Molnar

          M 1 Reply Last reply
          0
          • M Matthew Busche

            Peter, Thanks for your confirmation that this at least should work. So why then is my DllMain not getting invoked. Here's my current inplementation:

            #include "MyHeader.h"
            #include "windows.h"

            BOOL DllMain(HINSTANCE , DWORD pReason, LPVOID ) {
            try {
            printf("I'M HERE!\n");
            int *x = NULL;
            *x = 20;

            switch (pReason) {
            case DLL_PROCESS_ATTACH:
            MyProcessAttach();
            MyThreadAttach();
            break;
            case DLL_THREAD_ATTACH:
            MyThreadAttach();
            break;
            case DLL_THREAD_DETACH:
            MyThreadDetach();
            break;
            case DLL_PROCESS_DETACH:
            MyThreadDetach();
            MyProcessDetach();
            break;
            default:
            break;
            }
            }
            catch(std::exception& x) {
            std::cerr << x.what() << std::endl;
            return FALSE;
            }
            return FALSE;
            }

            where all the stuff in bold is just some garbage I've added trying to get it to print something, or crash or generate some failed load situation to show me that the function is getting called. It would certainly seem this function is never getting invoked. What the heck is going on? Again, do I need some project setting to identify this as an entry point function for the library? Matt

            M Offline
            M Offline
            Mike Dimmick
            wrote on last edited by
            #5

            If this is VC6: in the Link tab, select the Output category, and ensure that Entry Point Symbol is either blank or set to _DllMainCRTStartup. You should also check that the /NOENTRY switch is not specified. Both of you: be careful with your dependencies. The only routines you can guarantee will be initialised before your entry point is called are those implemented by KERNEL32.DLL. You should also be able to call C library functions if you're linking with the C run-time.

            1 Reply Last reply
            0
            • P Peter Molnar

              I have created a small project which contains 4 lines of code:

              #include <windows.h>
              BOOL DllMain(HINSTANCE , DWORD , LPVOID )
              {
              ::MessageBox(NULL,"test","Caption",MB_OK);
              return TRUE;
              }

              and call it

              HINSTANCE hInstance = LoadLibrary("pathto.dll");

              works just fine. Try the same. What is hInstance when you debug? Peter Molnar

              M Offline
              M Offline
              Matthew Busche
              wrote on last edited by
              #6

              Much thanks to Peter Molnar for mailing me a completely trivial DevStudio project containing a DllMain() that got properly invoked. What I was missing (and what I had never seen documented) was the APIENTRY declaration for the DLL entry point function. The DllMain must be declared like this:

              BOOL APIENTRY DllMain(HINSTANCE , DWORD , LPVOID )

              Once those 8 pesky characters were added, it worked great. Also, for those who care, this works independently of the application type (i.e., works for both console apps and windows apps) and (as Peter said in an earlier message on this thread) works independently of how the library is loaded. (I.e., you can use either load-time dynamic linking or run-time dynamic linking.) Matt

              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