Announcement

Collapse
No announcement yet.

Global variable inside Function WndProc

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

  • Global variable inside Function WndProc

    Greetings and a happy new year.


    If the code runs below, then I receive a funny result in the 2. MSGBOX.

    The 1. MSGBOX shows me the value 20. That is okay. But the 2. MSGBOX shows 10.

    Is there inside the WndProc only a limited use of global variables allowed?

    Please can somebody explain me this conduct.


    Thank you

    Norbert
    (PBWin 7.04 / Win 98 SE)

    Code:
    #COMPILE EXE
    #DIM ALL
    
    #INCLUDE "WIN32API.INC"
    
    GLOBAL hWndMain AS LONG
    GLOBAL g_var AS LONG
    
    FUNCTION WINMAIN (BYVAL hInstance AS LONG, _
                      BYVAL hPrevInstance AS LONG, _
                      lpCmdLine AS ASCIIZ PTR, _
                      BYVAL iCmdShow AS LONG) AS LONG
    
      LOCAL Msg AS tagMsg
      LOCAL w_ndclass AS WndClassEx
      LOCAL szClassName AS ASCIIZ * 80
      LOCAL hWnd AS LONG
      LOCAL hMenu AS LONG
    
      szClassName = "MYPROGRAM32"
      w_ndclass.cbSize = SIZEOF(W_ndClass)
      w_ndclass.style = %CS_HREDRAW OR %CS_VREDRAW
      w_ndclass.lpfnWndProc = CODEPTR(WndProc)
      w_ndclass.cbClsExtra = 0
      w_ndclass.cbWndExtra = 0
      w_ndclass.hInstance = hInstance
      w_ndclass.hIcon = LoadIcon(hInstance, "PROGRAM")
      w_ndclass.hCursor = LoadCursor(%NULL, BYVAL %IDC_ARROW)
      w_ndclass.hbrBackground = GetStockObject(%WHITE_BRUSH) '%WHITE_BRUSH)
      w_ndclass.lpszMenuName = %NULL
      w_ndclass.lpszClassName = VARPTR(szClassName)
      w_ndclass.hIconSm = LoadIcon(hInstance, BYVAL %IDI_APPLICATION)
    
      RegisterClassEx w_ndclass
    
      hWndMain = CreateWindowEx(0, szClassName, _ 
                                "Test", _ 
                                %WS_OVERLAPPEDWINDOW, _ 
                                100, _ 
                                100, _ 
                                700, _ 
                                400, _ 
                                %HWND_DESKTOP, _ 
                                hMenu, _ 
                                hInstance, _ 
                                BYVAL %NULL) 
    
      ShowWindow hWndMain, iCmdShow
      UpdateWindow hWndMain
      
      WHILE GetMessage(Msg, %NULL, 0, 0)
        TranslateMessage Msg
        DispatchMessage Msg
      WEND
    
      FUNCTION = msg.wParam
    
    END FUNCTION 
    
    FUNCTION WndProc (BYVAL hWnd AS LONG, BYVAL wMsg AS LONG, _
                      BYVAL wParam AS LONG, BYVAL lParam AS LONG) EXPORT AS LONG
    
      g_var = 10
    
      SELECT CASE wMsg
    
        CASE %WM_CREATE
    
        g_var = g_var + 10
        
        MSGBOX STR$(g_var),,"1. MSGBOX"
        MSGBOX STR$(g_var),,"2. MSGBOX"
    
        PostQuitMessage 0
        FUNCTION = 0
        EXIT FUNCTION
     
      END SELECT
    
      FUNCTION = DefWindowProc(hWnd, wMsg, wParam, lParam)
    
    END FUNCTION

  • #2
    Added output file to show WndProc Calls. Listed at end.

    Code:
    #COMPILE EXE
    #DIM ALL
    Global fp As Long
    
    #INCLUDE "WIN32API.INC"
    
    GLOBAL hWndMain AS LONG
    GLOBAL g_var AS LONG
    
    FUNCTION WINMAIN (BYVAL hInstance AS LONG, _
                      BYVAL hPrevInstance AS LONG, _
                      Byval lpCmdLine AS ASCIIZ PTR, _
                      BYVAL iCmdShow AS LONG) AS LONG
    
      LOCAL Msg AS tagMsg
      LOCAL w_ndclass AS WndClassEx
      LOCAL szClassName AS ASCIIZ * 80
      LOCAL hWnd AS LONG
      LOCAL hMenu AS LONG
    
      fp=freefile
      Open "Output.txt" For Output As #fp
      szClassName = "MYPROGRAM32"
      w_ndclass.cbSize = SIZEOF(W_ndClass)
      w_ndclass.style = %CS_HREDRAW OR %CS_VREDRAW
      w_ndclass.lpfnWndProc = CODEPTR(WndProc)
      w_ndclass.cbClsExtra = 0
      w_ndclass.cbWndExtra = 0
      w_ndclass.hInstance = hInstance
      w_ndclass.hIcon = LoadIcon(hInstance, "PROGRAM")
      w_ndclass.hCursor = LoadCursor(%NULL, BYVAL %IDC_ARROW)
      w_ndclass.hbrBackground = GetStockObject(%WHITE_BRUSH) '%WHITE_BRUSH)
      w_ndclass.lpszMenuName = %NULL
      w_ndclass.lpszClassName = VARPTR(szClassName)
      w_ndclass.hIconSm = LoadIcon(hInstance, BYVAL %IDI_APPLICATION)
    
      RegisterClassEx w_ndclass
    
      hWndMain = CreateWindowEx(0, szClassName, _
                                "Test", _
                                %WS_OVERLAPPEDWINDOW, _
                                100, _
                                100, _
                                700, _
                                400, _
                                %HWND_DESKTOP, _
                                hMenu, _
                                hInstance, _
                                BYVAL %NULL)
    
      ShowWindow hWndMain, iCmdShow
      UpdateWindow hWndMain
    
      WHILE GetMessage(Msg, %NULL, 0, 0)
        TranslateMessage Msg
        DispatchMessage Msg
      WEND
      Close #fp
      
      FUNCTION = msg.wParam
    
    END FUNCTION
    
    FUNCTION WndProc (BYVAL hWnd AS LONG, BYVAL wMsg AS LONG, _
                      BYVAL wParam AS LONG, BYVAL lParam AS LONG) EXPORT AS LONG
    
      g_var = 10
      Print #fp, wMsg, g_var
      SELECT CASE wMsg
    
        CASE %WM_CREATE
    
        g_var = g_var + 10
    
        MSGBOX STR$(g_var),,"1. MSGBOX"
        MSGBOX STR$(g_var),,"2. MSGBOX"
    
        PostQuitMessage 0
        FUNCTION = 0
    
        EXIT FUNCTION
    
      END SELECT
    
      FUNCTION = DefWindowProc(hWnd, wMsg, wParam, lParam)
    
    END FUNCTION
    1 is %WM_CREATE

    Output.txt
    ===================
    ' 36 10
    ' 129 10
    ' 131 10
    ' 1 10
    ' 28 10
    ' 28 10
    ' 24 10
    ' 70 10
    ' 70 10
    ' 28 10
    ' 134 10
    ' 13 10
    ' 6 10
    ' 7 10
    ' 133 10
    ' 13 10
    ' 20 10
    ' 71 10
    ' 5 10
    ' 3 10
    ' 15 10
    '
    Fred
    "fharris"+Chr$(64)+"evenlink"+Chr$(46)+"com"

    Comment


    • #3
      No limitation, it is just that other messages are executing in between the first and second message box, thus resetting g_var to 10 before the second one executes.
      Adam Drake
      Drake Software

      Comment


      • #4
        Perfection With A Vengence

        So, not only do globals work under these conditions, they work to perfection with a vengence. IMO, the MsgBox function isn't a good way to go in terms of debugging windows programs.
        Fred
        "fharris"+Chr$(64)+"evenlink"+Chr$(46)+"com"

        Comment


        • #5
          IMO, the MsgBox function isn't a good way to go in terms of debugging windows programs.

          If you misuse GLOBALs like this, NOTHING is a good way to debug!!

          But MSGBOX brings some other "gotchas" ...so use STDOUT instead:
          Simple STDOUT for PB/DLL and PB/Win
          Michael Mattias
          Tal Systems (retired)
          Port Washington WI USA
          [email protected]
          http://www.talsystems.com

          Comment


          • #6
            Actually, I found that example interesting. I would have never thought to even attempt something like that. But before seeing this, if you had asked me if other messages could come through and be processed without the WM_CREATE message returning, I wouldn't have really been absolutely certain. I certainly wouldn't have bet my life on it.
            Fred
            "fharris"+Chr$(64)+"evenlink"+Chr$(46)+"com"

            Comment


            • #7
              Edited Note: hmm, I just ran this under XP and for some reason the Console Viewer window gets minimized to the taskbar, not sure why (will look later) but you may have to maximize the Console Viewer from the taskbar


              here's a couple of ways to get messages and variable values on the fly, w/o using msgbox,



              Code:
              'MessageViewer.bas
              '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
              'Here's a little windows message viewer, based on Semen Matusovski's WriteConsole code,
              'also using Jim Huguley's message list, it's been very helpful to me ..maybe someone would like...
              'also has a little demo to play with, using some of Edwin Knoppert's NCCALCSIZE_PARAMS code...
              'resize dialog boxes to see NCCALCSIZE msg's....
              'Thanks, All
              'Brad
              
                 #COMPILE EXE
                 #REGISTER NONE
                 #DIM ALL
                 #INCLUDE "Win32Api.Inc"
                 GLOBAL hDlg2&, hVDsp& ,Msg AS tagMsg
                 '================================
              
                 DECLARE FUNCTION GetMsg (hndl AS LONG) AS STRING
                 DECLARE FUNCTION ShowVar (Var_label$, Var_value&) AS LONG
              
              '________________________________________________________________________________
              'Semen Matusovski's CPrint code:
              
                 SUB CPrint (SOut AS STRING)
                    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&
                 END SUB
              
              '________________________________________________________________________________
                 CALLBACK FUNCTION DlgProc3
                    STATIC p1 AS LONG, p2 AS LONG, p3 AS LONG, p4 AS LONG ,msgCnt&,NcCapture&,NCmoveDiff&
                    DIM pNCC AS NCCALCSIZE_PARAMS PTR
              
                    CPrint STR$(msgCnt&)+GetMsg(CBMSG)
                    INCR msgCnt&
              
              showVar "hover", CBCTL
              
                 SELECT CASE CBMSG
              
                     CASE %WM_MOUSEMOVE
                        BEEP
                 showVar "NCBCTL", CBMSG
              
                 CASE %WM_NCCALCSIZE
                     IF CBWPARAM = 0 THEN EXIT SELECT
              
                     pNCC = CBLPARAM
                     showVar "NCCALCSIZE0",@pNCC.rgrc( 0 ).nRight
                     showVar "NCCALCSIZEl",@pNCC.rgrc( 1 ).nRight
                     showVar "NCCALCSIZE2",@pNCC.rgrc( 2 ).nRight
                     showVar "NCCALCSIZE0b",@pNCC.rgrc( 0 ).nBottom
                     showVar "NCCALCSIZElb",@pNCC.rgrc( 1 ).nBottom
                     showVar "NCCALCSIZE2b",@pNCC.rgrc( 2 ).nBottom
                 END SELECT
              
                 IF CBMSG = %WM_DESTROY THEN PostQuitMessage 0
                 END FUNCTION
              
              
              '________________________________________________________________________________
                 CALLBACK FUNCTION DlgProc
                    STATIC p1 AS LONG, p2 AS LONG, p3 AS LONG, p4 AS LONG ,msgCnt&,NcCapture&,NCmoveDiff&
                    DIM pNCC AS NCCALCSIZE_PARAMS PTR
              
                    CPrint STR$(msgCnt&)+GetMsg(CBMSG)
                    INCR msgCnt&
              
                 SELECT CASE CBMSG
              
                 CASE %WM_NCCALCSIZE
                     IF CBWPARAM = 0 THEN EXIT SELECT
              
                     pNCC = CBLPARAM
                     showVar "NCCALCSIZE0",@pNCC.rgrc( 0 ).nRight
                     showVar "NCCALCSIZEl",@pNCC.rgrc( 1 ).nRight
                     showVar "NCCALCSIZE2",@pNCC.rgrc( 2 ).nRight
                     showVar "NCCALCSIZE0b",@pNCC.rgrc( 0 ).nBottom
                     showVar "NCCALCSIZElb",@pNCC.rgrc( 1 ).nBottom
                     showVar "NCCALCSIZE2b",@pNCC.rgrc( 2 ).nBottom
                 END SELECT
              
                 IF CBMSG = %WM_DESTROY THEN PostQuitMessage 0
                 END FUNCTION
              
                  CALLBACK FUNCTION DlgProc2
                      IF CBMSG = %WM_DESTROY THEN PostQuitMessage 0
                  END FUNCTION
              
              '________________________________________________________________________________
                 FUNCTION PBMAIN
                    LOCAL hDlg AS LONG ,TextStr$
                            TextStr$ = " An Introduction...New to PowerBASIC? We offer compilers "
              
                    DIALOG NEW 0, "Sample Dlg: NCCALCSIZE_PARAMS", , , 300, 200, %WS_CLIPCHILDREN OR %WS_CLIPSIBLINGS _
                                  OR %WS_overlappedwindow TO hDlg
                    PostMessage hDlg, %WM_USER + 1, 300, 50
                    PostMessage hDlg, %WM_USER + 2, 400, 200
                    CONTROL ADD BUTTON, hdlg, 101, "Button", 10, 10, 60, 12 ,CALL DlgProc3
                    CONTROL ADD TEXTBOX, hDlg, 100, TextStr$, 33, 43, 100, 50, %ES_multiline OR %WS_VSCROLL _
                                                       OR %ES_AUTOVSCROLL , %WS_EX_CLIENTEDGE
              
              
                    DIALOG NEW hDlg&, "Custom control test1",20,100, 240, 80, %WS_CHILD OR %WS_VISIBLE _
                     OR %WS_CLIPCHILDREN OR %WS_VSCROLL OR %WS_thickframe  , %ws_ex_clientedge TO hDlg2&
                    CONTROL ADD BUTTON, hDlg2&, 4000, "Button", 5,  20, 50, 14,%bS_NOTIFY,CALL DlgProc3
              
                    DIALOG SHOW MODELESS hDlg2&, CALL DlgProc2
              
                    DIALOG SHOW MODELESS hDlg CALL DlgProc
              
                    WHILE getMessage(Msg, %NULL, 0, 0)
                      TranslateMessage Msg
                      DispatchMessage Msg
                    WEND
              
                 END FUNCTION
              
              '________________________________________________________________________________
              'List Cut'n'Pasted from Jim Huguley's "Display Control Messages" code at:
              'MESSAGE [URL=http://www.powerbasic.com/support/forums/Forum7/HTML/000348.html]
              'partial list... more at Jim's post
              
              FUNCTION GetMsg (hndl AS LONG) AS STRING
              SELECT CASE hndl
                  CASE %EN_UPDATE
                        FUNCTION = " &H0  %EN_UPDATE"
              
                  CASE %WM_NULL
                       FUNCTION = " &H0  %WM_NULL"
                  CASE %WM_CREATE
                       FUNCTION = " &H1  %WM_CREATE"
              
                  CASE %WM_DESTROY
                       FUNCTION = " &H2  %WM_DESTROY"
                  CASE %WM_MOVE
                       FUNCTION = " &H3  %WM_MOVE"
              
                  CASE %WM_SIZE
                       FUNCTION = " &H5  %WM_SIZE"
                  CASE %WM_ACTIVATE
                       FUNCTION = " &H6  %WM_ACTIVATE"
              
                  CASE %WM_SETFOCUS
                       FUNCTION = " &H7  %WM_SETFOCUS"
                  CASE %WM_KILLFOCUS
                       FUNCTION = " &H8  %WM_KILLFOCUS"
              
                  CASE %WM_ENABLE
                       FUNCTION = " &HA  %WM_ENABLE"
                  CASE %WM_SETREDRAW
                       FUNCTION = " &HB  %WM_SETREDRAW"
              
                  CASE %WM_SETTEXT
                       FUNCTION = " &HC  %WM_SETTEXT"
                  CASE %WM_GETTEXT
                       FUNCTION = " &HD  %WM_GETTEXT"
              
                  CASE %WM_GETTEXTLENGTH
                       FUNCTION = " &HE  %WM_GETTEXTLENGTH"
                  CASE %WM_PAINT
                       FUNCTION = " &HF  %WM_PAINT"
              
                  CASE %WM_CLOSE
                       FUNCTION = " &H10  %WM_CLOSE"
                  CASE %WM_QUERYENDSESSION
                       FUNCTION = " &H11  %WM_QUERYENDSESSION"
              
                  CASE %WM_QUIT
                       FUNCTION = " &H12  %WM_QUIT"
                  CASE %WM_QUERYOPEN
                       FUNCTION = " &H13  %WM_QUERYOPEN"
              
                  CASE %WM_ERASEBKGND
                       FUNCTION = " &H14  %WM_ERASEBKGND"
                  CASE %WM_SYSCOLORCHANGE
                       FUNCTION = " &H15  %WM_SYSCOLORCHANGE"
              
                  CASE %WM_ENDSESSION
                       FUNCTION = " &H16  %WM_ENDSESSION"
                  CASE %WM_SHOWWINDOW
                       FUNCTION = " &H18  %WM_SHOWWINDOW"
              
                  CASE %WM_WININICHANGE,%WM_SETTINGCHANGE
                       FUNCTION = " &H1A  %WM_WININICHANGE,%WM_SETTINGCHANGE"
                  CASE %WM_DEVMODECHANGE
                       FUNCTION = " &H1B  %WM_DEVMODECHANGE"
              
                  CASE %WM_ACTIVATEAPP
                       FUNCTION = " &H1C  %WM_ACTIVATEAPP"
                  CASE %WM_FONTCHANGE
                       FUNCTION = " &H1D  %WM_FONTCHANGE"
              
                  CASE %WM_TIMECHANGE
                       FUNCTION = " &H1E  %WM_TIMECHANGE"
                  CASE %WM_CANCELMODE
                       FUNCTION = " &H1F  %WM_CANCELMODE"
              
                  CASE %WM_SETCURSOR
                       FUNCTION = " &H20  %WM_SETCURSOR"
                  CASE %WM_MOUSEACTIVATE
                       FUNCTION = " &H21  %WM_MOUSEACTIVATE"
              
                  CASE %WM_CHILDACTIVATE
                       FUNCTION = " &H22  %WM_CHILDACTIVATE"
                  CASE %WM_QUEUESYNC
                       FUNCTION = " &H23  %WM_QUEUESYNC"
              
                  CASE %WM_GETMINMAXINFO
                       FUNCTION = " &H24  %WM_GETMINMAXINFO"
                  CASE %WM_PAINTICON
                       FUNCTION = " &H26  %WM_PAINTICON"
              
                  CASE %WM_ICONERASEBKGND
                       FUNCTION = " &H27  %WM_ICONERASEBKGND"
                  CASE %WM_NEXTDLGCTL
                       FUNCTION = " &H28  %WM_NEXTDLGCTL"
              
                  CASE %WM_SPOOLERSTATUS
                       FUNCTION = " &H2A  %WM_SPOOLERSTATUS"
                  CASE %WM_DRAWITEM
                       FUNCTION = " &H2B  %WM_DRAWITEM"
              
                  CASE %WM_MEASUREITEM
                       FUNCTION = " &H2C  %WM_MEASUREITEM"
                  CASE %WM_DELETEITEM
                       FUNCTION = " &H2D  %WM_DELETEITEM"
              
                  CASE %WM_VKEYTOITEM
                       FUNCTION = " &H2E  %WM_VKEYTOITEM"
                  CASE %WM_CHARTOITEM
                       FUNCTION = " &H2F  %WM_CHARTOITEM"
              
                  CASE %WM_SETFONT
                       FUNCTION = " &H30  %WM_SETFONT"
                  CASE %WM_GETFONT
                       FUNCTION = " &H31  %WM_GETFONT"
              
                  CASE %WM_SETHOTKEY
                       FUNCTION = " &H32  %WM_SETHOTKEY"
                  CASE %WM_GETHOTKEY
                       FUNCTION = " &H33  %WM_GETHOTKEY"
              
                  CASE %WM_QUERYDRAGICON
                       FUNCTION = " &H37  %WM_QUERYDRAGICON"
                  CASE %WM_COMPAREITEM
                       FUNCTION = " &H39  %WM_COMPAREITEM"
              
                  CASE %WM_COMPACTING
                       FUNCTION = " &H41  %WM_COMPACTING"
                  CASE %WM_OTHERWINDOWCREATED
                       FUNCTION = " &H42  %WM_OTHERWINDOWCREATED"   '  no longer suported
              
                  CASE %WM_OTHERWINDOWDESTROYED
                       FUNCTION = " &H43  %WM_OTHERWINDOWDESTROYED" '  no longer suported
                  CASE %WM_COMMNOTIFY
                       FUNCTION = " &H44  %WM_COMMNOTIFY            '  no longer suported
              
                  '
                  ' notifications passed in low word of lParam on WM_COMMNOTIFY messages
                  ' %CN_RECEIVE                                  = &H1
                  ' %CN_TRANSMIT                                 = &H2
                  ' %CN_EVENT                                    = &H4
                  '
              
                  CASE %WM_WINDOWPOSCHANGING
                       FUNCTION = " &H46  %WM_WINDOWPOSCHANGING"
                  CASE %WM_WINDOWPOSCHANGED
                       FUNCTION = " &H47  %WM_WINDOWPOSCHANGED"
              
                  CASE %WM_POWER
                       FUNCTION = " &H48  %WM_POWER"
                  '
                  ' wParam for WM_POWER window message and DRV_POWER driver notification
              
                  ' %PWR_OK                                      = 1
                  ' %PWR_FAIL                                    = -1
                  ' %PWR_SUSPENDREQUEST                          = 1
                  ' %PWR_SUSPENDRESUME                           = 2
                  ' %PWR_CRITICALRESUME                          = 3
                  '
              
                  CASE %WM_COPYDATA
                       FUNCTION = " &H4A   %WM_COPYDATA"
                  CASE %WM_CANCELJOURNAL
                       FUNCTION = " &H4B   %WM_CANCELJOURNAL"
              
                  CASE %WM_NOTIFY
                       FUNCTION = " &H4E   %WM_NOTIFY"
                  CASE %WM_INPUTLANGUAGECHANGEREQUEST
                       FUNCTION = " &H50   %WM_INPUTLANGUAGECHANGEREQUEST"
              
                  CASE %WM_INPUTLANGUAGECHANGE
                       FUNCTION = " &H51   %WM_INPUTLANGUAGECHANGE"
                  CASE %WM_TCARD
                       FUNCTION = " &H52   %WM_TCARD"
              
                  CASE %WM_HELP
                       FUNCTION = " &H53   %WM_HELP"
                  CASE %WM_USERCHANGED
                       FUNCTION = " &H54   %WM_USERCHANGED"
              
                  CASE %WM_NOTIFYFORMAT
                       FUNCTION = " &H55   %WM_NOTIFYFORMAT"
                  CASE %WM_CONTEXTMENU
                       FUNCTION = " &H7B   %WM_CONTEXTMENU"
              
                  CASE %WM_STYLECHANGING
                       FUNCTION = " &H7C   %WM_STYLECHANGING"
                  CASE %WM_STYLECHANGED
                       FUNCTION = " &H7D   %WM_STYLECHANGED"
              
                  CASE %WM_DISPLAYCHANGE
                       FUNCTION = " &H7E   %WM_DISPLAYCHANGE"
                  CASE %WM_GETICON
                       FUNCTION = " &H7F   %WM_GETICON"
              
                  CASE %WM_SETICON
                       FUNCTION = " &H80   %WM_SETICON"
                  CASE %WM_NCCREATE
                       FUNCTION = " &H81   %WM_NCCREATE"
              
                  CASE %WM_NCDESTROY
                       FUNCTION = " &H82   %WM_NCDESTROY"
                  CASE %WM_NCCALCSIZE
                       FUNCTION = " &H83   %WM_NCCALCSIZE"
              
                  CASE %WM_NCHITTEST
                       FUNCTION = " &H84   %WM_NCHITTEST"
                  CASE %WM_NCPAINT
                       FUNCTION = " &H85   %WM_NCPAINT"
              
                  CASE %WM_NCACTIVATE
                       FUNCTION = " &H86   %WM_NCACTIVATE"
                  CASE %WM_GETDLGCODE
                       FUNCTION = " &H87   %WM_GETDLGCODE"
              
                  CASE %WM_NCMOUSEMOVE
                       FUNCTION = " &HA0   %WM_NCMOUSEMOVE"
                  CASE %WM_NCLBUTTONDOWN
                       FUNCTION = " &HA1   %WM_NCLBUTTONDOWN"
              
                  CASE %WM_NCLBUTTONUP
                       FUNCTION = " &HA2   %WM_NCLBUTTONUP"
                  CASE %WM_NCLBUTTONDBLCLK
                       FUNCTION = " &HA3   %WM_NCLBUTTONDBLCLK"
              
                  CASE %WM_NCRBUTTONDOWN
                       FUNCTION = " &HA4   %WM_NCRBUTTONDOWN"
                  CASE %WM_NCRBUTTONUP
                       FUNCTION = " &HA5   %WM_NCRBUTTONUP"
              
                  CASE %WM_NCRBUTTONDBLCLK
                       FUNCTION = " &HA6   %WM_NCRBUTTONDBLCLK"
                  CASE %WM_NCMBUTTONDOWN
                       FUNCTION = " &HA7   %WM_NCMBUTTONDOWN"
              
                  CASE %WM_NCMBUTTONUP
                       FUNCTION = " &HA8   %WM_NCMBUTTONUP"
                  CASE %WM_NCMBUTTONDBLCLK
                       FUNCTION = " &HA9   %WM_NCMBUTTONDBLCLK"
              
                  CASE %WM_KEYFIRST, %WM_KEYDOWN
                       FUNCTION = " &H100  %WM_KEYFIRST, %WM_KEYDOWN"
                  CASE %WM_KEYUP
                       FUNCTION = " &H101  %WM_KEYUP"
              
                  CASE %WM_CHAR
                       FUNCTION = " &H102  %WM_CHAR"
                  CASE %WM_DEADCHAR
                       FUNCTION = " &H103  %WM_DEADCHAR"
              
                  CASE %WM_SYSKEYDOWN
                       FUNCTION = " &H104  %WM_SYSKEYDOWN"
                  CASE %WM_SYSKEYUP
                       FUNCTION = " &H105  %WM_SYSKEYUP"
              
                  CASE %WM_SYSCHAR
                       FUNCTION = " &H106  %WM_SYSCHAR"
                  CASE %WM_SYSDEADCHAR
                       FUNCTION = " &H107  %WM_SYSDEADCHAR"
              
                  CASE %WM_KEYLAST
                       FUNCTION = " &H108  %WM_KEYLAST"
                  CASE %WM_INITDIALOG
                       FUNCTION = " &H110  %WM_INITDIALOG"
              
                  CASE %WM_COMMAND
                       FUNCTION = " &H111  %WM_COMMAND  " + STR$(Msg.wparam)
                  CASE %WM_SYSCOMMAND
                       FUNCTION = "&H112  %WM_SYSCOMMAND"
              
                  CASE %WM_TIMER
                       FUNCTION = " &H113  %WM_TIMER"
                  CASE %WM_HSCROLL
                       FUNCTION = " &H114  %WM_HSCROLL"
              
                  CASE %WM_VSCROLL
                       FUNCTION = " &H115  %WM_VSCROLL"
                  CASE %WM_INITMENU
                       FUNCTION = " &H116  %WM_INITMENU"
              
                  CASE %WM_INITMENUPOPUP
                       FUNCTION = " &H117  %WM_INITMENUPOPUP"
                  CASE %WM_MENUSELECT
                       FUNCTION = " &H11F  %WM_MENUSELECT"
              
                  CASE %WM_MENUCHAR
                       FUNCTION = " &H120  %WM_MENUCHAR"
                  CASE %WM_ENTERIDLE
                       FUNCTION = " &H121  %WM_ENTERIDLE"
              
                  CASE %WM_CTLCOLORMSGBOX
                       FUNCTION = " &H132  %WM_CTLCOLORMSGBOX"
                  CASE %WM_CTLCOLOREDIT
                       FUNCTION = " &H133  %WM_CTLCOLOREDIT       "  + STR$(LOWRD(Msg.Lparam))
                  CASE %WM_CTLCOLORLISTBOX
                       FUNCTION = " &H134  %WM_CTLCOLORLISTBOX"
              
                  CASE %WM_CTLCOLORBTN
                       FUNCTION = " &H135  %WM_CTLCOLORBTN"
                  CASE %WM_CTLCOLORDLG
                       FUNCTION = " &H136  %WM_CTLCOLORDLG"
              
                  CASE %WM_CTLCOLORSCROLLBAR
                       FUNCTION = " &H137  %WM_CTLCOLORSCROLLBAR"
                  CASE %WM_CTLCOLORSTATIC
                       FUNCTION = " &H138  %WM_CTLCOLORSTATIC"
              
                  CASE %WM_MOUSEFIRST,%WM_MOUSEMOVE
                       FUNCTION = " &H200  %WM_MOUSEFIRST,%WM_MOUSEMOVE"
                  CASE %WM_LBUTTONDOWN
                       FUNCTION = " &H201  %WM_LBUTTONDOWN"
              
                  CASE %WM_LBUTTONUP
                       FUNCTION = " &H202  %WM_LBUTTONUP"
                  CASE %WM_LBUTTONDBLCLK
                       FUNCTION = " &H203  %WM_LBUTTONDBLCLK"
              
                  CASE %WM_RBUTTONDOWN
                       FUNCTION = " &H204  %WM_RBUTTONDOWN"
                  CASE %WM_RBUTTONUP
                       FUNCTION = " &H205  %WM_RBUTTONUP"
              
                  CASE %WM_RBUTTONDBLCLK
                       FUNCTION = " &H206  %WM_RBUTTONDBLCLK"
                  CASE %WM_MBUTTONDOWN
                       FUNCTION = " &H207  %WM_MBUTTONDOWN"
              
                  CASE %WM_MBUTTONUP
                       FUNCTION = " &H208  %WM_MBUTTONUP"
                  CASE %WM_MBUTTONDBLCLK
                       FUNCTION = " &H209  %WM_MBUTTONDBLCLK"
              
                  CASE %WM_MOUSEWHEEL, %WM_MOUSELAST
                       FUNCTION = " &H20A  %WM_MOUSEWHEEL, %WM_MOUSELAST"
                  CASE %WM_PARENTNOTIFY
                       FUNCTION = " &H210  %WM_PARENTNOTIFY"
              
                  CASE %WM_ENTERMENULOOP
                       FUNCTION = " &H211  %WM_ENTERMENULOOP"
                  CASE %WM_EXITMENULOOP
                       FUNCTION = " &H212  %WM_EXITMENULOOP"
              
                  CASE %WM_SIZING
                       FUNCTION = " &H214  %WM_SIZING"
                  CASE %WM_CAPTURECHANGED
                       FUNCTION = " &H215  %WM_CAPTURECHANGED"
              
                  CASE %WM_MOVING
                       FUNCTION = " &H216  %WM_MOVING"
                  CASE %WM_POWERBROADCAST
                       FUNCTION = " &H218  %WM_POWERBROADCAST"
              
                  CASE %WM_DEVICECHANGE
                       FUNCTION = " &H219  %WM_DEVICECHANGE"
                  CASE %WM_MDICREATE
                       FUNCTION = " &H220  %WM_MDICREATE"
              
                  CASE %WM_MDIDESTROY
                       FUNCTION = " &H221  %WM_MDIDESTROY"
                  CASE %WM_MDIACTIVATE
                       FUNCTION = " &H222  %WM_MDIACTIVATE"
              
                  CASE %WM_MDIRESTORE
                       FUNCTION = " &H223  %WM_MDIRESTORE"
                  CASE %WM_MDINEXT
                       FUNCTION = " &H224  %WM_MDINEXT"
              
                  CASE %WM_MDIMAXIMIZE
                       FUNCTION = " &H225  %WM_MDIMAXIMIZE"
                  CASE %WM_MDITILE
                       FUNCTION = " &H226  %WM_MDITILE"
              
                  CASE %WM_MDICASCADE
                       FUNCTION = " &H227  %WM_MDICASCADE"
                  CASE %WM_MDIICONARRANGE
                       FUNCTION = " &H228  %WM_MDIICONARRANGE"
              
                  CASE %WM_MDIGETACTIVE
                       FUNCTION = " &H229  %WM_MDIGETACTIVE"
                  CASE %WM_MDISETMENU
                       FUNCTION = " &H230  %WM_MDISETMENU"
              
                  CASE %WM_DROPFILES
                       FUNCTION = " &H233  %WM_DROPFILES"
                  CASE %WM_MDIREFRESHMENU
                       FUNCTION = " &H234  %WM_MDIREFRESHMENU"
              
                  CASE %WM_MOUSEHOVER
                       FUNCTION = " &H2A1  %WM_MOUSEHOVER"
                  CASE %WM_MOUSELEAVE
                       FUNCTION = " &H2A3  %WM_MOUSELEAVE"
              
                  CASE %WM_CUT
                       FUNCTION = " &H300  %WM_CUT"
                  CASE %WM_COPY
                       FUNCTION = " &H301  %WM_COPY"
              
                  CASE %WM_PASTE
                       FUNCTION = " &H302  %WM_PASTE"
                  CASE %WM_CLEAR
                       FUNCTION = " &H303  %WM_CLEAR"
              
                  CASE %WM_UNDO
                       FUNCTION = " &H304  %WM_UNDO"
                  CASE %WM_RENDERFORMAT
                       FUNCTION = " &H305  %WM_RENDERFORMAT"
              
                  CASE %WM_RENDERALLFORMATS
                       FUNCTION = " &H306  %WM_RENDERALLFORMATS"
                  CASE %WM_DESTROYCLIPBOARD
                       FUNCTION = " &H307  %WM_DESTROYCLIPBOARD"
              
                  CASE %WM_DRAWCLIPBOARD
                       FUNCTION = " &H308  %WM_DRAWCLIPBOARD"
                  CASE %WM_PAINTCLIPBOARD
                       FUNCTION = " &H309  %WM_PAINTCLIPBOARD"
              
                  CASE %WM_VSCROLLCLIPBOARD
                       FUNCTION = " &H30A  %WM_VSCROLLCLIPBOARD"
                  CASE %WM_SIZECLIPBOARD
                       FUNCTION = " &H30B  %WM_SIZECLIPBOARD"
              
                  CASE %WM_ASKCBFORMATNAME
                       FUNCTION = " &H30C  %WM_ASKCBFORMATNAME"
                  CASE %WM_CHANGECBCHAIN
                       FUNCTION = " &H30D  %WM_CHANGECBCHAIN"
              
                  CASE %WM_HSCROLLCLIPBOARD
                       FUNCTION = " &H30E  %WM_HSCROLLCLIPBOARD"
                  CASE %WM_QUERYNEWPALETTE
                       FUNCTION = " &H30F  %WM_QUERYNEWPALETTE"
              
                  CASE %WM_PALETTEISCHANGING
                       FUNCTION = " &H310  %WM_PALETTEISCHANGING"
                  CASE %WM_PALETTECHANGED
                       FUNCTION = " &H311  %WM_PALETTECHANGED"
              
                  CASE %WM_HOTKEY
                       FUNCTION = " &H312  %WM_HOTKEY"
                  CASE %WM_PRINTCLIENT
                       FUNCTION = " &H318  %WM_PRINTCLIENT"
              
                  CASE %WM_PENWINFIRST
                       FUNCTION = " &H380  %WM_PENWINFIRST"
                  CASE %WM_PENWINLAST
                       FUNCTION = " &H38F  %WM_PENWINLAST"
              
                  CASE %WM_USER
                       FUNCTION = " &H400  %WM_USER"
                  CASE  %WM_ENTERSIZEMOVE
                       FUNCTION  = "&H231  %WM_ENTERSIZEMOVE"
              
                  CASE    %WM_EXITSIZEMOVE
                      FUNCTION   = "&H232  %WM_EXITSIZEMOVE"
                  CASE ELSE
                       FUNCTION = STR$(hndl)+"  "+HEX$(hndl)
                  END SELECT
              
              END FUNCTION
              
              
              'Show Variable Function
              '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
              FUNCTION ShowVar (Var_label$, Var_value&) AS LONG          'FINISHED
                 STATIC ControlCount&
                 LOCAL i&,CntrlText$,NewLbl$,MatchLbl&,yy&
                 MatchLbl&=0: yy&=ControlCount&*10+22
                 IF  ControlCount& = 0 THEN
              
                  DIALOG NEW 0, "VARIABLE DISPLAY",500,50, 137, yy&+4,%ws_border , TO hVDsp&
                  CONTROL ADD FRAME ,hVDsp&,1000,"Variables",2,1,130,yy&, %ss_sunken,
                  DIALOG SHOW MODELESS hVDsp&, 'CALL Var_Dspl()
              
                 END IF
                     NewLbl$=Var_label$+"   :"
                      FOR i&= 1 TO ControlCount&+1
                        CONTROL GET TEXT hVDsp&,(1100+i&) TO CntrlText$
                           IF CntrlText$= NewLbl$ THEN
              
                               CONTROL SET TEXT hVDsp&,(1200+i&),STR$(Var_value&)
                               MatchLbl&=1
                               EXIT FOR
                           END IF
                      NEXT i&
              
                      IF MatchLbl&=0   THEN
              
                               DIALOG SET SIZE hVDsp&,137,yy&+18
                               CONTROL SET SIZE hVDsp&,1000,130,yy&
                               CONTROL ADD LABEL ,hVDsp&,(1100+i&),NewLbl$,4,i&*10-10,60,10,%SS_RIGHT,
                               CONTROL ADD LABEL ,hVDsp&,(1200+i&),STR$(Var_value&),70,i&*10-10,60,10,,
              
              
                               INCR ControlCount&
                      END IF
              
                    SendMessage hVDsp&,%wm_paint,0,0
              END FUNCTION
              '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
              Last edited by Brad D Byrne; 2 Jan 2008, 08:27 PM. Reason: added note

              Comment


              • #8
                > the MsgBox function isn't a good way to go in terms of
                > debugging windows programs

                Bingo.

                -- Eric
                "Not my circus, not my monkeys."

                Comment


                • #9
                  Eric, I don't think your 'Join Date' could be right? They didn't even have computers that far back, did they?
                  Fred
                  "fharris"+Chr$(64)+"evenlink"+Chr$(46)+"com"

                  Comment


                  • #10
                    Having adjusted the code a little, the original question still remains.
                    Why the change in the global variable?
                    Why not in the local variable as well?
                    HTML Code:
                     #COMPILE EXE
                    #DIM ALL
                    GLOBAL fp AS LONG
                    
                    #INCLUDE "WIN32API.INC"
                    
                    GLOBAL hWndMain AS LONG
                    GLOBAL g_var AS LONG
                    
                    FUNCTION WINMAIN (BYVAL hInstance AS LONG, _
                                      BYVAL hPrevInstance AS LONG, _
                                      BYVAL lpCmdLine AS ASCIIZ PTR, _
                                      BYVAL iCmdShow AS LONG) AS LONG
                    
                      LOCAL Msg AS tagMsg
                      LOCAL w_ndclass AS WndClassEx
                      LOCAL szClassName AS ASCIIZ * 80
                      LOCAL hWnd AS LONG
                      LOCAL hMenu AS LONG
                    
                      fp=FREEFILE
                      OPEN "Output.txt" FOR OUTPUT AS #fp
                      szClassName = "MYPROGRAM32"
                      w_ndclass.cbSize = SIZEOF(W_ndClass)
                      w_ndclass.style = %CS_HREDRAW OR %CS_VREDRAW
                      w_ndclass.lpfnWndProc = CODEPTR(WndProc)
                      w_ndclass.cbClsExtra = 0
                      w_ndclass.cbWndExtra = 0
                      w_ndclass.hInstance = hInstance
                      w_ndclass.hIcon = LoadIcon(hInstance, "PROGRAM")
                      w_ndclass.hCursor = LoadCursor(%NULL, BYVAL %IDC_ARROW)
                      w_ndclass.hbrBackground = GetStockObject(%WHITE_BRUSH) '%WHITE_BRUSH)
                      w_ndclass.lpszMenuName = %NULL
                      w_ndclass.lpszClassName = VARPTR(szClassName)
                      w_ndclass.hIconSm = LoadIcon(hInstance, BYVAL %IDI_APPLICATION)
                    
                      RegisterClassEx w_ndclass
                    
                      hWndMain = CreateWindowEx(0, szClassName, _
                                                "Test", _
                                                %WS_OVERLAPPEDWINDOW, _
                                                100, _
                                                100, _
                                                700, _
                                                400, _
                                                %HWND_DESKTOP, _
                                                hMenu, _
                                                hInstance, _
                                                BYVAL %NULL)
                    
                      ShowWindow hWndMain, iCmdShow
                      UpdateWindow hWndMain
                    
                      WHILE GetMessage(Msg, %NULL, 0, 0)
                        TranslateMessage Msg
                        DispatchMessage Msg
                      WEND
                      CLOSE #fp
                    
                      FUNCTION = msg.wParam
                    
                    END FUNCTION
                    
                    FUNCTION WndProc (BYVAL hWnd AS LONG, BYVAL wMsg AS LONG, _
                                      BYVAL wParam AS LONG, BYVAL lParam AS LONG) EXPORT AS LONG
                      LOCAL l_var AS LONG
                      
                      g_var = 10
                      l_var = 10
                      SELECT CASE wMsg
                    
                        CASE %WM_CREATE
                    
                        g_var = g_var + 10
                        l_var=l_var+10
                        PRINT #fp, wMsg, g_var
                        MSGBOX STR$(g_var),,"1. MSGBOX"
                        PRINT #fp, wMsg, g_var
                        MSGBOX STR$(g_var),,"2. MSGBOX"
                        PRINT #fp, wMsg, g_var
                        PRINT #fp, wMsg, l_var
                        MSGBOX STR$(l_var),,"3. MSGBOX"
                        PRINT #fp, wMsg, l_var
                        MSGBOX STR$(l_var),,"4. MSGBOX"
                        PRINT #fp, wMsg, l_var
                        PostQuitMessage 0
                        FUNCTION = 0
                    
                        EXIT FUNCTION
                    
                      END SELECT
                    
                      FUNCTION = DefWindowProc(hWnd, wMsg, wParam, lParam)
                    
                    END FUNCTION
                    Rod
                    I want not 'not', not Knot, not Knott, not Nott, not knot, not naught, not nought, but aught.

                    Comment


                    • #11
                      Short answer - variable scope.

                      The variable l_var you are displaying in the MSGBOX for WM_CREATE is not the same one that will get changed when WM_ACTIVATEAPP executes in between the MSGBOXs.

                      When WM_ACTIVATEAPP executes a call to WndProc, it will create its own copy of l_var, changing it only there.

                      Run this copy of the code in the debugger and you'll see what I'm talking about:

                      Code:
                       #COMPILE EXE
                      #DIM ALL
                      
                      #INCLUDE "WIN32API.INC"
                      
                      GLOBAL hWndMain AS LONG
                      GLOBAL g_var AS LONG
                      
                      FUNCTION WINMAIN (BYVAL hInstance AS LONG, _
                                        BYVAL hPrevInstance AS LONG, _
                                        BYVAL lpCmdLine AS ASCIIZ PTR, _
                                        BYVAL iCmdShow AS LONG) AS LONG
                      
                        LOCAL Msg AS tagMsg
                        LOCAL w_ndclass AS WndClassEx
                        LOCAL szClassName AS ASCIIZ * 80
                        LOCAL hWnd AS LONG
                        LOCAL hMenu AS LONG
                      
                      
                        szClassName = "MYPROGRAM32"
                        w_ndclass.cbSize = SIZEOF(W_ndClass)
                        w_ndclass.style = %CS_HREDRAW OR %CS_VREDRAW
                        w_ndclass.lpfnWndProc = CODEPTR(WndProc)
                        w_ndclass.cbClsExtra = 0
                        w_ndclass.cbWndExtra = 0
                        w_ndclass.hInstance = hInstance
                        w_ndclass.hIcon = LoadIcon(hInstance, "PROGRAM")
                        w_ndclass.hCursor = LoadCursor(%NULL, BYVAL %IDC_ARROW)
                        w_ndclass.hbrBackground = GetStockObject(%WHITE_BRUSH) '%WHITE_BRUSH)
                        w_ndclass.lpszMenuName = %NULL
                        w_ndclass.lpszClassName = VARPTR(szClassName)
                        w_ndclass.hIconSm = LoadIcon(hInstance, BYVAL %IDI_APPLICATION)
                      
                        RegisterClassEx w_ndclass
                      
                        hWndMain = CreateWindowEx(0, szClassName, _
                                                  "Test", _
                                                  %WS_OVERLAPPEDWINDOW, _
                                                  100, _
                                                  100, _
                                                  700, _
                                                  400, _
                                                  %HWND_DESKTOP, _
                                                  hMenu, _
                                                  hInstance, _
                                                  BYVAL %NULL)
                      
                        ShowWindow hWndMain, iCmdShow
                        UpdateWindow hWndMain
                      
                        WHILE GetMessage(Msg, %NULL, 0, 0)
                          TranslateMessage Msg
                          DispatchMessage Msg
                        WEND
                      
                        FUNCTION = msg.wParam
                      
                      END FUNCTION
                      
                      FUNCTION WndProc (BYVAL hWnd AS LONG, BYVAL wMsg AS LONG, _
                                        BYVAL wParam AS LONG, BYVAL lParam AS LONG) EXPORT AS LONG
                        LOCAL l_var AS LONG
                      
                        g_var = 10
                        l_var = 10
                        SELECT CASE wMsg
                      
                          CASE %WM_CREATE
                      
                              g_var = g_var + 10
                              l_var=l_var + 10
                              MSGBOX STR$(g_var),,"1. MSGBOX"
                              MSGBOX STR$(g_var),,"2. MSGBOX"
                              MSGBOX STR$(l_var),,"3. MSGBOX"
                              MSGBOX STR$(l_var),,"4. MSGBOX"
                              PostQuitMessage 0
                              FUNCTION = 0
                              EXIT FUNCTION
                      
                          CASE %WM_ACTIVATEAPP
                              
                              l_var=l_var + 55
                              #DEBUG PRINT FORMAT$(l_var)
                      
                        END SELECT
                      
                        FUNCTION = DefWindowProc(hWnd, wMsg, wParam, lParam)
                      
                      END FUNCTION
                      Adam Drake
                      Drake Software

                      Comment


                      • #12
                        Hoping to help - the following sample should illustrate what is happening behind the scenes with WndProc (run in debugger):

                        Code:
                        #COMPILE EXE
                        #DIM ALL
                        
                        GLOBAL g_var    AS LONG
                        
                        FUNCTION PBMAIN () AS LONG
                        
                            CALL WndProc("WM_CREATE")
                        
                        END FUNCTION
                        
                        SUB WndProc(msgtext AS STRING)
                        
                            LOCAL l_var AS LONG
                        
                            g_Var = 10
                        
                            IF msgtext = "WM_CREATE" THEN
                        
                                l_Var = l_Var + 10
                                g_Var = g_Var + 10
                                
                                #DEBUG PRINT msgtext + " l_var: "+FORMAT$(l_var) + " g_var: "+FORMAT$(g_Var)
                                
                                CALL WndProc("WM_ACTIVATEAPP")
                                
                                #DEBUG PRINT msgtext + " l_var: "+FORMAT$(l_var) + " g_var: "+FORMAT$(g_Var)
                                
                            ELSE
                                
                                #DEBUG PRINT msgtext + " l_var: "+FORMAT$(l_var) + " g_var: "+FORMAT$(g_Var)
                                
                            END IF
                                
                        
                        END SUB
                        Adam Drake
                        Drake Software

                        Comment


                        • #13
                          Adam,
                          The first program hung my machine, which I'll investigate later.
                          However, what you're implying is some sort of recursion, it appears to me, as a penalty for using the MSGBOX.
                          The act of calling the MSGBOX should not affect the value of any variable, regardless of scope.
                          There would be very little point in having such a thing as a MSGBOX if this were the case.

                          The following code shows that the global variable is re-initialized even though it is not the subject of the message box. That the use of MSGBOX statements or functions would re-initialize global variables should be noted in the manual, I would think. I would also think that the input box should be checked out in this regard.

                          This may be a characteristic of where in the code the MSGBOX is used, i.e. handling the %WM_CREATE message as opposed to others.

                          You may be right about why the global variable is reset, but something sure seems wrong about it happening.

                          HTML Code:
                           #COMPILE EXE
                          #DIM ALL
                          GLOBAL fp AS LONG
                          
                          #INCLUDE "WIN32API.INC"
                          
                          GLOBAL hWndMain AS LONG
                          GLOBAL g_var AS LONG
                          
                          FUNCTION WINMAIN (BYVAL hInstance AS LONG, _
                                            BYVAL hPrevInstance AS LONG, _
                                            BYVAL lpCmdLine AS ASCIIZ PTR, _
                                            BYVAL iCmdShow AS LONG) AS LONG
                          
                            LOCAL Msg AS tagMsg
                            LOCAL w_ndclass AS WndClassEx
                            LOCAL szClassName AS ASCIIZ * 80
                            LOCAL hWnd AS LONG
                            LOCAL hMenu AS LONG
                          
                            fp=FREEFILE
                            OPEN "Output.txt" FOR OUTPUT AS #fp
                            szClassName = "MYPROGRAM32"
                            w_ndclass.cbSize = SIZEOF(W_ndClass)
                            w_ndclass.style = %CS_HREDRAW OR %CS_VREDRAW
                            w_ndclass.lpfnWndProc = CODEPTR(WndProc)
                            w_ndclass.cbClsExtra = 0
                            w_ndclass.cbWndExtra = 0
                            w_ndclass.hInstance = hInstance
                            w_ndclass.hIcon = LoadIcon(hInstance, "PROGRAM")
                            w_ndclass.hCursor = LoadCursor(%NULL, BYVAL %IDC_ARROW)
                            w_ndclass.hbrBackground = GetStockObject(%WHITE_BRUSH) '%WHITE_BRUSH)
                            w_ndclass.lpszMenuName = %NULL
                            w_ndclass.lpszClassName = VARPTR(szClassName)
                            w_ndclass.hIconSm = LoadIcon(hInstance, BYVAL %IDI_APPLICATION)
                          
                            RegisterClassEx w_ndclass
                          
                            hWndMain = CreateWindowEx(0, szClassName, _
                                                      "Test", _
                                                      %WS_OVERLAPPEDWINDOW, _
                                                      100, _
                                                      100, _
                                                      700, _
                                                      400, _
                                                      %HWND_DESKTOP, _
                                                      hMenu, _
                                                      hInstance, _
                                                      BYVAL %NULL)
                          
                            ShowWindow hWndMain, iCmdShow
                            UpdateWindow hWndMain
                          
                            WHILE GetMessage(Msg, %NULL, 0, 0)
                              TranslateMessage Msg
                              DispatchMessage Msg
                            WEND
                            CLOSE #fp
                          
                            FUNCTION = msg.wParam
                          
                          END FUNCTION
                          
                          FUNCTION WndProc (BYVAL hWnd AS LONG, BYVAL wMsg AS LONG, _
                                            BYVAL wParam AS LONG, BYVAL lParam AS LONG) EXPORT AS LONG
                            LOCAL l_var AS LONG
                            g_var = 10
                            l_var = 10
                            SELECT CASE wMsg
                          
                              CASE %WM_CREATE
                              
                              g_var = g_var + 10
                              l_var = l_var + 10
                              
                              MSGBOX STR$(l_var),,"1. MSGBOX"
                             
                              MSGBOX STR$(l_var),,"2. MSGBOX"
                              
                              g_var = g_var + 10
                              
                              MSGBOX STR$(g_var),,"3. MSGBOX"
                              
                              MSGBOX STR$(g_var),,"4. MSGBOX"
                              
                              PostQuitMessage 0
                              FUNCTION = 0
                          
                              EXIT FUNCTION
                          
                            END SELECT
                          
                            FUNCTION = DefWindowProc(hWnd, wMsg, wParam, lParam)
                          
                          END FUNCTION
                          Rod
                          I want not 'not', not Knot, not Knott, not Nott, not knot, not naught, not nought, but aught.

                          Comment


                          • #14
                            You may be right about why the global variable is reset, but something sure seems wrong about it happening.
                            He *is* right, and there is nothing wrong... MSGBOX is working 'as designed'

                            I think you could get the efffect you are after (a messagebox which blocks execution of the thread until dismissed) by using the %MB_TASKMODAL style in your MSGBOX statement.

                            But MSGBOX is IMO the wrong tool to try to debug "message processing." It's fine for other debugging when there is no possibility of re-entrancy at the point of call.

                            You can try using the %MB_TASKMODAL style, but in the long run you will do better to learn to use TRACE or STDOUT or some other facility for "message processing" debugging.

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

                            Comment


                            • #15
                              I havn't looked at it again since late yesterday afternoon, but my original take on it was that various invocations of the WndProc were existing on the stack. The first MsgBox() call blocked, but Windows continued firing messages at WndProc - messages which didn't block and were processed, and each call affected the global.
                              Fred
                              "fharris"+Chr$(64)+"evenlink"+Chr$(46)+"com"

                              Comment


                              • #16
                                but my original take on it was that various invocations of the WndProc were existing on the stack...
                                Give that man one of the big stuffed teddy bears for his little lady friend.
                                Michael Mattias
                                Tal Systems (retired)
                                Port Washington WI USA
                                [email protected]
                                http://www.talsystems.com

                                Comment


                                • #17
                                  What if MSGBOX is used for some other purpose besides debugging? Should variables be reset then?

                                  I did not discover this particular nuance, someone else started this thread. I have always used %MB_TASKMODAL with MSGBOX .
                                  I also do not use MSGBOX for debugging. (I don't put bugs in my programs:whistle
                                  I still don't think that the value of the variable should be changed when MSGBOX is used.
                                  I do use MSGBOX for verification of user input and I would not want it changing values.
                                  Rod
                                  I want not 'not', not Knot, not Knott, not Nott, not knot, not naught, not nought, but aught.

                                  Comment


                                  • #18
                                    Your best bet would be to not reset any variable outside of a specific select case inside of WndProc. If you want to alter g_var for WM_CREATE - do so there only. Modifying it at the beginning of a window procedure will modify it for every message that gets sent to the window procedure, without regard to whether or not one has finished executing.

                                    MB_TASKMODAL would have absolutely zero effect in what you are seeing here because as soon as the first MSGBOX disappears, WM_ACTIVATEAPP executes since the window is for just a millisecond being activated before the next MSGBOX appears.
                                    Adam Drake
                                    Drake Software

                                    Comment


                                    • #19
                                      > still don't think that the value of the variable should be changed when MSGBOX is used
                                      >I do use MSGBOX for verification of user input and I would not want it changing values

                                      MSGBOX is not changing values. A re-entrant execution of your dialog procedure is.

                                      i.e., your problem is not with MSGBOX, it's your lack of understanding of how Windows' GUI programs work and how that affects GLOBAL and/or STATIC variables you choose to use.

                                      For the most part, MSGBOX will effectively suspend execution of the current thread until dismissed as far as user input-generated notification messages is concerned; however, when processing system-generated notification messages (eg WM_CREATE or WM_INITDIALOG) that will not always be true.

                                      In your case the simplest solution is to quit updating that GLOBAL variable EXCEPT when processing a specific notification message.


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

                                      Comment


                                      • #20
                                        Michael & Adam are absolutely right on the money here. The program below shows that neither MB_TASKMODAL nor MB_SYSTEMMODAL help at all. These flags effectively prevent you from interacting with your program, in that all your windows are disabled in terms of you're further input to them. However, Windows still keeps its messaging machinery running and your window procedure continually get bombarded with various system messages, each one resetting g_var back to 10.

                                        If you really insist on using message boxes to debug your programs, you'll be OK so long as you don't have other message handlers that could be called that may change global variables you are outputting to a MsgBox.

                                        There is absolutely nothing unusual going on here, as I said before. Even for this totally crazy setup, the program, Windows, and PowerBASIC are working to perfection and with a vengence.

                                        Code:
                                        #Compile Exe
                                        #Dim All
                                        #Include "WIN32API.INC"
                                        Global g_var As Long
                                        Global fp As Long
                                        
                                        Function WndProc(Byval hWnd As Long, Byval wMsg As Long,Byval wParam As Long, Byval lParam As Long) As Long
                                          g_var = 10
                                          Select Case wMsg
                                            Case %WM_GETMINMAXINFO
                                              Print #fp, wMsg;Tab(10)"%WM_GETMINMAXINFO";Tab(40)g_var
                                            Case %WM_NCCREATE
                                              Print #fp, wMsg;Tab(10)"%WM_NCCREATE";Tab(40)g_var
                                            Case %WM_NCCALCSIZE
                                              Print #fp, wMsg;Tab(10)"%WM_NCCALCSIZE";Tab(40)g_var
                                            Case %WM_ACTIVATEAPP
                                              Print #fp, wMsg;Tab(10)"%WM_ACTIVATEAPP";Tab(40)g_var
                                            Case %WM_CREATE
                                              g_var=g_var+10
                                              'Print #fp,wMsg;Tab(10)"%WM_CREATE";Tab(40)g_var
                                              MSGBOX STR$(g_var),%MB_SYSTEMMODAL,"1. MSGBOX"
                                              Print #fp,wMsg;Tab(10)"%WM_CREATE";Tab(40)g_var
                                              MSGBOX STR$(g_var),%MB_SYSTEMMODAL,"2. MSGBOX"
                                              PostQuitMessage 0
                                              Function = 0
                                              Exit Function
                                            Case %WM_SHOWWINDOW
                                              Print #fp, wMsg;Tab(10)"%WM_SHOWWINDOW";Tab(40)g_var
                                            Case %WM_MOVE
                                              Print #fp, wMsg;Tab(10)"%WM_MOVE";Tab(40)g_var
                                            Case %WM_WINDOWPOSCHANGING
                                              Print #fp, wMsg;Tab(10)"%WM_WINDOWPOSCHANGING";Tab(40)g_var
                                            Case %WM_WINDOWPOSCHANGED
                                              Print #fp, wMsg;Tab(10)"%WM_WINDOWPOSCHANGED";Tab(40)g_var
                                            Case %WM_SIZE
                                              Print #fp, wMsg;Tab(10)"%WM_SIZE"; Tab(40)g_var
                                            Case %WM_NCACTIVATE
                                              Print #fp, wMsg;Tab(10)"%WM_NCACTIVATE"; Tab(40)g_var
                                            Case Else
                                              Print #fp, wMsg;Tab(10)"%UNKNOWN";Tab(40)g_var
                                          End Select
                                        
                                          Function = DefWindowProc(hWnd, wMsg, wParam, lParam)
                                        End Function
                                        
                                        
                                        Function WinMain (Byval hIns As Long,Byval hPrev As Long,Byval lpCmdLn As Asciiz Ptr,Byval iCmdShow As Long) As Long
                                          LOCAL w_ndclass As WndClassEx
                                          LOCAL szClassName As Asciiz*80
                                          LOCAL Msg As tagMsg
                                          LOCAL hWnd As Long
                                          LOCAL hMenu As Long
                                        
                                          fp=freefile
                                          Open "Output.txt" For Output As #fp
                                          szClassName = "MYPROGRAM32"
                                          w_ndclass.cbSize = SIZEOF(W_ndClass)
                                          w_ndclass.style = %CS_HREDRAW OR %CS_VREDRAW
                                          w_ndclass.lpfnWndProc = CODEPTR(WndProc)
                                          w_ndclass.cbClsExtra = 0
                                          w_ndclass.cbWndExtra = 0
                                          w_ndclass.hInstance = hIns
                                          w_ndclass.hIcon = LoadIcon(hIns, "PROGRAM")
                                          w_ndclass.hCursor = LoadCursor(%NULL, Byval %IDC_ARROW)
                                          w_ndclass.hbrBackground = GetStockObject(%WHITE_BRUSH) '%WHITE_BRUSH)
                                          w_ndclass.lpszMenuName = %NULL
                                          w_ndclass.lpszClassName = VARPTR(szClassName)
                                          w_ndclass.hIconSm = LoadIcon(hIns, Byval %IDI_APPLICATION)
                                          Call RegisterClassEx(w_ndclass)
                                          hWnd=CreateWindowEx(0,szClassName,"Test",%WS_OVERLAPPEDWINDOW,100,100,700,400,0,hMenu,hIns,Byval %NULL)
                                          ShowWindow hWnd, iCmdShow
                                          UpdateWindow hWnd
                                          WHILE GetMessage(Msg, %NULL, 0, 0)
                                            TranslateMessage Msg
                                            DispatchMessage Msg
                                          WEND
                                          Close #fp
                                          Shell "Notepad.exe " & "Output.txt", 1
                                        
                                          Function = msg.wParam
                                        End Function
                                        Code:
                                        'wMsg     wMsg                           g_var
                                        '=============================================
                                        ' 36      %WM_GETMINMAXINFO              10
                                        ' 129     %WM_NCCREATE                   10
                                        ' 131     %WM_NCCALCSIZE                 10
                                        ' 28      %WM_ACTIVATEAPP                10
                                        ' 1       %WM_CREATE                     10
                                        ' 28      %WM_ACTIVATEAPP                10
                                        ' 24      %WM_SHOWWINDOW                 10
                                        ' 70      %WM_WINDOWPOSCHANGING          10
                                        ' 70      %WM_WINDOWPOSCHANGING          10
                                        ' 28      %WM_ACTIVATEAPP                10
                                        ' 134     %WM_NCACTIVATE                 10
                                        ' 13      %UNKNOWN                       10
                                        ' 6       %UNKNOWN                       10
                                        ' 7       %UNKNOWN                       10
                                        ' 133     %UNKNOWN                       10
                                        ' 13      %UNKNOWN                       10
                                        ' 20      %UNKNOWN                       10
                                        ' 71      %WM_WINDOWPOSCHANGED           10
                                        ' 5       %WM_SIZE                       10
                                        ' 3       %WM_MOVE                       10
                                        ' 15      %UNKNOWN                       10
                                        '
                                        '
                                        Fred
                                        "fharris"+Chr$(64)+"evenlink"+Chr$(46)+"com"

                                        Comment

                                        Working...
                                        X