Announcement

Collapse
No announcement yet.

FrostCrashedWindow

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

  • FrostCrashedWindow

    Undocumented FrostCrashedWindow replaces a window with a ghosted "hung" version that cannot be interacted with.

    For the fun of it, more work to do, but give a little idea of what's under the hood.

    ' You can create ghost class windows using "Ghost" as the class name to CreateWindow(Ex).
    ' If a window is specified as hwndErrorReportingOwner, it will be sent the registered "WerHangRepMessage" message,
    ' flashed, and brought to the foreground whenever there is a click on the replacement window,
    ' just like the standard "This window is not responding" dialog

    From airesoft

    Tested Windows Vista/32 ok, 7/64 ok, 8.1/64 and 10/64 not working yet.
    Have fun,
    Pierre

    Code:
    #COMPILE EXE '#Win 8.04#
    #DIM ALL
    #INCLUDE "Win32Api.inc"
     
    DECLARE FUNCTION FrostCrashedWindow LIB "User32.dll" ALIAS "FrostCrashedWindow" _ 'Aka NtUserFrostCrashedWindow
    (BYVAL hWindow AS DWORD, OPTIONAL BYVAL hGhostClass AS DWORD)AS LONG 'Vista+
     
    TYPE HandleGroup
     Dlg     AS DWORD
     Ghost   AS DWORD
     Frosted AS DWORD
     WinVer  AS SINGLE
    END TYPE
     
    DECLARE FUNCTION RtlGetVersion LIB "NtDll.dll" ALIAS "RtlGetVersion" _
    (OsVersionInformation AS ANY) AS LONG 'RtlGetVersion returns 0-STATUS_SUCCESS.
     
    TYPE RTL_OSVERSIONINFOW
     dwOSVersionInfoSize AS DWORD
     dwMajorVersion      AS DWORD
     dwMinorVersion      AS DWORD
     dwBuildNumber       AS DWORD
     dwPlatformId        AS DWORD
     szCSDVersion        AS STRING * 256
    END TYPE
     
    %LabelMainFrost    = 1101
    %LabelMainSpec     = 1102
    %ButtonMainFrost   = 1201
     
    %LabelGhostInfo    = 2101
    %LabelGhostHandle  = 2102
    %LabelGhostMessage = 2103
    %ButtonGhostClose  = 2201
    '______________________________________________________________________________
     
    FUNCTION GhostProc(BYVAL hWnd AS DWORD, BYVAL uMsg AS DWORD, BYVAL wParam AS DWORD, BYVAL lParam AS LONG) AS LONG
     LOCAL  OsVerInfoEx       AS RTL_OSVERSIONINFOW
     STATIC h                 AS HandleGroup POINTER
     STATIC WERHANGREPMESSAGE AS WORD
     
     SELECT CASE uMsg
     
       CASE %WM_APP
         h = wParam
         SetWindowText(GetDlgItem(hWnd, %LabelGhostHandle), "Ghost window handle is " & HEX$(@h.Frosted, 8))
     
         OsVerInfoEx.dwOSVersionInfoSize = SIZEOF(OsVerInfoEx)
         RtlGetVersion(OsVerInfoEx)
         @h.WinVer = OsVerInfoEx.dwMajorVersion + OsVerInfoEx.dwMinorVersion / 10
         SELECT CASE @h.WinVer
           CASE  6.0 : WERHANGREPMESSAGE = 49281 'Vista/32
           CASE  6.1 : WERHANGREPMESSAGE = 49288 'Seven/64
           CASE  6.2 : WERHANGREPMESSAGE = 49000 'Windows 8   <<<not found yet!
           CASE  6.3 : WERHANGREPMESSAGE = 49000 'Windows 8.1 <<<not found yet!
           CASE 10.0 : WERHANGREPMESSAGE = 49000 'Windows 10  <<<not found yet!
         END SELECT
     
       CASE WERHANGREPMESSAGE
         SetWindowText(GetDlgItem(hWnd, %LabelGhostMessage), "Received %WerHangRepMessage at " & TIME$)
     
       CASE %WM_COMMAND
         SELECT CASE LOWRD(wParam)
           CASE %ButtonGhostClose
             IF HIWRD(wParam) = %BN_CLICKED THEN
               PostMessage(@h.Dlg, %WM_SYSCOMMAND, %SC_CLOSE, %NULL)
             END IF
         END SELECT
     
     
       CASE %WM_SYSCOMMAND
         IF (wParam AND &HFFF0) = %SC_CLOSE THEN
           IF MessageBox(hWnd, "Exit without closing crashed program?" & $CRLF & "", _
                         "FrostCrashedWindow", %MB_OKCANCEL OR %MB_TOPMOST) = %IDCANCEL THEN
             FUNCTION = 1 'App won't terminate
             EXIT FUNCTION
           END IF
         END IF
     
       CASE %WM_DESTROY
         PostQuitMessage(0)
     
     END SELECT
     
     FUNCTION = DefWindowProc(hWnd, uMsg, wParam, lParam)
     
    END FUNCTION
    '______________________________________________________________________________
     
    #IF ((%DEF(%PB_WIN32)) AND (%PB_REVISION > &H0805))
    THREAD FUNCTION MainGhost(BYVAL h AS HandleGroup POINTER) AS LONG
    #ELSE
    FUNCTION MainGhost(BYVAL h AS HandleGroup POINTER) AS LONG
    #ENDIF
     LOCAL zClassName  AS ASCIIZ * %MAX_PATH
     LOCAL ClassInfo   AS WNDCLASSEX
     LOCAL tmsg        AS TAGMSG
     LOCAL WindowSize  AS SIZEL
     LOCAL hWnd        AS DWORD
     LOCAL hFont       AS DWORD
     LOCAL hWndChild   AS DWORD
     LOCAL hInstance   AS DWORD
     
     hInstance               = GetModuleHandle(BYVAL 0)
     zClassName              = "Ghost"
     ClassInfo.cbSize        = SIZEOF(ClassInfo)                       'Size of WNDCLASSEX structure
     ClassInfo.STYLE         = %CS_DBLCLKS                             'Class styles
     ClassInfo.lpfnWndProc   = CODEPTR(GhostProc)                      'Address of window procedure used by class
     ClassInfo.cbClsExtra    = 0                                       'Extra class bytes
     ClassInfo.cbWndExtra    = 0                                       'Extra window bytes
     ClassInfo.hInstance     = hInstance                               'Instance of the process that is registering the window
     ClassInfo.hIcon         = ExtractIcon(GetModuleHandle(""), "Shell32.dll", 80) 'Handle of class icon
     ClassInfo.hCursor       = LoadCursor(%NULL, BYVAL %IDC_ARROW)     'Handle of class cursor
     ClassInfo.hbrBackground = %COLOR_BTNFACE + 1                      'Brush used to fill background of window's client area
     ClassInfo.lpszMenuName  = %NULL                                   'Resource identifier of the class menu
     ClassInfo.lpszClassName = VARPTR(zClassName)                      'Class name
     ClassInfo.hIconSm       = %NULL                                   'Handle of small icon shown in caption/system Taskbar
     RegisterClassEx(ClassInfo)
     
     WindowSize.cx = 360
     WindowSize.cy = 165
     
     hFont = GetStockObject(%DEFAULT_GUI_FONT)
     
     'You can create ghost class windows using "Ghost" as the class name to CreateWindow(Ex).
     hWnd = CreateWindowEx(%WS_EX_WINDOWEDGE, _                     'Extended styles
                           "Ghost", _                               'Class name
                           "SDK Ghost window", _                    'Caption
                           %WS_OVERLAPPED OR _                      'Window styles
                           %WS_BORDER OR _
                           %WS_DLGFRAME OR _
                           %WS_CAPTION OR _
                           %WS_SYSMENU OR _
                           %WS_CLIPSIBLINGS OR _
                           %WS_VISIBLE, _
                           (GetSystemMetrics(%SM_CXSCREEN) - WindowSize.cx) / 2, _
                           200 + (GetSystemMetrics(%SM_CYSCREEN) - WindowSize.cy) / 2, _
                           WindowSize.cx, WindowSize.cy, _         'Width, height
                           %HWND_DESKTOP, %NULL, _                 'Handle of owner, menu handle
                           hInstance, BYVAL %NULL)                 'Handle of instance, creation parameters
     @h.Ghost = hWnd
     
     hWndChild = CreateWindowEx(0, _                                       'Extended styles
                                "Static", _                                'Class name
                                "Click crashed program close button or client area" & $CRLF & _ 'Caption
                                "to flash and bring this ghost window to the top.", _
                                %WS_CHILD OR %WS_VISIBLE OR %WS_GROUP OR _ 'Window styles
                                %SS_CENTER, _                              'Class styles
                                12, 15, _                                  'Left, top
                                WindowSize.cx - 24, 26, _                  'Width, height
                                hWnd, %LabelGhostInfo, _                   'Handle of parent, control ID
                                hInstance, BYVAL %NULL)                    'Handle of instance, creation parameters
     SendMessage(hWndChild, %WM_SETFONT, hFont, %TRUE)
     
     hWndChild = CreateWindowEx(0, _                                       'Extended styles
                                "Static", _                                'Class name
                                "-", _                                     'Caption
                                %WS_CHILD OR %WS_VISIBLE OR %WS_GROUP OR _ 'Window styles
                                %SS_CENTER, _                              'Class styles
                                12, 48, _                                  'Left, top
                                WindowSize.cx - 24, 13, _                  'Width, height
                                hWnd, %LabelGhostHandle, _                 'Handle of parent, control ID
                                hInstance, BYVAL %NULL)                    'Handle of instance, creation parameters
     SendMessage(hWndChild, %WM_SETFONT, hFont, %TRUE)
     
     hWndChild = CreateWindowEx(0, _                                       'Extended styles
                                "Static", _                                'Class name
                                "-", _                                     'Caption
                                %WS_CHILD OR %WS_VISIBLE OR %WS_GROUP OR _ 'Window styles
                                %SS_CENTER, _                              'Class styles
                                12, 70, _                                  'Left, top
                                WindowSize.cx - 24, 13, _                  'Width, height
                                hWnd, %LabelGhostMessage, _                'Handle of parent, control ID
                                hInstance, BYVAL %NULL)                    'Handle of instance, creation parameters
     SendMessage(hWndChild, %WM_SETFONT, hFont, %TRUE)
     
     hWndChild = CreateWindowEx(0, _                                              'Extended styles
                                "Button", _                                       'Class name
                                "Close simulated crashed program?", _             'Caption
                                %WS_CHILD OR %WS_VISIBLE OR %WS_TABSTOP OR _      'Window styles
                                %BS_DEFPUSHBUTTON OR %BS_CENTER OR %BS_VCENTER, _ 'Class styles
                                WindowSize.cx / 2 - 100, 100, _                    'Left, top
                                200, 33, _                                        'Width, height
                                hWnd, %ButtonGhostClose, _                        'Handle of parent, control ID
                                hInstance, BYVAL %NULL)                           'Handle of instance, creation parameters
     SendMessage(hWndChild, %WM_SETFONT, hFont, %TRUE)
     
     ShowWindow(hWnd, %SW_SHOW)
     UpdateWindow(hWnd)
     
     WHILE GetMessage(tmsg, BYVAL %NULL, 0, 0) > 0
       IF IsDialogMessage(hWnd, tmsg) = 0 THEN
         TranslateMessage(tmsg)
         DispatchMessage(tmsg)
       END IF
     WEND
     
    END FUNCTION
    '______________________________________________________________________________
     
    CALLBACK FUNCTION DlgProc
     STATIC h        AS HandleGroup
     LOCAL  hThread  AS DWORD
     
     SELECT CASE CBMSG
     
       CASE %WM_COMMAND
         SELECT CASE CBCTL
           CASE %ButtonMainFrost
             IF CBCTLMSG = %BN_CLICKED OR CBCTLMSG = 1 THEN
               h.dlg = CBHNDL
               THREAD CREATE MainGhost(VARPTR(h)) TO hThread
               THREAD CLOSE hThread TO hThread
               WHILE h.Ghost = 0
                 SLEEP 100 'Give time to start ghost window
               WEND
               'Replaces a window with a ghosted "hung" version that cannot be interacted with
     
               'The window to replace, Optional handle to a "ghost" class window which acts as the error reporting dialog
               'If a window is specified as hwndErrorReportingOwner, it will be sent the registered "WerHangRepMessage" message,
               'flashed, and brought to the foreground whenever there is a click
               'on the replacement window, just like the standard "This window is not responding" dialog
               'To receive messages sent, and to control the hwndErrorReportOwner window, you must subclass it.
               'Its default WndProc does not respond to attempting to close the ghosted window
     
               'Return the handle to the replacement window or NULL on failure.
               h.Frosted = FrostCrashedWindow(h.Dlg, h.Ghost)
               PostMessage(h.Ghost, %WM_APP, VARPTR(h), 0)
             END IF
         END SELECT
      END SELECT
     
    END FUNCTION
    '______________________________________________________________________________
     
    FUNCTION PBMAIN()
     LOCAL hDlg  AS DWORD
     LOCAL hIcon AS DWORD
     
     DIALOG FONT "Segoe UI", 9
     DIALOG NEW %HWND_DESKTOP, "FrostCrashedWindow", , , 200, 60, _
     %WS_CAPTION OR %WS_MINIMIZEBOX OR %WS_SYSMENU, 0 TO hDlg
     
     hIcon = ExtractIcon(GetModuleHandle(""), "Shell32.dll", 80)
     SetClassLong(hDlg, %GCL_HICON, hIcon)
     
     CONTROL ADD LABEL,  hDlg, %LabelMainFrost,  "FrostCrashedWindow",         5,  5, 190, 10, %SS_CENTER OR %SS_NOTIFY
     CONTROL ADD LABEL,  hDlg, %LabelMainSpec,   "Undocumented api: Vista +",  5, 20, 190, 10, %SS_CENTER OR %SS_NOTIFY
     CONTROL ADD BUTTON, hDlg, %ButtonMainFrost, "&FrostCrashedWindow",       50, 35, 100, 15
     
     DIALOG SHOW MODAL hDlg CALL DlgProc
     
     DestroyIcon(hIcon)
     
    END FUNCTION
    '______________________________________________________________________________
    '
    Last edited by Pierre Bellisle; 13 Apr 2021, 08:31 PM.

  • #2
    Runs on W7/32 also, have not yet worked out what it's for!

    Comment


    • #3
      Hey Chris,
      Good to know it is working on 7-32.
      The utility of this code is to have a better understanding of what the OS is doing after it detect that a program crashed.
      To know what's under the hood.
      Nothing but pure curiosity.

      Pierre


      Comment

      Working...
      X