Announcement

Collapse
No announcement yet.

DDT Subclassing, What am I doing wrong?

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

  • DDT Subclassing, What am I doing wrong?

    Guys, I'm trying to make a way to be able to do this:


    SHOWDDT hDlg&, CODEPTR(dlgProc)

    The key is that I don't want any global variables, so
    I use a function with STATIC vars to keep track of all
    the hwnd's and DDT CodePtr's. However, when I run this,
    it seems as if DDT is getting any of the messages.


    Here is what I have so far.

    Code:
    #COMPILE EXE
    #INCLUDE "win32api.inc"
    %DEBUGMODE = 1
    
    TYPE SubClassStruct
        hWnd AS LONG
        lpPrevWndFunc AS LONG
    END TYPE
        
    DECLARE CALLBACK FUNCTION DlgProc()
    
    SUB DEBUG_PRINT (SOut AS STRING)
       #IF %DEBUGMODE
           STATIC hConsole AS LONG, cWritten AS LONG
           IF hConsole = 0 THEN AllocConsole: hConsole = GetStdHandle(-11&)
           WriteConsole hConsole, BYCOPY sOut + $CrLf, LEN(sOut) + 2, cWritten, BYVAL 0&
       #ENDIF
    END SUB
    
    SUB CodePTRList(hWnd&, lCodePTR&)
        STATIC SubClass() AS SubClassStruct
        STATIC lDialogs&
        DEBUG_PRINT "Number of dialogs: " & STR$(lDialogs&)
        IF lDialogs& = 0 THEN
            REDIM SubClass(0 TO 99)
        END IF
    
        FOR lCounter& = 0 TO lDialogs&
            IF SubClass(lCounter&).hWnd& = hWnd& THEN
                lCodePTR& = SubClass(lCounter&).lpPrevWndFunc&
                EXIT SUB
            END IF
        NEXT
    
        INCR lDialogs&
        SubClass(lDialogs&).hWnd& = hWnd&
        SubClass(lDialogs&).lpPrevWndFunc& = lCodePTR&
            
    END SUB
    
    FUNCTION SubClassProc(BYVAL hWnd&, BYVAL wMsg&, BYVAL wParam&, BYVAL lParam&) AS LONG
        CodePTRList hWnd&, OldProc&
        DEBUG_PRINT STR$(hWnd&) & " " & STR$(OldProc&)
        a& = CallWindowProc(OldProc&, hWnd&, wMsg&, wParam&, lParam&)
    END FUNCTION
    
    CALLBACK FUNCTION DlgProc() AS LONG
        DEBUG_PRINT "DLGPROC..." & STR$(CBMSG)
    END FUNCTION
    
    FUNCTION PBMAIN() AS LONG
        DIALOG NEW 0, "This is a Test",,, 160, 50, Style&, eStyle& TO hDlg&   'CALL DWORD CODEPTR(DlgProc())
        CodePTRList hDlg&, SetWindowLong(hDlg&, %GWL_WNDPROC, CODEPTR(SubClassProc))
        DIALOG SHOW MODELESS hDlg&, CALL DlgProc()
        DO
            DIALOG DOEVENTS
        LOOP
    END FUNCTION

    ------------------
    -Greg



    [This message has been edited by Gregery D Engle (edited July 20, 2001).]
    -Greg
    [email protected]
    MCP,MCSA,MCSE,MCSD

  • #2
    Well, a few things for you:

    1. The code is not thread-safe (use of static variables without synchronization control). Search the BBS for "CriticalSection".

    2. The subclass function is not returning the dialog procedure return value:
    Change:
    Code:
    a& = CallWindowProc(OldProc&, hWnd&, wMsg&, wParam&, lParam&)
    to:
    Code:
    FUNCTION = CallWindowProc(OldProc&, hWnd&, wMsg&, wParam&, lParam&)
    However, since a dialog callback will likely process a very large volume of mesages, your code is adding a significant amount of overhead because it is enumerating your array with every single message the subclassed callback processes.

    I'm not clear on what you really want to achieve here, but I think your current approach may need to be reconsidered.

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

    Comment


    • #3
      Lance,

      Thanks for your help. I'm just trying to do some crazy ideas
      that I came up with, using DDT. My code is only enumerating
      the array by the number of dialogs that are being controlled.

      I don't see how that is going to slow it down that much.

      ------------------
      -Greg
      -Greg
      [email protected]
      MCP,MCSA,MCSE,MCSD

      Comment


      • #4
        Lance,

        I read some topics about CriticalSection and this is what I came
        up with, is this Thread Safe?

        Code:
        SUB CodePTRList(hWnd&, lCodePTR&)
            DIM gtCritSect AS CRITICAL_SECTION
            InitializeCriticalSection gtCritSect
            
            EnterCriticalSection gtCritSect
            STATIC SubClass() AS SubClassStruct
            STATIC lDialogs&
            DEBUG_PRINT "Number of dialogs: " & STR$(lDialogs&)
            IF lDialogs& = 0 THEN
                REDIM SubClass(0 TO 99)
            END IF
        
            FOR lCounter& = 0 TO lDialogs&
                IF SubClass(lCounter&).hWnd& = hWnd& THEN
                    lCodePTR& = SubClass(lCounter&).lpPrevWndFunc&
                    EXIT SUB
                END IF
            NEXT
        
            INCR lDialogs&
            SubClass(lDialogs&).hWnd& = hWnd&
            SubClass(lDialogs&).lpPrevWndFunc& = lCodePTR&
            LeaveCriticalSection gtCritSect
        END SUB
        ------------------
        -Greg
        -Greg
        [email protected]
        MCP,MCSA,MCSE,MCSD

        Comment


        • #5
          Firstly, you'll probably need the critical_section variable to be global.

          Basically, you must call InitializeCriticalSection just once at the start of the app. Although you could probably do it by making it a STATIC variable in the subclass function, and ensuring it is only called one per thread. Ditto for DeleteCriticalSection().

          The calls to EnterCriticalSection() and LeaveCriticalSection(), etc, should stay as they are in that function.

          Out of curousity, when testing your code, have you tried counting the number of messages that flow though the subclass and/or callback function? You may be surprised at the numbers you get.

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

          Comment


          • #6
            Lance,

            Ok, I'll make the changes that you recommend.

            No I haven't counted the messages yet, but with my tests so
            far I don't see any slowness whatsoever.

            ------------------
            -Greg
            -Greg
            [email protected]
            MCP,MCSA,MCSE,MCSD

            Comment

            Working...
            X