• Welcome to Theos PowerBasic Museum 2017.

News:

Attachments are only available to registered users.
Please register using your full, real name.

Main Menu

Problem with Performance Counters

Started by Bud Meyer, August 10, 2009, 02:24:13 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Bud Meyer

I've noticed a small issue when browsing for performance counters on Vista (64-bit). When hWndOwner is not %NULL, the selected object is not being returned as it should be. I tried running the exe as admin and with XP compatibility mode, but it doesn't make a difference.

On XP, it doesn't matter what hWndOwner is. It works either way.

Anyone have any ideas?

#COMPILE EXE
#DIM ALL
#INCLUDE "PDH.INC"

FUNCTION PBMAIN () AS LONG
    LOCAL hDlg AS DWORD
    DIALOG NEW %HWND_DESKTOP, "fails unless hWndOwner = %NULL", 100, 100, 160, 65, %WS_POPUP OR %WS_CAPTION OR %WS_SYSMENU OR %WS_VISIBLE, , TO hDlg
    CONTROL ADD CHECKBOX, hDlg, 100, "Set PDH.hWndOwner = %NULL", 25, 10, 125, 12
    CONTROL ADD BUTTON, hDlg, 200, "Click Me", 25, 30, 100, 25
    DIALOG SHOW MODAL hDlg, CALL DialogProc
END FUNCTION

CALLBACK FUNCTION DialogProc ()
    LOCAL PDH AS PDH_BROWSE_DLG_CONFIG_A
    LOCAL ReturnPathBuffer AS ASCIIZ * %PDH_MAX_COUNTER_PATH
    LOCAL ReturnPathLen AS DWORD
    LOCAL captionstr AS ASCIIZ * %MAX_PATH
    LOCAL checkresult AS LONG
   
    SELECT CASE AS LONG CBMSG
        CASE %WM_COMMAND
            SELECT CASE AS LONG CBCTL
                CASE 200
                    IF CBCTLMSG = %BN_CLICKED OR CBCTLMSG = 1 THEN
                        CONTROL GET CHECK CB.HNDL, 100 TO checkresult

                        PDH.bIncludeInstanceIndex    = %FALSE
                        PDH.bSingleCounterPerAdd     = %TRUE
                        PDH.bSingleCounterPerDialog  = %TRUE
                        PDH.bLocalCountersOnly       = %TRUE
                        PDH.bWildCardInstances       = %FALSE
                        PDH.bHideDetailBox           = %FALSE
                        PDH.bInitializePath          = %FALSE
                        PDH.bDisableMachineSelection = %TRUE
                        PDH.bIncludeCostlyObjects    = %FALSE
                        PDH.bShowObjectBrowser       = %FALSE
                        PDH.bReserved                = 0
                        PDH.hWndOwner                = IIF&(checkresult, %NULL, CB.HNDL) '<-- NULL works; CB.HNDL doesn't work
                        PDH.szDataSource             = %NULL
                        PDH.szReturnPathBuffer       = VARPTR(ReturnPathBuffer)
                        PDH.cchReturnPathLength      = %PDH_MAX_COUNTER_PATH 'ReturnPathLen
                        PDH.pCallBack                = %NULL
                        PDH.dwCallBackArg            = 0
                        PDH.CallBackStatus           = %NULL
                        PDH.dwDefaultDetailLevel     = %PERF_DETAIL_ADVANCED
                        captionstr = "Choose something and click OK"
                        PDH.szDialogBoxCaption       = VARPTR(captionstr)

                        IF PdhBrowseCounters(PDH) = %ERROR_SUCCESS THEN
                            ? LEFT$(ReturnPathBuffer, PDH.cchReturnPathLength)
                        END IF

                    END IF

            END SELECT

    END SELECT

END FUNCTION



Bud Meyer

#1
Another odd thing is when specifying %NULL for the caption:
PDH.szDialogBoxCaption = %NULL 'VARPTR(captionstr)

MSDN says %NULL will automatically make the caption be "Browse Performance Counters", which it does on XP, but on Vista, it's just "s". I'm guessing there's an issue with wchars/unicode, or maybe with how PDH_BROWSE_DLG_CONFIG_A is defined, but I don't know how to fix it.

José Roca

 
I don't have Windows Vista, but I have tested the window owner handle issue and it works with XP if you use a SDK windows instead of a DDT dialog, so it could be a DDT issue. Try the following SDK test in your computer to see if it works:


#COMPILE EXE
#DIM ALL
#DEBUG ERROR ON
#INCLUDE "PDH.INC"

' ========================================================================================
' Main
' ========================================================================================
FUNCTION WinMain (BYVAL hInstance AS DWORD, BYVAL hPrevInstance AS DWORD, BYVAL lpszCmdLine AS ASCIIZ PTR, BYVAL nCmdShow AS LONG) AS LONG

   LOCAL hWndMain    AS DWORD
   LOCAL hCtl        AS DWORD
   LOCAL hFont       AS DWORD
   LOCAL wcex        AS WNDCLASSEX
   LOCAL szClassName AS ASCIIZ * 80
   LOCAL rc          AS RECT
   LOCAL szCaption   AS ASCIIZ * 255
   LOCAL nLeft       AS LONG
   LOCAL nTop        AS LONG
   LOCAL nWidth      AS LONG
   LOCAL nHeight     AS LONG

   hFont = GetStockObject(%ANSI_VAR_FONT)

   ' Register the window class
   szClassName        = "MyClassName"
   wcex.cbSize        = SIZEOF(wcex)
   wcex.style         = %CS_HREDRAW OR %CS_VREDRAW
   wcex.lpfnWndProc   = CODEPTR(WndProc)
   wcex.cbClsExtra    = 0
   wcex.cbWndExtra    = 0
   wcex.hInstance     = hInstance
   wcex.hCursor       = LoadCursor (%NULL, BYVAL %IDC_ARROW)
   wcex.hbrBackground = %COLOR_3DFACE + 1
   wcex.lpszMenuName  = %NULL
   wcex.lpszClassName = VARPTR(szClassName)
   wcex.hIcon         = LoadIcon (%NULL, BYVAL %IDI_APPLICATION) ' Sample, if resource icon: LoadIcon(hInst, "APPICON")
   wcex.hIconSm       = LoadIcon (%NULL, BYVAL %IDI_APPLICATION) ' Remember to set small icon too..
   RegisterClassEx wcex

   ' Window caption
   szCaption = "Test PdhBrowseCounters Function"

   ' Retrieve the size of the working area
   SystemParametersInfo %SPI_GETWORKAREA, 0, BYVAL VARPTR(rc), 0

   ' Calculate the position and size of the window
   nWidth  = (((rc.nRight - rc.nLeft)) + 2) * 0.75   ' 75% of the client screen width
   nHeight = (((rc.nBottom - rc.nTop)) + 2) * 0.70   ' 70% of the client screen height
   nLeft   = ((rc.nRight - rc.nLeft) \ 2) - nWidth \ 2
   nTop    = ((rc.nBottom - rc.nTop) \ 2) - (nHeight \ 2)

   ' Create a window using the registered class
   hWndMain = CreateWindowEx(%WS_EX_CONTROLPARENT, _           ' extended style
                             szClassName, _                    ' window class name
                             szCaption, _                      ' window caption
                             %WS_OVERLAPPEDWINDOW OR _
                             %WS_CLIPCHILDREN, _               ' window styles
                             nLeft, _                          ' initial x position
                             nTop, _                           ' initial y position
                             nWidth, _                         ' initial x size
                             nHeight, _                        ' initial y size
                             %NULL, _                          ' parent window handle
                             0, _                              ' window menu handle
                             hInstance, _                      ' program instance handle
                             BYVAL %NULL)                      ' creation parameters

   hCtl = CreateWindowEx(0, "BUTTON", "&Ok", %WS_CHILD OR %WS_VISIBLE OR %WS_TABSTOP OR %BS_FLAT, _
          0, 0, 0, 0, hWndMain, %IDOK, hInstance, BYVAL %NULL)
   IF hFont THEN SendMessage hCtl, %WM_SETFONT, hFont, 0

   hCtl = CreateWindowEx(0, "BUTTON", "&Close", %WS_CHILD OR %WS_VISIBLE OR %WS_TABSTOP OR %BS_FLAT, _
          0, 0, 0, 0, hWndMain, %IDCANCEL, hInstance, BYVAL %NULL)
   IF hFont THEN SendMessage hCtl, %WM_SETFONT, hFont, 0

   ' Show the window
   ShowWindow hWndMain, nCmdShow
   UpdateWindow hWndMain

   ' Message handler loop
   LOCAL uMsg AS tagMsg
   WHILE GetMessage(uMsg, %NULL, 0, 0)
      IF ISFALSE IsDialogMessage(hWndMain, uMsg) THEN
         TranslateMessage uMsg
         DispatchMessage uMsg
      END IF
   WEND

   FUNCTION = uMsg.wParam

END FUNCTION
' ========================================================================================

' ========================================================================================
' Main Window procedure
' ========================================================================================
FUNCTION WndProc (BYVAL hWnd AS DWORD, BYVAL wMsg AS DWORD, BYVAL wParam AS DWORD, BYVAL lParam AS LONG) AS LONG

   LOCAL rc AS RECT

   SELECT CASE wMsg

      CASE %WM_SIZE
         ' Resize the two sample buttons of the dialog
         IF wParam <> %SIZE_MINIMIZED THEN
            GetClientRect hWnd, rc
            MoveWindow GetDlgItem(hWnd, %IDOK), (rc.nRight - rc.nLeft) - 185, (rc.nBottom - rc.nTop) - 35, 75, 23, %TRUE
            MoveWindow GetDlgItem(hWnd, %IDCANCEL), (rc.nRight - rc.nLeft) - 95, (rc.nBottom - rc.nTop) - 35, 75, 23, %TRUE
         END IF

      CASE %WM_COMMAND
         ' -------------------------------------------------------
         ' Messages from controls and menu items are handled here.
         ' -------------------------------------------------------
         SELECT CASE LO(WORD, wParam)

            CASE %IDOK
               IF HI(WORD, wParam) = %BN_CLICKED THEN

                  LOCAL PDH AS PDH_BROWSE_DLG_CONFIG_A
                  LOCAL ReturnPathBuffer AS ASCIIZ * %PDH_MAX_COUNTER_PATH
                  LOCAL ReturnPathLen AS DWORD
                  LOCAL captionstr AS ASCIIZ * %MAX_PATH

                  PDH.bIncludeInstanceIndex    = %FALSE
                  PDH.bSingleCounterPerAdd     = %TRUE
                  PDH.bSingleCounterPerDialog  = %TRUE
                  PDH.bLocalCountersOnly       = %TRUE
                  PDH.bWildCardInstances       = %FALSE
                  PDH.bHideDetailBox           = %FALSE
                  PDH.bInitializePath          = %FALSE
                  PDH.bDisableMachineSelection = %TRUE
                  PDH.bIncludeCostlyObjects    = %FALSE
                  PDH.bShowObjectBrowser       = %FALSE
                  PDH.bReserved                = 0
                  PDH.hWndOwner                = hwnd 'IIF&(checkresult, %NULL, CB.HNDL) '<-- NULL works; CB.HNDL doesn't work
                  PDH.szDataSource             = %NULL
                  PDH.szReturnPathBuffer       = VARPTR(ReturnPathBuffer)
                  PDH.cchReturnPathLength      = %PDH_MAX_COUNTER_PATH 'ReturnPathLen
                  PDH.pCallBack                = %NULL
                  PDH.dwCallBackArg            = 0
                  PDH.CallBackStatus           = %NULL
                  PDH.dwDefaultDetailLevel     = %PERF_DETAIL_ADVANCED
                  captionstr = "Choose something and click OK"
                  PDH.szDialogBoxCaption       = VARPTR(captionstr)

                  IF PdhBrowseCounters(PDH) = %ERROR_SUCCESS THEN
                      ? LEFT$(ReturnPathBuffer, PDH.cchReturnPathLength)
                  END IF

               END IF

            CASE %IDCANCEL
               IF HI(WORD, wParam) = %BN_CLICKED THEN
                  SendMessage hWnd, %WM_CLOSE, 0, 0
                  EXIT FUNCTION
               END IF

         END SELECT

      CASE %WM_SYSCOMMAND
         ' Capture this message and send a WM_CLOSE message
         IF (wParam AND &HFFF0) = %SC_CLOSE THEN
            SendMessage hWnd, %WM_CLOSE, 0, 0
            EXIT FUNCTION
         END IF

      CASE %WM_DESTROY
         PostQuitMessage 0
         EXIT FUNCTION

   END SELECT

   FUNCTION = DefWindowProc(hWnd, wMsg, wParam, lParam)

END FUNCTION
' ========================================================================================


Edwin Knoppert

Jose, why do you use the code under %WM_SYSCOMMAND ?


Bud Meyer

Thanks for looking, José, but the SDK window produces the same result: On Vista, nothing is returned when a non-null owner is used, and the caption text is "s" when there's a null caption (mentioned in my 2nd post). On XP, everything works fine.

José Roca

Quote from: Edwin Knoppert on August 10, 2009, 05:48:23 PM
Jose, why do you use the code under %WM_SYSCOMMAND ?

It is not needed here, but it is needed with certain OCXs that, otherwise, remain in memory when you close the dialog using the X button.

Edwin Knoppert

Do
Quote from: José Roca on August 10, 2009, 05:55:12 PM
Quote from: Edwin Knoppert on August 10, 2009, 05:48:23 PM
Jose, why do you use the code under %WM_SYSCOMMAND ?

It is not needed here, but it is needed with certain OCXs that, otherwise, remain in memory when you close the dialog using the X button.

Do you have an example of such a control?
Like a VB6 ocx or so?
I would like to test that.
Thanks,

José Roca

 
I had this problem with certain VB6 OCXs when used with SDK (not problem with DDT). If I remember well, one of them was the MS Flex Grid and/or the MS Hierarchical Flex Grid.