doing things when dll's are loaded...

This is a discussion on doing things when dll's are loaded... within the C++ Programming forums, part of the General Programming Boards category; i want to do some initializing when a dll is loaded.... this is my dll of course, and what i ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    9

    doing things when dll's are loaded...

    i want to do some initializing when a dll is loaded.... this is my dll of course, and what i want to do is actually initialize winsock...
    ok...
    this is my code for DllMain:
    Code:
    BOOL APIENTRY DllMain (HINSTANCE hInst     /* Library instance handle. */ ,
                           DWORD reason        /* Reason this function is being called. */ ,
                           LPVOID reserved     /* Not used. */ )
    {
        switch (reason)
        {
          case DLL_PROCESS_ATTACH:
            break;
    
          case DLL_PROCESS_DETACH:
            break;
    
          case DLL_THREAD_ATTACH:
            break;
    
          case DLL_THREAD_DETACH:
            break;
        }
    
        /* Returns TRUE on success, FALSE on failure */
        return TRUE;
    }
    where exactly should i put it? (keep in mind i also want to denitialize and cleanup winsock when the dll is unloaded...)

    thanks in advance!
    seec77

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,680
    Code:
    case DLL_PROCESS_ATTACH:
        WSAStartup(wVersion, &WSAData);
        break;
    ...
    case DLL_PROCESS_DETACH:
        WSACleanup();
        break;
    gg

  3. #3
    Nor
    Nor is offline
    h ֆhr s Nor's Avatar
    Join Date
    Nov 2001
    Posts
    299
    you using a dll?
    export export export

    export some OnLoad() OnExit() functions.
    when you load the dll call the OnLoad(). place your initializing code there.
    then your ready to go
    Try to help all less knowledgeable than yourself, within
    the limits provided by time, complexity and tolerance.
    - Nor

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    9
    Originally posted by Nor
    you using a dll?
    export export export

    export some OnLoad() OnExit() functions.
    when you load the dll call the OnLoad(). place your initializing code there.
    then your ready to go
    i'd rather go with Codeplug's suggestion....
    thank you both!!!


    p.s.
    what is "gg"?!?!

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,680
    gg - my initials

  6. #6
    Registered User
    Join Date
    Jun 2003
    Posts
    9
    weird...
    it didn't work...
    i put the initialization code inside the process_attach, and when i ran the program, it went kaboom
    then, when i put the initialization code inside the first function that was called, it worked... and i compiled it in the same way btw!

  7. #7
    Registered User
    Join Date
    Jun 2003
    Posts
    9
    ok... this is really weird....
    i just understood that my DllMain function isn't even called!
    first of all i tried putting the winsock initialization code in the body of the function and it still didn't work.... then i commented the whole function out, and it gave me no error!
    Code:
    BOOL APIENTRY DllMain (HINSTANCE hInst     /* Library instance handle. */ ,
                           DWORD reason        /* Reason this function is being called. */ ,
                           LPVOID reserved     /* Not used. */ )
    {
    ...
    }
    anything wrong with this code? this is really weird!
    thanks in advance!

  8. #8
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    The range of functions that can be called in DllMain is extremely limited.
    MSDN Documentation for DllMain
    Warning The entry-point function should perform only simple initialization or termination tasks. It must not call the LoadLibrary or LoadLibraryEx function (or a function that calls these functions), because this may create dependency loops in the DLL load order. This can result in a DLL being used before the system has executed its initialization code. Similarly, the entry-point function must not call the FreeLibrary function (or a function that calls FreeLibrary), because this can result in a DLL being used after the system has executed its termination code.
    I think it is safe to say that WSAStartup calls LoadLibrary or its equivalent.

    If you do not provide a DllMain the compiler provides a default version. Try putting a MessageBox in it if you are worried that it is not being called.

  9. #9
    Registered User
    Join Date
    Jun 2003
    Posts
    9
    damn...
    then WSAStartup uses LoadLibrary?
    damn...
    so how can i go about doing this???
    btw, i do think DllMain is only called if you use the LoadLibrary function to open it, and not just do a __declspec (dllimport)
    and are you sure it's not allowed at all, or is it just not recommended (using LoadLibrary or FreeLibrary in DllMain)
    thanks so far!

  10. #10
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    so how can i go about doing this???

    As Nor suggested. Use descriptive names like MyLibraryStartup and MyLibraryCleanup.

    btw, i do think DllMain is only called if you use the LoadLibrary function to open it, and not just do a __declspec (dllimport)

    I think it is always called.

    and are you sure it's not allowed at all, or is it just not recommended (using LoadLibrary or FreeLibrary in DllMain)

    Your process going kaboom suggests that it is not allowed. Even if you ignore the documentation and get something working it may break on other versions of Windows.

  11. #11
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,680
    The code I posted is from source code that's running "in the field" (on NT, 2000, XP, CE 2.11).
    However, it's since been changed to not make those calls in DllMain() for other reasons.

    You'll know if it works once DllMain() is being called (duh).
    What compiler you using? And how are you compiling/linking?

    gg

  12. #12
    Registered User
    Join Date
    Jun 2003
    Posts
    9
    first of all....
    please excuse me for being so stupid....
    secondly, im using dev-cpp, and im compiling... ummm.... normally....
    i make the output dll and object files to another directory where my "client" project is... then, the client project uses LoadLibrary and GetProcAddress to call the functions off the dll....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Suggestions for things to study
    By Mastadex in forum Windows Programming
    Replies: 5
    Last Post: 08-18-2008, 09:23 AM
  2. accessing a DLL's resources...
    By dug in forum Windows Programming
    Replies: 9
    Last Post: 12-13-2005, 04:04 AM
  3. Can't load string from resource DLL's string table
    By s_k in forum Windows Programming
    Replies: 4
    Last Post: 07-15-2003, 06:43 AM
  4. question about DLL's and class functions
    By btq in forum Windows Programming
    Replies: 2
    Last Post: 02-25-2003, 05:08 AM
  5. C++ Compiler Problem
    By Prog.Patterson in forum C++ Programming
    Replies: 0
    Last Post: 05-02-2002, 10:17 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21