Announcement

Collapse
No announcement yet.

Newbie - User interfaces with DLL

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

  • Newbie - User interfaces with DLL

    Sorry about this dumb question. I'm currently porting a large PDS app and need to 'chop' it into sections and put some code into a DLL. The question is, can DLL's have user interface commands, such as INKEY$, INPUT, COLOR and so on


    TIA

    -------------
    Nigel Thomas

  • #2
    Nigel --

    That's not a dumb question!

    The simple answer is No, because the PB/DLL compiler does not support those console functions.

    The more advanced answer is Yes. Picture this...

    1) Create a function in your PB/CC program that "encapsulates" the functions that you want to use in the DLL. For example, create a PB/CC function called MyInstat that is simply a wrapper for the INSTAT function.

    Code:
    FUNCTION MyInstat AS LONG
        FUNCTION = INSTAT
    END FUNCTION
    2) Create a function in the DLL like this...

    Code:
    FUNCTION SetInstat(BYVAL dwCodePtr AS DWORD) EXPORT AS LONG
        GLOBAL dwInstatPtr AS DWORD
        dwInstatPtr = dwCodePtr
    END FUNCTION
    3) In your main program, do this...

    Code:
    SetInstat CODEPTR(MyInstat)
    This gives the DLL the memory location of the MyInstat function in your main program, so that it can perform a "callback" to that function.

    4) In the DLL, when you need to use INSTAT, do this...

    Code:
    GLOBAL dwInstatPtr AS DWORD
    CALL DWORD dwInstatPtr TO lResult
    Those are only the basic steps, to give you the idea. You'll need to become familiar with CODEPTR and CALL DWORD. But once you do, callbacks can be very powerful tools!

    -- Eric

    "Not my circus, not my monkeys."

    Comment


    • #3
      Actually, the easiest way it so encapsulate the PB/CC specific statements in your main application and export them from your .EXE so that you can call them from your .DLL.
      Code:
      FUNCTION MyInkey() EXPORT AS STRING
        FUNCTION = INKEY$
      END FUNCTION
      Then import it from your DLL as:
      Code:
      DECLARE FUNCTION Inkey LIB "MYPROG.EXE" ALIAS "MYINKEY" () AS STRING
      You can do the same with Print, Color, Locate, etc. Just remember that some keywords like PRINT are reserved in PB/DLL.

      --Dave

      -------------
      PowerBASIC Support
      mailto:[email protected][email protected]</A>

      Home of the BASIC Gurus
      www.basicguru.com

      Comment


      • #4
        I tried to write PB/DLL program that calls WAITKEY$ function in PB/CC program but when I tried to run the program, it locked up my computer and I had to reset my computer.

        Code:
        WAITKEY.BAS code:
        function mywaitkey() export as string
           function=waitkey$
        end function
        function pbmain() as long
        end function
        
        TEST.BAS code:
        #compile exe
        #include "ddt.inc"
        declare function mywaitkey lib "WAITKEY.EXE" alias "MYWAITKEY" () as string
        function pbmain as long
          local hdlg as long
          local char01 as string
          dialog new 0,"Test",0,0,100,100,%ds_center or %ws_sysmenu to hdlg
          control add textbox,hdlg,100,"",10,10,50,10
          dialog show modeless hdlg
          char01=mywaitkey
          if char01="1" then control set text hdlg,100,"Key [1] Pressed"
          if char01="2" then control set text hdlg,100,"Key [2] Pressed"
        end function
        Jeffrey.

        ------------------

        Comment


        • #5
          You cannot call a function in an EXE *FROM* an EXE, only from a DLL. Therefore you need to make the 'main' program with PB/CC and place the 'auxillary' PB/DLL code in a DLL.



          ------------------
          Lance
          PowerBASIC Support
          mailto:[email protected][email protected]</A>
          Lance
          mailto:[email protected]

          Comment


          • #6
            Jeffrey --
            You should do approx. so
            Code:
            PB/CC Tmp4.Bas
            
            #Compile Exe
            #Register None
            #Include "Win32Api.Inc"
            Global hWndConsole As Long
            Declare Sub Pb Lib "Tmp3.Dll" Alias "PB" (hWnd As Long)
            Function Win01 (hWnd As Long) Export As String
              Dim st As String, w As String
              Cls
              Print "Type something and press Enter"
              Do
                 w$ = InKey$
                 If w$ = Chr$(13) Then Exit Loop
                 If Len(w$) <> 0 Then Print w$;: st$ = st$ + w$ Else Sleep 0
              Loop
              ShowWindow hWndConsole, 0
              ShowWindow hWnd, 1
              SetForegroundWindow hWnd
              Function = st$
            End Function
            
            Function WinMain (ByVal hCurInstance  As Long, _
                                ByVal hPrevInstance As Long, _
                                lpszCmdLine  As Asciiz Ptr, _
                                ByVal nCmdShow  As Long) Export As Long
              hWndConsole = GetForegroundWindow()
              ShowWindow hWndConsole, 0
              Pb hWndConsole
              
            End Function
            
            Pb/Dll  Tmp3.Bas
            
            #Compile Dll
            #Register None
            #Dim All
            #Include "win32Api.inc"
            Global hWndMain As Long
            Declare Function mywaitkey Lib "Tmp4.Exe" Alias "WIN01" (hWnd As Long) As String
            CallBack Function Cb
               Select Case CbMsg
                  Case %WM_COMMAND
                     If CbCtl = 101 Then
                        ShowWindow CbHndl, 0
                        ShowWindow hWndMain, 1
                        SetForegroundWindow hWndMain
                        Control Set Text CbHndl, 100, mywaitkey(CbHndl)
                     End If
               End Select
            End Function
                    
            Sub PB (hWndConsole As Long) Export
              hWndMain = hWndConsole
              Local hdlg As Long
              Local char01 As String
              Dialog New 0,"Test",,,100,100, %ws_sysmenu, %WS_EX_TOPMOST To hdlg
              Control Add Label, hdlg,100, "", 10, 10, 80, 12, %WS_BORDER
              Control Add Button,  hdlg,101, "Get Text", 10, 30, 80, 15
              Dialog Show Modal hdlg Call Cb
            End Sub
            This is very dirty code. In real program it's necessary to use more reliable functions than GetForegroundWindow and SetForegroundWindow.
            For example, to find console window hwnd it's better to give "unique" caption (by timer) and to use FindWindow.
            Instead of SetForegroundWindow - NOT DOCUMENTED SwitchToThisWindow (see Source code forum)

            ------------------


            [This message has been edited by Semen Matusovski (edited April 02, 2000).]

            Comment


            • #7
              Semen, How do I write a PB/DLL program that calls the TMP3.DLL file that calls the TMP4.EXE file? I tried this code but it didn't work:

              <font face="Courier New, Courier" size="3"><pre>
              #compile exe
              #register none
              #dim all
              #include "win32api.inc"
              declare function pb lib "TMP3.DLL" alias "PB" (hwndconsole as long) as long
              function pbmain() as long
              local hdlg as long
              local hwndconsole as long
              dialog new 0,"Test",100,0,0,200,100,%ws_sysmenu or %ds_center to hdlg
              control add textbox,hdlg,100,"Stillwell Avenue",10,10,180,20
              dialog show modeless hdlg
              pb(hwndconsole)
              control set text hdlg,100,"86th Street"
              pb(hwndconsole)
              control set text hdlg,100,"Avenue U"
              pb(hwndconsole)
              control set text hdlg,100,"Kings Highway"
              pb(hwndconsole)
              end function
              </pre></font>

              Jeffrey.

              ------------------

              Semen, Forget this message. I am surprised that a console program calls a WIN32 DLL and displays the dialog box. When I click on the button, the console window appears and I type some text. When I press the [ENTER] key, the console window disappears and text appears on the label. Jeffrey.


              [This message has been edited by Jeffrey Morris (edited April 02, 2000).]

              Comment

              Working...
              X