Announcement

Collapse
No announcement yet.

DLL Entry on Startup - How Many Times?

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • DLL Entry on Startup - How Many Times?

    I thought the function PBLibMain would be entered once when the DLL is loaded, then again when the calling app closes. Instead, I see 3 entries on startup. Why is that?

    The DLL ...
    Code:
    #Compile DLL "testb.dll"
    Function PBLibMain
       ? "here"                   '<------- shows up 3 times 
       Function = 1
    End Function
    The EXE ...
    Code:
    #Compile Exe "testb.exe"
    #Include "win32api.inc"
    Function PBMain
       LoadLibrary "testb.dll"
    End Function

  • #2
    Ok, perhaps this is it.

    1. load
    2. main exe thread starts
    3. unload ... because main exe thread ends.



    In Help is this ... where I got the idea that the main thread of the EXE is something Help refers to.

    In 32-bit Windows, PBLIBMAIN is called each time a DLL is loaded or unloaded by an application or process, and (usually) if a thread is started and stopped.

    Comment


    • #3
      I notice that Help for LIBMAIN does not say the same thing about when the function is called - i.e., no mention of threads in the Help description.

      LIBMAIN (or its synonym DLLMAIN) is an optional user-defined function called by Windows each time a DLL is loaded into, and unloaded from, memory.

      Comment


      • #4
        In this DLL test, LIBMAIN is only accessed twice - on load and unload.

        I wonder why the difference between PBLIBMAIN and LIBMAIN.

        The DLL ...
        Code:
        #Compile DLL "testc.dll"
        #Include "win32api.inc"
        
        Function LibMain (ByVal hInstance As Long, ByVal fwdReason As Long, ByVal lpvReserved As Long) As Long
          Select Case fwdReason
            Case %DLL_PROCESS_ATTACH
               ? "load"
            Case %DLL_PROCESS_DETACH
               ? "unload"
            Case %DLL_THREAD_ATTACH
               ? "thread in"
            Case %DLL_THREAD_DETACH
               ? "thread out"
          End Select
        End Function
        The EXE ...
        Code:
        #Compile Exe "testC.exe"
        #Include "win32api.inc"
        
        Function PBMain
           LoadLibrary "testC.dll"
        End Function

        Comment


        • #5
          Some time before Bob passed away I remember the comment that PBWIN has to construct a lower level LIBMAIN then interface it to an alias high level LIBMAIN so that string and other basic specific capacities can be initialised properly. This seems to be handled at the linking stage which you don't see in PB as the compiler does all of the operations from compiling code, building the resources and linking it all together. I tend to write LIBMAIN functions but where you don't need to perform initialisation, you don't have to have a LIBMAIN.
          hutch at movsd dot com
          The MASM Forum

          www.masm32.com

          Comment


          • #6
            Hi Steve!
            My questions got started when this code failed. I thought it was an interesting/minimal way to create/kill a timer in a DLL, that I'd read somewhere in a ~2002 forum thread.

            Code:
            #Compile DLL "test.dll"
             Function PBLibMain    
               Static hTimer as Long    
               If hTimer = 0 Then hTimer = SetTimer(0,%ID_Timer, 100, %Null) Else KillTimer 0, %ID_TImer  
              Function = 1
            End Function

            But because PBLIBMAIN was called twice on loading, the timer was never available. It was only after I read Help more carefully that the answer seemed there.

            Comment


            • #7
              You generally set up things like globals, instance handles and the like but a timer is an OS based thread that is different from a process or a thread calling a DLL. A DLL can be configured in many ways depending on how you load it, it can be loaded statically for the duration of the app or it can be done dynamically which you load on call and shut down when you are finished with it.

              If I understand what you are doing correctly, you would set up any persistent values in the LIBMAIN and alter them when you call a function in the DLL.
              hutch at movsd dot com
              The MASM Forum

              www.masm32.com

              Comment

              Working...
              X