Announcement

Collapse
No announcement yet.

Need some kinda of guidelines to convert an exe program to a DLL

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

  • Need some kinda of guidelines to convert an exe program to a DLL

    Hi All,

    Being a beginner in PB, I w'd like to learn how to convert an exe program to a DLL.
    Hopefully some one here can give me some kinda of guidelines in this issue. something like DO and DON"T

    I had made created some exe programs in PB but only a handful of DLL programs, but now I have been tasked to
    convert some old PB exe programs to DLLs. Appreciate any help

  • #2
    See Help:
    What is a DLL?
    and
    Creating a Dynamic Link Library

    Comment


    • #3
      Tim,

      The main difference is in the startup code, where an EXE has an entry point "PBmain", a DLL has a DLLmain where a number of parameters for the DLL are set. You have to EXPORT the function that starts the code in the DLL. PB can simplify the technique but its worth the effort to create A DLL Main so you understand how and why they work. There are a few tricks to learn, the instance handle of a DLL is different to that in an EXE and you must make sure you don't get name clashes of functions and variables between the EXE and DLL but once you are familiar with this stuff, its easy enough to do.
      hutch at movsd dot com
      The MASM Forum

      www.masm32.com

      Comment


      • #4
        Here is a DLL skeleton that should make sense.
        Code:
        ' ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
        '                                       Compile with pbwin10
        ' ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
        
            #COMPILE DLL
        
            GLOBAL DLLinstance as LONG      ' the DLLs instance handle
        
          ' -----------------------------------
          ' set correct paths for include files
          ' -----------------------------------
            #INCLUDE "\basic\include\win32api.inc"
            #INCLUDE "\basic\include\richedit.inc"
        
        ' ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
        
        FUNCTION LibMain(BYVAL hInst    as LONG, _
                         BYVAL Reason   as LONG, _
                         BYVAL Reserved as LONG) as LONG
        
            LOCAL RetVal as LONG
        
            Select Case Reason
              Case %DLL_PROCESS_ATTACH
                DLLinstance = hInst     ' make DLL instance global
                RetVal = 1              ' needed so DLL will start
        
              ' -------------------
              ' uncomment if needed
              ' -------------------
              ' Case %DLL_PROCESS_DETACH
              
              ' Case %DLL_THREAD_ATTACH
              
              ' Case %DLL_THREAD_DETACH
              
            End Select
        
            FUNCTION = RetVal
        
        END FUNCTION
        
        ' ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
        
        FUNCTION YourProc ALIAS "YourProc"() EXPORT as LONG
        
        
          ; -----------------------------------------
          ; this is where you call your app code from
          ; -----------------------------------------
        
        
            FUNCTION = 0
        
        END FUNCTION
        
        ' ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
        hutch at movsd dot com
        The MASM Forum

        www.masm32.com

        Comment


        • #5
          See declare in help

          Code:
          'save as d1.bas and compile
          
          #COMPILE DLL "d1.dll"
          
          SUB oNE EXPORT
           ? "hello, world!"
          END SUB
          Code:
          'save as test.bas and run
          
          #COMPILE EXE
          
          DECLARE SUB one LIB "d1.dll"
          
          FUNCTION PBMAIN AS LONG
           one
          END FUNCTION
          How long is an idea? Write it down.

          Comment


          • #6
            had made created some exe programs in PB but only a handful of DLL programs, but now I have been tasked to
            convert some old PB exe programs to DLLs
            Typically you would not convert programs to DLLs, you convert procedures (SUBs and FUNCTIONs).

            So first ask yourself if the program can be reduced to a collection of functions... and you still have the issue of "how will I execute this?" After all, a DLL contains no "starting point" and are executed one procedure at a time.
            Michael Mattias
            Tal Systems Inc. (retired)
            Racine WI USA
            [email protected]
            http://www.talsystems.com

            Comment


            • #7
              Originally posted by Michael Mattias View Post

              Typically you would not convert programs to DLLs, you convert procedures (SUBs and FUNCTIONs).

              So first ask yourself if the program can be reduced to a collection of functions... and you still have the issue of "how will I execute this?" After all, a DLL contains no "starting point" and are executed one procedure at a time.
              I generally agree with Michael's point but DLL's do indeed have a starting point that users can use via the DLL_PROCESS_ATTACH message.

              The BCX code snippet below shows the registration of a custom class followed by a MSGBOX.
              In this example, the calling program can not display its main window until the MsgBox is dismissed.


              FUNCTION DllMain (hInst AS HINSTANCE,Reason AS DWORD,Reserved AS LPVOID) EXPORT
              SELECT CASE Reason
              '**************************************************************
              CASE DLL_PROCESS_ATTACH
              ghInstance = hInst ' < -- Do our initializations here
              Register_AquaBtnClass (hInst) ' < -- Do our initializations here
              MSGBOX "Aqua Button by Kevin Diggins has been loaded"
              FUNCTION = TRUE
              '**************************************************************

              the rest snipped for brevity ...

              Comment


              • #8
                Thank you Everyone, this is an EXCELLENT tutorial on DLL !

                Comment


                • #9

                  The BCX code snippet below shows the registration of a custom class followed by a MSGBOX.
                  In this example, the calling program can not display its main window until the MsgBox is dismissed.
                  The DLL_PROCESS_ATTACH message on which you show calling MSGBOX has some limitations:

                  ....
                  Warning The entry-point function should perform only simple initialization 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.

                  It is safe to call other functions in Kernel32.dll, because this DLL is guaranteed to be loaded in the process address space when the entry-point function is called. It is common for the entry-point function to create synchronization objects such as critical sections and mutexes, and use TLS. Do not call the registry functions, because they are located in Advapi32.dll. If you are dynamically linking with the C run-time library, do not call malloc; instead, call .

                  Calling imported functions other than those located in 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 in their DLLs call LoadLibrary to load other system components.
                  Source: MSDN WinAPI reference on disk

                  Note that MSGBOX calls functions outside of Kernel32.dll and therefore should not be called on this notification.

                  MCM

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

                  Comment


                  • #10
                    Deleted ...
                    Last edited by Mr. Kevin Diggins; 19 Oct 2020, 03:27 PM.

                    Comment


                    • #11
                      (no longer needed, see next)
                      Last edited by Dale Yarker; 19 Oct 2020, 04:52 PM.
                      Dale

                      Comment


                      • #12
                        Originally posted by Dale Yarker View Post
                        How is what C or C++ do relevant?

                        From PowerBASIC Help - "In addition, code within LIBMAIN must not call API functions in any other DLL (for example, USER32.DLL, SHELL32.DLL, ADVAPU32.DLL, GDI32.DLL, etc) . . . "

                        Your comments not helpful to PB users.
                        Dale,

                        I agree ... (I deleted my earlier post)

                        Similar idea compiles successfully in PBWin10 with JR's includes but freezes up when run from a PB compiled Test.exe

                        The same PB compiled DLL runs and terminates correctly when loaded and run from a non-PB Test.exe


                        Code:
                        $COMPILE DLL
                        $INCLUDE "WIN32API.INC"
                        
                        FUNCTION LIBMAIN ALIAS "LibMain" (BYVAL hInstance AS LONG, _
                        BYVAL fwdReason AS LONG, BYVAL Reserved AS LONG) EXPORT AS LONG
                        SELECT CASE fwdReason
                        
                        CASE %DLL_PROCESS_ATTACH
                        MSGBOX "Look! The world is still spinning!"
                        
                        FUNCTION = 1
                        EXIT FUNCTION
                        
                        CASE %DLL_PROCESS_DETACH
                        EXIT FUNCTION
                        
                        CASE %DLL_THREAD_ATTACH
                        EXIT FUNCTION
                        
                        CASE %DLL_THREAD_DETACH
                        EXIT FUNCTION
                        END SELECT
                        END FUNCTION

                        Comment

                        Working...
                        X