Announcement

Collapse
No announcement yet.

Why use Call Dword for Internal Procedures?

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

  • Why use Call Dword for Internal Procedures?

    Help indicates that "Call Dword" can be used for internal procedures, rather than for external DLL procedures.

    Can someone discuss why you'd need to do that?

  • #2
    For callback situations.
    I sometimes write 'standard' code where a callback can be used for custom things.

    For example, UrlDownloadToFileEx() can be used with a callback, a pointer is needed.
    To extend on that... if the API callback doesn't suit you, you can still call a callback which suits you better.

    Anyway, a real functionname is not always present + you may have different callback behaviour.
    hellobasic

    Comment


    • #3
      Memory-Mapped File Version of LINE INPUT

      PB/WIN: Write your own ENUM functions to callback September 23, 2002
      Michael Mattias
      Tal Systems Inc. (retired)
      Racine WI USA
      [email protected]
      http://www.talsystems.com

      Comment


      • #4
        Another important use involves testing for something, then calling the appropriate function. Something like:
        Code:
        if has(HardwareX) then
           pFunction = codeptr(FunctionX)
        else
           pFunction = codeptr(FunctionY)
        end if
        
        for i = 0 to maxx
           call dword pFunction using FunctionXYdeclaration(Params(i))
        next i
        Moving the test out of the loop is straight from Programming 101.

        Comment


        • #5
          I've used CALL DWORD to do a jump table. Kind of like an ON x GOSUB statement.
          Jeff Blakeney

          Comment


          • #6
            Call Dword

            Call Dword comes in handy in all sorts of places. I guess its the newer counterpart to the old Call Absolute in older DOS basics.

            I use it in all my programs (as Edwin indicated) as part of my message cracker routines like so (see fnWndProc() near bottom)...

            Code:
            #Compile       Exe
            #Include       "Win32api.inc"
            %IDC_BUTTON1   =2000
            %IDC_BUTTON2   =2002
            
            Type WndEventArgs
              wParam As Long
              lParam As Long
              hWnd   As Dword
              hInst  As Dword
            End Type
            
            Declare Function FnPtr(wea As WndEventArgs) As Long
            
            Type MessageHandler
              wMessage As Long
              dwFnPtr As Dword
            End Type
            Global MsgHdlr() As MessageHandler
            
            
            Sub Command1_OnClick(Wea As WndEventArgs)
              MsgBox("Thanks!  I Needed That!")
            End Sub
            
            
            Sub Command2_OnClick(Wea As WndEventArgs)
              MsgBox("I Don't Care To Be Clicked!")
            End Sub
            
            
            Function fnWndProc_OnCreate(wea As WndEventArgs) As Long
              Local pCreateStruct As CREATESTRUCT Ptr
              Local hBtn As Dword
            
              pCreateStruct=wea.lParam
              [email protected]
              hBtn=CreateWindow("button","Click Me!",%WS_CHILD Or %WS_VISIBLE,100,20,120,25,wea.hWnd,%IDC_BUTTON1,wea.hInst,Byval 0)
              hBtn=CreateWindow("button","But Not Me!",%WS_CHILD Or %WS_VISIBLE,100,60,120,25,wea.hWnd,%IDC_BUTTON2,wea.hInst,Byval 0)
            
              fnWndProc_OnCreate=0
            End Function
            
            
            Function fnWndProc_OnCommand(wea As WndEventArgs) As Long
              Select Case As Long Lowrd(wea.wParam)
                Case %IDC_BUTTON1
                  Call Command1_OnClick(wea)
                Case %IDC_BUTTON2
                  Call Command2_OnClick(wea)
              End Select
            
              fnWndProc_OnCommand=0
            End Function
            
            
            Function fnWndProc_OnClose(wea As WndEventArgs) As Long
              Call PostQuitMessage(0)
              fnWndProc_OnClose=0
            End Function
            
            
            Function fnWndProc(ByVal hWnd As Long,ByVal wMsg As Long,ByVal wParam As Long,ByVal lParam As Long) As Long
              Local wea As WndEventArgs
              Register iReturn As Long
              Register i As Long
            
              For i=0 To 2
                If wMsg=MsgHdlr(i).wMessage Then
                   wea.hWnd=hWnd: wea.wParam=wParam: wea.lParam=lParam
                   Call Dword MsgHdlr(i).dwFnPtr Using FnPtr(wea) To iReturn
                   fnWndProc=iReturn
                   Exit Function
                End If
              Next i
            
              fnWndProc=DefWindowProc(hWnd,wMsg,wParam,lParam)
            End Function
            
            
            Sub AttachMessageHandlers()
              ReDim MsgHdlr(2) As MessageHandler   'Associate Windows Message With Message Handlers
              MsgHdlr(0).wMessage=%WM_CREATE   :   MsgHdlr(0).dwFnPtr=CodePtr(fnWndProc_OnCreate)
              MsgHdlr(1).wMessage=%WM_COMMAND  :   MsgHdlr(1).dwFnPtr=CodePtr(fnWndProc_OnCommand)
              MsgHdlr(2).wMessage=%WM_CLOSE    :   MsgHdlr(2).dwFnPtr=CodePtr(fnWndProc_OnClose)
            End Sub
            
            
            Function WinMain(ByVal hIns As Long, ByVal hPrevIns As Long, ByVal lpCmdLn As Asciiz Ptr, ByVal iShowWnd As Long) As Long
              Local szAppName As Asciiz*16
              Local wc As WndClassEx
              Local hWnd As Dword
              Local Msg As tagMsg
            
              Call AttachMessageHandlers()                       : szAppName="Form2"
              wc.lpszClassName=VarPtr(szAppName)                 : wc.lpfnWndProc=CodePtr(fnWndProc)
              wc.cbSize=SizeOf(wc)                               : wc.style=%CS_HREDRAW Or %CS_VREDRAW
              wc.cbClsExtra=0                                    : wc.cbWndExtra=0
              wc.hInstance=hIns                                  : wc.hIcon=LoadIcon(%NULL, ByVal %IDI_APPLICATION)
              wc.hCursor=LoadCursor(%NULL, ByVal %IDC_ARROW)     : wc.hbrBackground=%COLOR_BTNFACE+1
              wc.lpszMenuName=%NULL
              Call RegisterClassEx(wc)
              hWnd=CreateWindowEx(0,szAppName,"Form2",%WS_OVERLAPPEDWINDOW Xor %WS_MAXIMIZEBOX,400,300,325,150,0,0,hIns,ByVal 0)
              Call ShowWindow(hWnd,iShowWnd)
              While GetMessage(Msg,%NULL,0,0)
                TranslateMessage Msg
                DispatchMessage Msg
              Wend
            
              Function=msg.wParam
            End Function
            Fred
            "fharris"+Chr$(64)+"evenlink"+Chr$(46)+"com"

            Comment


            • #7
              This is how I used it a few years ago. Note this was all auto generated from a dialog resource file not hand coded.
              James
              Code:
              '==============================================================================
              TYPE DLG_10_InfoType
                MaxDlgMsgs           AS LONG
                DlgMsgs              AS DWORD PTR
                DlgProcs             AS DWORD PTR
                MaxCtlMsgs           AS LONG
                CtlMsgs              AS DWORD PTR
                CtlProcs             AS DWORD PTR
                MaxNotifyMsgs        AS LONG
                NotifyCtlIds         AS DWORD PTR
                NotifyProcs          AS DWORD PTR
                ExitFlag             AS LONG
                MaxControls          AS LONG
                CtlInfo              AS ControlInfoType PTR
              END TYPE
              '==============================================================================
              
              FUNCTION _
                Create_DLG_10 ( _
                  hWnd    AS DWORD _
                ) AS LONG
              
                DIM lpMem        AS DWORD,_
                    lpDataInfo    AS DLG_10_InfoType PTR, _
                    MsgIndex        AS LONG, _
                    MemOffset        AS DWORD, _
                    RetVal        AS LONG
              
                lpMem = HeapAlloc(ghHeap,%HEAP_NO_SERIALIZE OR %HEAP_ZERO_MEMORY,LEN(DLG_10_InfoType))
                IF ISFALSE lpMem THEN
                  MsgBox "Not Able to Allocate Mem"
                  FUNCTION = 0
                  EXIT FUNCTION
                END IF
                lpDataInfo = lpMem
              
              '---------------------------------------------------------------------------
              '>> Message Crackers
                #INCLUDE "DLG_10.MSG"
              
                RetVal = DialogBoxParam(ghInst,BYVAL 10,hWnd,CODEPTR(Dlg_Event_Proc),lpMem)
              '=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*

              Message Cracker file
              Code:
              DLG_10.MSG file
              '=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
              'DLG_10
              '=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
              @lpDataInfo.MaxDlgMsgs =  9 
              @lpDataInfo.MaxCtlMsgs = 5
              @lpDataInfo.MaxNotifyMsgs =  0 
              '---------------------------------------------------------------------------
              MemOffset = HeapAlloc(ghHeap,%HEAP_NO_SERIALIZE OR %HEAP_ZERO_MEMORY,(@lpDataInfo.MaxDlgMsgs + _
                                             @lpDataInfo.MaxCtlMsgs + _
                                             @lpDataInfo.MaxNotifyMsgs)*8)
              IF ISFALSE MemOffset THEN
                MsgBox "Message Array memory allocation error"
                FUNCTION = 0
                EXIT FUNCTION
              END IF
              '---------------------------------------------------------------------------
              IF @lpDataInfo.MaxDlgMsgs THEN
                 @lpDataInfo.DlgMsgs = MemOffset
                 MemOffset = MemOffset + (@lpDataInfo.MaxDlgMsgs * 4)
                 @lpDataInfo.DlgProcs = MemOffset
                 MemOffset = MemOffset + (@lpDataInfo.MaxDlgMsgs * 4)
              END IF
              IF @lpDataInfo.MaxCtlMsgs THEN
                 @lpDataInfo.CtlMsgs = MemOffset 
                 MemOffset = MemOffset + (@lpDataInfo.MaxCtlMsgs * 4)
                 @lpDataInfo.CtlProcs = MemOffset
                 MemOffset = MemOffset + (@lpDataInfo.MaxCtlMsgs * 4)
              END IF
              IF @lpDataInfo.MaxNotifyMsgs THEN
                 @lpDataInfo.NotifyCtlIds =  MemOffset
                 MemOffset = MemOffset + (@lpDataInfo.MaxNotifyMsgs * 4)
                 @lpDataInfo.NotifyProcs = MemOffset
              END IF
              '===========================================================================
              '>> BEGIN DLG_MSGS
              '===========================================================================
                MsgIndex = 0
              '---------------------------------------------------------------------------
              '~%WM_INITDIALOG   (%WM_INITDIALOG IS ALWAYS FIRST)
              '---------------------------------------------------------------------------
                @[email protected][MsgIndex] = %WM_INITDIALOG
                @[email protected][MsgIndex] = CODEPTR(DLG_10_OnWmInitDialog)
                INCR MsgIndex
              '---------------------------------------------------------------------------
              '~%WM_COMMAND
              '---------------------------------------------------------------------------
                @[email protected][MsgIndex] = %WM_COMMAND
                @[email protected][MsgIndex] = CODEPTR(Ctl_Event_Proc)
                INCR MsgIndex
              '---------------------------------------------------------------------------
              '~%WM_CTLCOLORSTATIC
              '---------------------------------------------------------------------------
                @[email protected][MsgIndex] = %WM_CTLCOLORSTATIC
                @[email protected][MsgIndex] = CODEPTR(WM_CTLCOLOR_Proc)
                INCR MsgIndex
              '---------------------------------------------------------------------------
              '~%WM_CTLCOLORBTN
              '---------------------------------------------------------------------------
                @[email protected][MsgIndex] = %WM_CTLCOLORBTN
                @[email protected][MsgIndex] = CODEPTR(WM_CTLCOLOR_Proc)
                INCR MsgIndex
              '---------------------------------------------------------------------------
              '~%WM_CTLCOLOREDIT
              '---------------------------------------------------------------------------
                @[email protected][MsgIndex] = %WM_CTLCOLOREDIT
                @[email protected][MsgIndex] = CODEPTR(WM_CTLCOLOR_Proc)
                INCR MsgIndex
              '---------------------------------------------------------------------------
              '~%WM_CTLCOLORLISTBOX
              '---------------------------------------------------------------------------
                @[email protected][MsgIndex] = %WM_CTLCOLORLISTBOX
                @[email protected][MsgIndex] = CODEPTR(WM_CTLCOLOR_Proc)
                INCR MsgIndex
              '---------------------------------------------------------------------------
              '~%WM_CLOSE
              '---------------------------------------------------------------------------
                @[email protected][MsgIndex] = %WM_CLOSE
                @[email protected][MsgIndex] = CODEPTR(DLG_10_OnWmClose)
                INCR MsgIndex
              '---------------------------------------------------------------------------
              '~%WM_DESTROY
              '---------------------------------------------------------------------------
                @[email protected][MsgIndex] = %WM_DESTROY
                @[email protected][MsgIndex] = CODEPTR(DLG_10_OnWmDestroy)
                INCR MsgIndex
              '---------------------------------------------------------------------------
              '~%WM_DRAWITEM
              '---------------------------------------------------------------------------
                @[email protected][MsgIndex] = %WM_DRAWITEM
                @[email protected][MsgIndex] = CODEPTR(DLG_10_OnWmDrawItem)
                INCR MsgIndex
              '---------------------------------------------------------------------------
              '>> END DLG_MSGS
              '===========================================================================
              '>> BEGIN CTL_MSGS
              '===========================================================================
                MsgIndex = 0
              '---------------------------------------------------------------------------
              '~DLG_10_CB_OK_OnClick >> %BN_CLICKED <<
              '---------------------------------------------------------------------------
                @[email protected][MsgIndex] = MAKDWD(%DLG_10_CB_OK,%BN_CLICKED)
                @[email protected][MsgIndex] = CODEPTR(DLG_10_CB_OK_OnClick)
                INCR MsgIndex
              '---------------------------------------------------------------------------
              '~DLG_10_CB_CANCEL_OnClick >> %BN_CLICKED <<
              '---------------------------------------------------------------------------
                @[email protected][MsgIndex] = MAKDWD(%DLG_10_CB_CANCEL,%BN_CLICKED)
                @[email protected][MsgIndex] = CODEPTR(DLG_10_CB_CANCEL_OnClick)
                INCR MsgIndex
              '---------------------------------------------------------------------------
              '~DLG_10_PB_BROWSE_OnClick >> %BN_CLICKED <<
              '---------------------------------------------------------------------------
                @[email protected][MsgIndex] = MAKDWD(%DLG_10_PB_BROWSE,%BN_CLICKED)
                @[email protected][MsgIndex] = CODEPTR(DLG_10_PB_BROWSE_OnClick)
                INCR MsgIndex
              '---------------------------------------------------------------------------
              '~DLG_10_LB_1_Onlbnselchange
                @[email protected][MsgIndex] = MAKDWD(%DLG_10_LB_1,%LBN_SELCHANGE)
                @[email protected][MsgIndex] = CODEPTR(DLG_10_LB_1_Onlbnselchange)
                INCR MsgIndex
              ''---------------------------------------------------------------------------
              '>> END CTL_MSGS
              '===========================================================================
              '>> BEGIN NOTIFY_MSGS
              '===========================================================================
                MsgIndex = 0
              '---------------------------------------------------------------------------
              '>> END NOTIFY_MSGS
              '===========================================================================
              '~EOM         End of Messages
              '=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=

              Comment

              Working...
              X