No announcement yet.

MsgBox in LIBMAIN - no fail?

  • Filter
  • Time
  • Show
Clear All
new posts

  • MsgBox in LIBMAIN - no fail?

    LIBMAIN Help says this:

    For example, never call the MessageBox API function from within LIBMAIN, nor use the related MSGBOX function or MSGBOX statement.

    Failure to observe these restrictions will result in Access Violation or General Protection Faults (GPFs), typically caused by the execution of code in DLLs that has yet to be initialized.
    But when I did that in this DLL, there was no failure. Any idea why? Perhaps because there was no other code in the DLL?

    #Compile DLL "testc.dll"
    #Include ""
    Function LibMain (ByVal hInstance As Long, ByVal fwdReason As Long, ByVal lpvReserved As Long) As Long
      Select Case fwdReason
           ? "load"
           ? "unload"
      End Select
    End Function

  • #2
    Luck. MessageBox is in User32.dll not Kernel32.dll. Only Kernel32.dll is GUARANTEED to be loaded.


    • #3
      It all depends upon which OS DLL's the app itself calls. The rule of thumb with DLL's is to not assume that more than the bear minimum OS DLL's have already been loaded into memory. This is why one needs to be careful of the API's called in Libmain itself. Once the DLL is loaded and Libmain is done, then you can call whatever OS DLL's you want (or third party DLL's).

      Powerbasic calls the routine LibMain, but in C (Windows docs) the name is DLLMain. When you look up DLLMain in the SDK docs, it says this:

      Warning There are serious limits on what you can do in a DLL entry point. To provide more complex initialization, create an initialization routine for the DLL. You can require applications to call the initialization routine before calling any other routines in the DLL.
      Because Kernel32.dll is guaranteed to be loaded in the process address space when the entry-point function is called, calling functions in Kernel32.dll does not result in the DLL being used before its initialization code has been executed. Therefore, the entry-point function can call functions in Kernel32.dll that do not load other DLLs. For example, DllMain can create synchronization objects such as critical sections and mutexes, and use TLS. Unfortunately, there is not a comprehensive list of safe functions in Kernel32.dll.
      Calling functions that require DLLs other than Kernel32.dll may result in problems that are difficult to diagnose. For example, calling User, Shell, and COM functions can cause access violation errors, because some functions load other system components. Conversely, calling functions such as these during termination can cause access violation errors because the corresponding component may already have been unloaded or uninitialized.
      Chris Boss
      Computer Workshop
      Developer of "EZGUI"