Announcement

Collapse
No announcement yet.

LibMain vs PBLibMain in a DLL

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

  • LibMain vs PBLibMain in a DLL

    I'm looking at some DLL code and have a question.

    I can use PBLibMain or LibMain in a DLL, each having code that will run when the DLL loads and unloads (compilable examples below).

    I can see in general that LibMain provides some additional information, but not having written many DLLs, it is not obvious that PBLibMain is to be avoided. On the other hand, I see very little use of PBLibMain in the forums and none at all in the distributed samples, so it appears to be avoided somewhat.

    Other than the additional information provided by LibMain, Is there any particular reason to avoid using PBLibMain?


    PBLibMain:
    Code:
    #Compile DLL
    Function PBLibMain
       Static i as Long
       If i Then 
          MsgBox "UnLoading ..."
       Else
          MsgBox "Loading ..."
       End If
       Incr i
       Function = 1
    End Function
    Function FlipCoin ALIAS "FlipCoin" (uLimit&) EXPORT As Long
       Function = Rnd(0,uLimit&)
    End Function
    LibMain
    Code:
    #Compile DLL
    #Include "Win32API.inc"
     Function LibMain (ByVal hInstance&, ByVal fwdReason&, ByVal lpvReserved&) As Long
       Select Case fwdReason&
          Case %DLL_PROCESS_ATTACH  
             MsgBox "loading ..."
             Function = 1  
          Case %DLL_PROCESS_DETACH
             MsgBox "unloading ..."
             Function = 1  
       End Select
    End Function
    Function FlipCoin ALIAS "FlipCoin" (uLimit&) EXPORT As Long
       Function = Rnd(0,uLimit&)
    End Function
    Executable code (works on both)
    Code:
    'Compilable Example:
    #Compile Exe
    #Dim All
    Declare Function FlipCoin Lib "gbsnippets_temp.dll" Alias "FlipCoin" (uLimit As Long) As Long
    Global hDlg As Dword
    Function PBMain() As Long
       Dialog New Pixels, 0, "Test Code",300,300,200,200, %WS_OverlappedWindow To hDlg
       Control Add Button, hDlg, 100,"Push", 50,10,100,20
       Control Add Label, hDlg, 110,"<result>", 50,40,100,20
       Dialog Show Modal hDlg Call DlgProc
    End Function
    CallBack Function DlgProc() As Long
       If Cb.Msg = %WM_Command And Cb.Ctl = 100 And Cb.CtlMsg = %BN_Clicked Then
          Control Set Text hDlg, 110, Str$(FlipCoin(10))      'here, the function FlipCoin() returns a value of 0-10
       End If
    End Function

  • #2
    [deleted]
    Eddy

    Comment


    • #3
      I never knew there was a "PBLIBMAIN"

      But after reviewing the documentation in the help file, I think I'd avoid it if only because without any parameters, you don't know why it's being called, which means you can't possibly know what to do when it is.

      Stick with "parameters included at no extra charge" versions of a DLL's Entry Point Function.

      MCM
      Michael Mattias
      Tal Systems (retired)
      Port Washington WI USA
      [email protected]talsystems.com
      http://www.talsystems.com

      Comment


      • #4
        Actually you don't need either, when compiling a DLL, if LIBMAIN is not found in the code then the compiler inserts the neccessary code for you. Using PBLIBMAIN (or none) if a DLL is normally loaded implicitly can be good practice and then have any initialising needed done with a specific exported function.
        If you do any initialisation in the Attach section that needs another DLL (even some windows functions) then that other DLL is not gauranteed to be loaded which can cause rare strange errors. If the initialisation is done with seperate function which the loading program later calls then all implicitly loaded DLL's will have been loaded

        Comment

        Working...
        X