[USERSRV] Hard-error improvements 5/7 - Implement STATUS_SERVICE_NOTIFICATION handling.
[reactos.git] / win32ss / user / winsrv / usersrv / harderror.c
index 9889a54..9a1d2c7 100644 (file)
 #include <undocelfapi.h>
 #include <ntstrsafe.h>
 
+#include "resource.h"
+
 #define NDEBUG
 #include <debug.h>
 
-#define IDTRYAGAIN 10
-#define IDCONTINUE 11
 
 /* FUNCTIONS ******************************************************************/
 
+/* Cache for the localized hard-error message box strings */
+LANGID g_CurrentUserLangId = 0;
+UNICODE_STRING g_SuccessU = {0, 0, NULL};
+UNICODE_STRING g_InformationU = {0, 0, NULL};
+UNICODE_STRING g_WarningU = {0, 0, NULL};
+UNICODE_STRING g_ErrorU = {0, 0, NULL};
+UNICODE_STRING g_SystemProcessU = {0, 0, NULL};
+UNICODE_STRING g_OKTerminateU = {0, 0, NULL};
+UNICODE_STRING g_CancelDebugU = {0, 0, NULL};
+
+VOID
+RtlLoadUnicodeString(
+    IN HINSTANCE hInstance OPTIONAL,
+    IN UINT uID,
+    OUT PUNICODE_STRING pUnicodeString,
+    IN PCWSTR pDefaultString)
+{
+    UINT Length;
+
+    /* Try to load the string from the resource */
+    Length = LoadStringW(hInstance, uID, (LPWSTR)&pUnicodeString->Buffer, 0);
+    if (Length == 0)
+    {
+        /* If the resource string was not found, use the fallback default one */
+        RtlInitUnicodeString(pUnicodeString, pDefaultString);
+    }
+    else
+    {
+        /* Set the string length (not NULL-terminated!) */
+        pUnicodeString->MaximumLength = (USHORT)(Length * sizeof(WCHAR));
+        pUnicodeString->Length = pUnicodeString->MaximumLength;
+    }
+}
+
+
+/* FIXME */
+int
+WINAPI
+MessageBoxTimeoutW(
+    HWND hWnd,
+    LPCWSTR lpText,
+    LPCWSTR lpCaption,
+    UINT uType,
+    WORD wLanguageId,
+    DWORD dwTime);
+
+
+static
+VOID
+UserpCaptureStringParameters(
+    OUT PULONG_PTR Parameters,
+    OUT PULONG SizeOfAllUnicodeStrings,
+    IN PHARDERROR_MSG Message,
+    IN HANDLE hProcess OPTIONAL)
+{
+    NTSTATUS Status;
+    ULONG nParam, Size = 0;
+    UNICODE_STRING TempStringU, ParamStringU;
+    ANSI_STRING TempStringA;
+
+    if (SizeOfAllUnicodeStrings)
+        *SizeOfAllUnicodeStrings = 0;
+
+    /* Read all strings from client space */
+    for (nParam = 0; nParam < Message->NumberOfParameters; ++nParam)
+    {
+        Parameters[nParam] = 0;
+
+        /* Check if the current parameter is a unicode string */
+        if (Message->UnicodeStringParameterMask & (1 << nParam))
+        {
+            /* Skip this string if we do not have a client process */
+            if (!hProcess)
+                continue;
+
+            /* Read the UNICODE_STRING from the process memory */
+            Status = NtReadVirtualMemory(hProcess,
+                                         (PVOID)Message->Parameters[nParam],
+                                         &ParamStringU,
+                                         sizeof(ParamStringU),
+                                         NULL);
+            if (!NT_SUCCESS(Status))
+            {
+                /* We failed, skip this string */
+                DPRINT1("NtReadVirtualMemory(Message->Parameters) failed, Status 0x%lx, skipping.\n", Status);
+                continue;
+            }
+
+            /* Allocate a buffer for the string and reserve a NULL terminator */
+            TempStringU.MaximumLength = ParamStringU.Length + sizeof(UNICODE_NULL);
+            TempStringU.Length = ParamStringU.Length;
+            TempStringU.Buffer = RtlAllocateHeap(RtlGetProcessHeap(),
+                                                 HEAP_ZERO_MEMORY,
+                                                 TempStringU.MaximumLength);
+            if (!TempStringU.Buffer)
+            {
+                /* We failed, skip this string */
+                DPRINT1("Cannot allocate memory with size %u, skipping.\n", TempStringU.MaximumLength);
+                continue;
+            }
+
+            /* Read the string buffer from the process memory */
+            Status = NtReadVirtualMemory(hProcess,
+                                         ParamStringU.Buffer,
+                                         TempStringU.Buffer,
+                                         ParamStringU.Length,
+                                         NULL);
+            if (!NT_SUCCESS(Status))
+            {
+                /* We failed, skip this string */
+                DPRINT1("NtReadVirtualMemory(ParamStringU) failed, Status 0x%lx, skipping.\n", Status);
+                RtlFreeHeap(RtlGetProcessHeap(), 0, TempStringU.Buffer);
+                continue;
+            }
+            /* NULL-terminate the string */
+            TempStringU.Buffer[TempStringU.Length / sizeof(WCHAR)] = UNICODE_NULL;
+
+            DPRINT("ParamString = \'%wZ\'\n", &TempStringU);
+
+            if (Message->Status == STATUS_SERVICE_NOTIFICATION)
+            {
+                /* Just keep the allocated NULL-terminated UNICODE string */
+                Parameters[nParam] = (ULONG_PTR)TempStringU.Buffer;
+                Size += TempStringU.Length;
+            }
+            else
+            {
+                /* Allocate a buffer for converted to ANSI string */
+                TempStringA.MaximumLength = RtlUnicodeStringToAnsiSize(&TempStringU);
+                TempStringA.Buffer = RtlAllocateHeap(RtlGetProcessHeap(),
+                                                     HEAP_ZERO_MEMORY,
+                                                     TempStringA.MaximumLength);
+                if (!TempStringA.Buffer)
+                {
+                    /* We failed, skip this string */
+                    DPRINT1("Cannot allocate memory with size %u, skipping.\n", TempStringA.MaximumLength);
+                    RtlFreeHeap(RtlGetProcessHeap(), 0, TempStringU.Buffer);
+                    continue;
+                }
+
+                /* Convert string to ANSI and free temporary buffer */
+                Status = RtlUnicodeStringToAnsiString(&TempStringA, &TempStringU, FALSE);
+                RtlFreeHeap(RtlGetProcessHeap(), 0, TempStringU.Buffer);
+                if (!NT_SUCCESS(Status))
+                {
+                    /* We failed, skip this string */
+                    DPRINT1("RtlUnicodeStringToAnsiString() failed, Status 0x%lx, skipping.\n", Status);
+                    RtlFreeHeap(RtlGetProcessHeap(), 0, TempStringA.Buffer);
+                    continue;
+                }
+
+                /* Note: RtlUnicodeStringToAnsiString() returns a NULL-terminated string */
+                Parameters[nParam] = (ULONG_PTR)TempStringA.Buffer;
+                Size += TempStringU.Length;
+            }
+        }
+        else
+        {
+            /* It's not a unicode string, just copy the parameter */
+            Parameters[nParam] = Message->Parameters[nParam];
+        }
+    }
+
+    if (SizeOfAllUnicodeStrings)
+        *SizeOfAllUnicodeStrings = Size;
+}
+
+static
+VOID
+UserpFreeStringParameters(
+    IN OUT PULONG_PTR Parameters,
+    IN PHARDERROR_MSG Message)
+{
+    ULONG nParam;
+
+    /* Loop all parameters */
+    for (nParam = 0; nParam < Message->NumberOfParameters; ++nParam)
+    {
+        /* Check if the current parameter is a string */
+        if ((Message->UnicodeStringParameterMask & (1 << nParam)) && (Parameters[nParam] != 0))
+        {
+            /* Free the string buffer */
+            RtlFreeHeap(RtlGetProcessHeap(), 0, (PVOID)Parameters[nParam]);
+        }
+    }
+}
+
 static
 NTSTATUS
 UserpGetClientFileName(
@@ -43,9 +230,7 @@ UserpGetClientFileName(
     PPEB Peb;
 
     /* Initialize string */
-    ClientFileNameU->MaximumLength = 0;
-    ClientFileNameU->Length = 0;
-    ClientFileNameU->Buffer = NULL;
+    RtlInitEmptyUnicodeString(ClientFileNameU, NULL, 0);
 
     /* Query process information */
     Status = NtQueryInformationProcess(hProcess,
@@ -55,6 +240,8 @@ UserpGetClientFileName(
                                        NULL);
     if (!NT_SUCCESS(Status)) return Status;
 
+    /* Locate the process loader data table and retrieve its name from it */
+
     Peb = ClientBasicInfo.PebBaseAddress;
     if (!Peb) return STATUS_UNSUCCESSFUL;
 
@@ -91,8 +278,13 @@ UserpGetClientFileName(
 
     ClientFileNameU->MaximumLength = ModuleData.BaseDllName.MaximumLength;
     ClientFileNameU->Buffer = RtlAllocateHeap(RtlGetProcessHeap(),
-                              HEAP_ZERO_MEMORY,
-                              ClientFileNameU->MaximumLength);
+                                              HEAP_ZERO_MEMORY,
+                                              ClientFileNameU->MaximumLength);
+    if (!ClientFileNameU->Buffer)
+    {
+        RtlInitEmptyUnicodeString(ClientFileNameU, NULL, 0);
+        return STATUS_NO_MEMORY;
+    }
 
     Status = NtReadVirtualMemory(hProcess,
                                  ModuleData.BaseDllName.Buffer,
@@ -102,172 +294,195 @@ UserpGetClientFileName(
     if (!NT_SUCCESS(Status))
     {
         RtlFreeHeap(RtlGetProcessHeap(), 0, ClientFileNameU->Buffer);
-        ClientFileNameU->Buffer = NULL;
-        ClientFileNameU->MaximumLength = 0;
+        RtlInitEmptyUnicodeString(ClientFileNameU, NULL, 0);
         return Status;
     }
 
-    ClientFileNameU->Length = wcslen(ClientFileNameU->Buffer) * sizeof(WCHAR);
-    DPRINT("ClientFileNameU=\'%wZ\'\n", &ClientFileNameU);
+    ClientFileNameU->Length = (USHORT)(wcslen(ClientFileNameU->Buffer) * sizeof(WCHAR));
+    DPRINT("ClientFileNameU = \'%wZ\'\n", &ClientFileNameU);
 
     return STATUS_SUCCESS;
 }
 
 static
 VOID
-UserpFreeStringParameters(
-    IN OUT PULONG_PTR Parameters,
-    IN PHARDERROR_MSG HardErrorMessage)
+UserpDuplicateParamStringToUnicodeString(
+    IN OUT PUNICODE_STRING UnicodeString,
+    IN PCWSTR ParamString)
 {
-    ULONG nParam;
+    UNICODE_STRING FormatU, TempStringU;
 
-    /* Loop all parameters */
-    for (nParam = 0; nParam < HardErrorMessage->NumberOfParameters; nParam++)
+    /* Calculate buffer length for the text message */
+    RtlInitUnicodeString(&FormatU, (PWSTR)ParamString);
+    if (UnicodeString->MaximumLength < FormatU.MaximumLength)
     {
-        /* Check if the current parameter is a string */
-        if ((HardErrorMessage->UnicodeStringParameterMask & (1 << nParam)) && Parameters[nParam])
+        /* Duplicate the text message in a larger buffer */
+        if (NT_SUCCESS(RtlDuplicateUnicodeString(RTL_DUPLICATE_UNICODE_STRING_NULL_TERMINATE,
+                                                 &FormatU, &TempStringU)))
         {
-            /* Free the string buffer */
-            RtlFreeHeap(RtlGetProcessHeap(), 0, (PVOID)Parameters[nParam]);
+            *UnicodeString = TempStringU;
+        }
+        else
+        {
+            /* We could not allocate a larger buffer; continue using the smaller original buffer */
+            DPRINT1("Cannot allocate memory for UnicodeString, use original buffer.\n");
+
+            /* Copy the truncated string, NULL-terminate it */
+            FormatU.MaximumLength = UnicodeString->MaximumLength;
+            FormatU.Length = FormatU.MaximumLength - sizeof(UNICODE_NULL);
+            RtlCopyUnicodeString(UnicodeString, &FormatU);
         }
     }
+    else
+    {
+        /* Copy the string, NULL-terminate it */
+        RtlCopyUnicodeString(UnicodeString, &FormatU);
+    }
 }
 
 static
-NTSTATUS
-UserpCaptureStringParameters(
-    OUT PULONG_PTR Parameters,
-    OUT PULONG SizeOfAllUnicodeStrings,
-    IN PHARDERROR_MSG HardErrorMessage,
-    IN HANDLE hProcess)
+VOID
+UserpFormatMessages(
+    IN OUT PUNICODE_STRING TextStringU,
+    IN OUT PUNICODE_STRING CaptionStringU,
+    OUT PUINT pdwType,
+    IN PHARDERROR_MSG Message)
 {
-    ULONG nParam, Size = 0;
-    NTSTATUS Status = STATUS_SUCCESS;
-    UNICODE_STRING TempStringU, ParamStringU;
-    ANSI_STRING TempStringA;
-
-    if (SizeOfAllUnicodeStrings)
-        *SizeOfAllUnicodeStrings = 0;
+    NTSTATUS Status;
+    OBJECT_ATTRIBUTES ObjectAttributes;
+    HANDLE hProcess;
+    ULONG SizeOfStrings;
+    ULONG_PTR Parameters[MAXIMUM_HARDERROR_PARAMETERS] = {0};
+    ULONG_PTR CopyParameters[MAXIMUM_HARDERROR_PARAMETERS];
+    UNICODE_STRING WindowTitleU, FileNameU, TempStringU, FormatU, Format2U;
+    ANSI_STRING FormatA, Format2A;
+    HWND hwndOwner;
+    PMESSAGE_RESOURCE_ENTRY MessageResource;
+    PWSTR FormatString, pszBuffer;
+    size_t cszBuffer;
+    ULONG Severity = (ULONG)(Message->Status) >> 30;
+    ULONG Size;
 
-    /* Read all strings from client space */
-    for (nParam = 0; nParam < HardErrorMessage->NumberOfParameters; nParam++)
+    /* Open client process */
+    InitializeObjectAttributes(&ObjectAttributes, NULL, 0, NULL, NULL);
+    Status = NtOpenProcess(&hProcess,
+                           PROCESS_VM_READ | PROCESS_QUERY_INFORMATION,
+                           &ObjectAttributes,
+                           &Message->h.ClientId);
+    if (!NT_SUCCESS(Status))
     {
-        Parameters[nParam] = 0;
-
-        /* Check if the current parameter is a unicode string */
-        if (HardErrorMessage->UnicodeStringParameterMask & (1 << nParam))
-        {
-            /* Read the UNICODE_STRING from the process memory */
-            Status = NtReadVirtualMemory(hProcess,
-                                         (PVOID)HardErrorMessage->Parameters[nParam],
-                                         &ParamStringU,
-                                         sizeof(ParamStringU),
-                                         NULL);
-            if (!NT_SUCCESS(Status))
-                break;
-
-            /* Allocate a buffer for the string */
-            TempStringU.MaximumLength = ParamStringU.Length;
-            TempStringU.Length = ParamStringU.Length;
-            TempStringU.Buffer = RtlAllocateHeap(RtlGetProcessHeap(),
-                                                 HEAP_ZERO_MEMORY,
-                                                 TempStringU.MaximumLength);
-            if (!TempStringU.Buffer)
-            {
-                DPRINT1("Cannot allocate memory %u\n", TempStringU.MaximumLength);
-                Status = STATUS_NO_MEMORY;
-            }
-
-            /* Read the string buffer from the process memory */
-            Status = NtReadVirtualMemory(hProcess,
-                                         ParamStringU.Buffer,
-                                         TempStringU.Buffer,
-                                         ParamStringU.Length,
-                                         NULL);
-            if (!NT_SUCCESS(Status))
-            {
-                DPRINT1("NtReadVirtualMemory failed with code: %lx\n", Status);
-                RtlFreeHeap(RtlGetProcessHeap(), 0, TempStringU.Buffer);
-                break;
-            }
-
-            DPRINT("ParamString=\'%wZ\'\n", &TempStringU);
+        DPRINT1("NtOpenProcess failed with status 0x%08lx, possibly SYSTEM process.\n", Status);
+        hProcess = NULL;
+    }
 
-            /* Allocate a buffer for converted to ANSI string */
-            TempStringA.MaximumLength = RtlUnicodeStringToAnsiSize(&TempStringU);
-            TempStringA.Buffer = RtlAllocateHeap(RtlGetProcessHeap(),
-                                                 HEAP_ZERO_MEMORY,
-                                                 TempStringA.MaximumLength);
-            if (!TempStringA.Buffer)
-            {
-                DPRINT1("Cannot allocate memory %u\n", TempStringA.MaximumLength);
-                RtlFreeHeap(RtlGetProcessHeap(), 0, TempStringU.Buffer);
-                Status = STATUS_NO_MEMORY;
-                break;
-            }
+    /* Capture all string parameters from the process memory */
+    UserpCaptureStringParameters(Parameters, &SizeOfStrings, Message, hProcess);
 
-            /* Convert string to ANSI and free temporary buffer */
-            Status = RtlUnicodeStringToAnsiString(&TempStringA, &TempStringU, FALSE);
-            RtlFreeHeap(RtlGetProcessHeap(), 0, TempStringU.Buffer);
-            if (!NT_SUCCESS(Status))
-            {
-                RtlFreeHeap(RtlGetProcessHeap(), 0, TempStringA.Buffer);
-                break;
-            }
+    /*
+     * Check whether it is a service notification, in which case
+     * we format the parameters and take the short route.
+     */
+    if (Message->Status == STATUS_SERVICE_NOTIFICATION)
+    {
+        /* Close the process handle */
+        if (hProcess) NtClose(hProcess);
+
+        /*
+         * Retrieve the message box flags. Note that we filter out
+         * MB_SERVICE_NOTIFICATION to not enter an infinite recursive
+         * loop when we will call MessageBox() later on.
+         */
+        *pdwType = (UINT)Parameters[2] & ~MB_SERVICE_NOTIFICATION;
+
+        /* Duplicate the UNICODE text message */
+        if (Message->UnicodeStringParameterMask & 0x1)
+        {
+            /* A string has been provided: duplicate it */
+            UserpDuplicateParamStringToUnicodeString(TextStringU, (PCWSTR)Parameters[0]);
+        }
+        else
+        {
+            /* No string (or invalid one) has been provided: keep the original buffer and reset the string length to zero */
+            TextStringU->Length = 0;
+        }
 
-            /* Note: RtlUnicodeStringToAnsiString returns NULL terminated string */
-            Parameters[nParam] = (ULONG_PTR)TempStringA.Buffer;
-            Size += TempStringU.Length;
+        /* Duplicate the UNICODE caption */
+        if (Message->UnicodeStringParameterMask & 0x2)
+        {
+            /* A string has been provided: duplicate it */
+            UserpDuplicateParamStringToUnicodeString(CaptionStringU, (PCWSTR)Parameters[1]);
         }
         else
         {
-            /* It's not a unicode string */
-            Parameters[nParam] = HardErrorMessage->Parameters[nParam];
+            /* No string (or invalid one) has been provided: keep the original buffer and reset the string length to zero */
+            CaptionStringU->Length = 0;
         }
+
+        goto Quit;
     }
 
-    if (!NT_SUCCESS(Status))
+    /* Set the message box type */
+    *pdwType = 0;
+    switch (Message->ValidResponseOptions)
     {
-        UserpFreeStringParameters(Parameters, HardErrorMessage);
-        return Status;
+        case OptionAbortRetryIgnore:
+            *pdwType = MB_ABORTRETRYIGNORE;
+            break;
+        case OptionOk:
+            *pdwType = MB_OK;
+            break;
+        case OptionOkCancel:
+            *pdwType = MB_OKCANCEL;
+            break;
+        case OptionRetryCancel:
+            *pdwType = MB_RETRYCANCEL;
+            break;
+        case OptionYesNo:
+            *pdwType = MB_YESNO;
+            break;
+        case OptionYesNoCancel:
+            *pdwType = MB_YESNOCANCEL;
+            break;
+        case OptionShutdownSystem:
+            *pdwType = MB_OK;
+            break;
+        case OptionOkNoWait:
+            *pdwType = MB_OK;
+            break;
+        case OptionCancelTryContinue:
+            *pdwType = MB_CANCELTRYCONTINUE;
+            break;
     }
 
-    if (SizeOfAllUnicodeStrings)
-        *SizeOfAllUnicodeStrings = Size;
+    /* Set the severity icon */
+    // STATUS_SEVERITY_SUCCESS
+         if (Severity == STATUS_SEVERITY_INFORMATIONAL) *pdwType |= MB_ICONINFORMATION;
+    else if (Severity == STATUS_SEVERITY_WARNING)       *pdwType |= MB_ICONWARNING;
+    else if (Severity == STATUS_SEVERITY_ERROR)         *pdwType |= MB_ICONERROR;
 
-    return Status;
-}
+    *pdwType |= MB_SYSTEMMODAL | MB_SETFOREGROUND;
 
-static
-NTSTATUS
-UserpFormatMessages(
-    OUT PUNICODE_STRING TextStringU,
-    OUT PUNICODE_STRING CaptionStringU,
-    IN  PULONG_PTR Parameters,
-    IN  ULONG SizeOfStrings,
-    IN  PHARDERROR_MSG Message,
-    IN  HANDLE hProcess)
-{
-    NTSTATUS Status;
-    UNICODE_STRING FileNameU, TempStringU, FormatU, Format2U;
-    ANSI_STRING FormatA, Format2A;
-    PMESSAGE_RESOURCE_ENTRY MessageResource;
-    PWSTR FormatString;
-    ULONG ExceptionCode, Severity;
-    ULONG Size;
+    /* Copy the Parameters array locally */
+    RtlCopyMemory(&CopyParameters, Parameters, sizeof(CopyParameters));
 
     /* Get the file name of the client process */
-    UserpGetClientFileName(&FileNameU, hProcess);
-
-    /* Check if we have a file name */
-    if (!FileNameU.Buffer)
+    Status = STATUS_SUCCESS;
+    if (hProcess)
+        Status = UserpGetClientFileName(&FileNameU, hProcess);
+
+    /* Close the process handle but keep its original value to know where stuff came from */
+    if (hProcess) NtClose(hProcess);
+
+    /*
+     * Fall back to SYSTEM process if the client process handle
+     * was NULL or we failed retrieving a file name.
+     */
+    if (!hProcess || !NT_SUCCESS(Status) || !FileNameU.Buffer)
     {
-        /* No, this is system process */
-        RtlInitUnicodeString(&FileNameU, L"System Process");
+        hProcess  = NULL;
+        FileNameU = g_SystemProcessU;
     }
 
-    Severity = (ULONG)(Message->Status) >> 30;
-
     /* Get text string of the error code */
     Status = RtlFindMessage(GetModuleHandleW(L"ntdll"),
                             (ULONG_PTR)RT_MESSAGETABLE,
@@ -283,14 +498,25 @@ UserpFormatMessages(
         }
         else
         {
-            RtlInitAnsiString(&FormatA, (PCHAR)MessageResource->Text);
-            RtlAnsiStringToUnicodeString(&FormatU, &FormatA, TRUE);
+            RtlInitAnsiString(&FormatA, (PSTR)MessageResource->Text);
+            /* Status = */ RtlAnsiStringToUnicodeString(&FormatU, &FormatA, TRUE);
         }
     }
     else
     {
-        /* Fall back to hardcoded value */
-        RtlInitUnicodeString(&FormatU, L"Unknown Hard Error");
+        /*
+         * Fall back to hardcoded value.
+         * NOTE: The value used here is ReactOS-specific: it allows specifying
+         * the exact hard error status value and the parameters. The version
+         * used on Windows only says "Unknown Hard Error".
+         */
+#if 0
+        RtlInitUnicodeString(&FormatU, L"Unknown Hard Error 0x%08lx\n"
+                                       L"Parameters: 0x%p 0x%p 0x%p 0x%p");
+#else
+        RtlInitUnicodeString(&FormatU, L"Unknown Hard Error 0x%08lx");
+        CopyParameters[0] = Message->Status;
+#endif
         FormatA.Buffer = NULL;
     }
 
@@ -314,40 +540,79 @@ UserpFormatMessages(
     }
     else
     {
-        /* FIXME: Use localized strings! */
-
         if (Severity == STATUS_SEVERITY_SUCCESS)
-            RtlInitUnicodeString(&TempStringU, L"Success");
+            TempStringU = g_SuccessU;
         else if (Severity == STATUS_SEVERITY_INFORMATIONAL)
-            RtlInitUnicodeString(&TempStringU, L"System Information");
+            TempStringU = g_InformationU;
         else if (Severity == STATUS_SEVERITY_WARNING)
-            RtlInitUnicodeString(&TempStringU, L"System Warning");
+            TempStringU = g_WarningU;
         else if (Severity == STATUS_SEVERITY_ERROR)
-            RtlInitUnicodeString(&TempStringU, L"System Error");
+            TempStringU = g_ErrorU;
         else
-            RtlInitEmptyUnicodeString(&TempStringU, NULL, 0);
+            ASSERT(FALSE); // Unexpected, since Severity is only <= 3.
     }
 
-    /* Calculate buffer length for the caption */
-    CaptionStringU->MaximumLength = FileNameU.Length + TempStringU.Length +
-                                    4 * sizeof(WCHAR);
-
-    /* Allocate a buffer for the caption */
-    CaptionStringU->Buffer = RtlAllocateHeap(RtlGetProcessHeap(),
-                             HEAP_ZERO_MEMORY,
-                             CaptionStringU->MaximumLength);
+    /* Retrieve the window title of the client, if it has one */
+    RtlInitEmptyUnicodeString(&WindowTitleU, NULL, 0);
+    hwndOwner = NULL;
+    EnumThreadWindows(HandleToUlong(Message->h.ClientId.UniqueThread),
+                      FindTopLevelWnd, (LPARAM)&hwndOwner);
+    if (hwndOwner)
+    {
+        cszBuffer = GetWindowTextLengthW(hwndOwner);
+        if (cszBuffer != 0)
+        {
+            cszBuffer += 3; // 2 characters for ": " and a NULL terminator.
+            WindowTitleU.MaximumLength = (USHORT)(cszBuffer * sizeof(WCHAR));
+            WindowTitleU.Buffer = RtlAllocateHeap(RtlGetProcessHeap(),
+                                                  HEAP_ZERO_MEMORY,
+                                                  WindowTitleU.MaximumLength);
+            if (WindowTitleU.Buffer)
+            {
+                cszBuffer = GetWindowTextW(hwndOwner,
+                                           WindowTitleU.Buffer,
+                                           WindowTitleU.MaximumLength / sizeof(WCHAR));
+                WindowTitleU.Length = (USHORT)(cszBuffer * sizeof(WCHAR));
+                RtlAppendUnicodeToString(&WindowTitleU, L": ");
+            }
+            else
+            {
+                RtlInitEmptyUnicodeString(&WindowTitleU, NULL, 0);
+            }
+        }
+    }
 
-    /* Append the file name, seperator and the caption text */
+    /* Calculate buffer length for the caption */
+    cszBuffer = WindowTitleU.Length + FileNameU.Length + TempStringU.Length +
+                3 * sizeof(WCHAR) + sizeof(UNICODE_NULL);
+    if (CaptionStringU->MaximumLength < cszBuffer)
+    {
+        /* Allocate a larger buffer for the caption */
+        pszBuffer = RtlAllocateHeap(RtlGetProcessHeap(),
+                                    HEAP_ZERO_MEMORY,
+                                    cszBuffer);
+        if (!pszBuffer)
+        {
+            /* We could not allocate a larger buffer; continue using the smaller original buffer */
+            DPRINT1("Cannot allocate memory for CaptionStringU, use original buffer.\n");
+        }
+        else
+        {
+            RtlInitEmptyUnicodeString(CaptionStringU, pszBuffer, (USHORT)cszBuffer);
+        }
+    }
     CaptionStringU->Length = 0;
-    RtlAppendUnicodeStringToString(CaptionStringU, &FileNameU);
-    RtlAppendUnicodeToString(CaptionStringU, L" - ");
-    RtlAppendUnicodeStringToString(CaptionStringU, &TempStringU);
 
-    /* Zero terminate the buffer */
-    CaptionStringU->Buffer[CaptionStringU->Length / sizeof(WCHAR)] = UNICODE_NULL;
+    /* Append the file name, the separator and the caption text */
+    RtlStringCbPrintfW(CaptionStringU->Buffer,
+                       CaptionStringU->MaximumLength,
+                       L"%wZ%wZ - %wZ",
+                       &WindowTitleU, &FileNameU, &TempStringU);
+    CaptionStringU->Length = (USHORT)(wcslen(CaptionStringU->Buffer) * sizeof(WCHAR));
 
-    /* Free the file name buffer */
-    RtlFreeUnicodeString(&FileNameU);
+    /* Free string buffers if needed */
+    if (WindowTitleU.Buffer) RtlFreeUnicodeString(&WindowTitleU);
+    if (hProcess) RtlFreeUnicodeString(&FileNameU);
 
     // FIXME: What is 42 == ??
     Size = 42;
@@ -355,7 +620,7 @@ UserpFormatMessages(
     /* Check if this is an exception message */
     if (Message->Status == STATUS_UNHANDLED_EXCEPTION)
     {
-        ExceptionCode = Parameters[0];
+        ULONG ExceptionCode = CopyParameters[0];
 
         /* Get text string of the exception code */
         Status = RtlFindMessage(GetModuleHandleW(L"ntdll"),
@@ -372,161 +637,213 @@ UserpFormatMessages(
             }
             else
             {
-                RtlInitAnsiString(&Format2A, (PCHAR)MessageResource->Text);
-                RtlAnsiStringToUnicodeString(&Format2U, &Format2A, TRUE);
+                RtlInitAnsiString(&Format2A, (PSTR)MessageResource->Text);
+                /* Status = */ RtlAnsiStringToUnicodeString(&Format2U, &Format2A, TRUE);
             }
 
-            /* Handle the special cases */
+            /* Handle special cases */
             if (ExceptionCode == STATUS_ACCESS_VIOLATION)
             {
-                FormatString  = Format2U.Buffer;
-                Parameters[0] = Parameters[1];
-                Parameters[1] = Parameters[3];
-                if (Parameters[2])
-                    Parameters[2] = (ULONG_PTR)L"written";
+                FormatString = Format2U.Buffer;
+                CopyParameters[0] = CopyParameters[1];
+                CopyParameters[1] = CopyParameters[3];
+                if (CopyParameters[2])
+                    CopyParameters[2] = (ULONG_PTR)L"written";
                 else
-                    Parameters[2] = (ULONG_PTR)L"read";
+                    CopyParameters[2] = (ULONG_PTR)L"read";
             }
             else if (ExceptionCode == STATUS_IN_PAGE_ERROR)
             {
-                FormatString  = Format2U.Buffer;
-                Parameters[0] = Parameters[1];
-                Parameters[1] = Parameters[3];
+                FormatString = Format2U.Buffer;
+                CopyParameters[0] = CopyParameters[1];
+                CopyParameters[1] = CopyParameters[3];
             }
             else
             {
-                PWSTR pTmp;
-
                 /* Keep the existing FormatString */
-                Parameters[2] = Parameters[1];
-                Parameters[1] = Parameters[0];
+                CopyParameters[2] = CopyParameters[1];
+                CopyParameters[1] = CopyParameters[0];
 
-                pTmp = Format2U.Buffer;
-                if (!_wcsnicmp(pTmp, L"{EXCEPTION}", 11))
+                pszBuffer = Format2U.Buffer;
+                if (!_wcsnicmp(pszBuffer, L"{EXCEPTION}", 11))
                 {
                     /*
                      * This is a named exception. Skip the mark and
                      * retrieve the exception name that follows it.
                      */
-                    pTmp += 11;
+                    pszBuffer += 11;
 
                     /* Skip '\r', '\n' */
-                    pTmp += 2;
+                    pszBuffer += 2;
 
-                    Parameters[0] = (ULONG_PTR)pTmp;
+                    CopyParameters[0] = (ULONG_PTR)pszBuffer;
                 }
                 else
                 {
                     /* Fall back to hardcoded value */
-                    Parameters[0] = (ULONG_PTR)L"unknown software exception";
+                    CopyParameters[0] = (ULONG_PTR)L"unknown software exception";
                 }
             }
         }
         else
         {
             /* Fall back to hardcoded value, and keep the existing FormatString */
-            Parameters[2] = Parameters[1];
-            Parameters[1] = Parameters[0];
-            Parameters[0] = (ULONG_PTR)L"unknown software exception";
+            CopyParameters[2] = CopyParameters[1];
+            CopyParameters[1] = CopyParameters[0];
+            CopyParameters[0] = (ULONG_PTR)L"unknown software exception";
         }
 
-        /* FIXME: Use localized strings! */
+        /* Add explanation text for dialog buttons */
         if (Message->ValidResponseOptions == OptionOk ||
             Message->ValidResponseOptions == OptionOkCancel)
         {
-            // Tmp = FormatString + wcslen(FormatString);
-            // *++Tmp = L'\n';
-            // *++Tmp = L'\n';
-            Size += 2 + wcslen(L"Click on OK to terminate the program.");
+            /* Reserve space for one newline and the OK-terminate-program string */
+            Size += 1 + (g_OKTerminateU.Length / sizeof(WCHAR));
         }
         if (Message->ValidResponseOptions == OptionOkCancel)
         {
-            Size += 1 + wcslen(L"Click on CANCEL to debug the program.");
+            /* Reserve space for one newline and the CANCEL-debug-program string */
+            Size += 1 + (g_CancelDebugU.Length / sizeof(WCHAR));
         }
     }
 
-    /* Calculate length of text buffer */
-    TextStringU->MaximumLength = FormatU.Length + SizeOfStrings +
-                                 (USHORT)(Size * sizeof(WCHAR)) +
-                                 sizeof(UNICODE_NULL);
-
-    /* Allocate a buffer for the text */
-    TextStringU->Buffer = RtlAllocateHeap(RtlGetProcessHeap(),
-                                          HEAP_ZERO_MEMORY,
-                                          TextStringU->MaximumLength);
+    /* Calculate buffer length for the text message */
+    cszBuffer = FormatU.Length + SizeOfStrings + Size * sizeof(WCHAR) +
+                sizeof(UNICODE_NULL);
+    if (TextStringU->MaximumLength < cszBuffer)
+    {
+        /* Allocate a larger buffer for the text message */
+        pszBuffer = RtlAllocateHeap(RtlGetProcessHeap(),
+                                    HEAP_ZERO_MEMORY,
+                                    cszBuffer);
+        if (!pszBuffer)
+        {
+            /* We could not allocate a larger buffer; continue using the smaller original buffer */
+            DPRINT1("Cannot allocate memory for TextStringU, use original buffer.\n");
+        }
+        else
+        {
+            RtlInitEmptyUnicodeString(TextStringU, pszBuffer, (USHORT)cszBuffer);
+        }
+    }
+    TextStringU->Length = 0;
 
     /* Wrap in SEH to protect from invalid string parameters */
     _SEH2_TRY
     {
         /* Print the string into the buffer */
-        RtlStringCbPrintfW(TextStringU->Buffer,
-                           TextStringU->MaximumLength,
-                           FormatString,
-                           Parameters[0],
-                           Parameters[1],
-                           Parameters[2],
-                           Parameters[3]);
-
+        pszBuffer = TextStringU->Buffer;
+        cszBuffer = TextStringU->MaximumLength;
+        RtlStringCbPrintfExW(pszBuffer, cszBuffer,
+                             &pszBuffer, &cszBuffer,
+                             STRSAFE_IGNORE_NULLS,
+                             FormatString,
+                             CopyParameters[0], CopyParameters[1],
+                             CopyParameters[2], CopyParameters[3]);
+
+        /* Add explanation text for dialog buttons */
         if (Message->Status == STATUS_UNHANDLED_EXCEPTION)
         {
-            /* FIXME: Use localized strings! */
             if (Message->ValidResponseOptions == OptionOk ||
                 Message->ValidResponseOptions == OptionOkCancel)
             {
-                // Tmp = FormatString + wcslen(FormatString);
-                // *++Tmp = L'\n';
-                // *++Tmp = L'\n';
-                RtlStringCbCatW(TextStringU->Buffer,
-                                TextStringU->MaximumLength,
-                                L"\n\n");
-                RtlStringCbCatW(TextStringU->Buffer,
-                                TextStringU->MaximumLength,
-                                L"Click on OK to terminate the program.");
+                RtlStringCbPrintfExW(pszBuffer, cszBuffer,
+                                     &pszBuffer, &cszBuffer,
+                                     STRSAFE_IGNORE_NULLS,
+                                     L"\n%wZ",
+                                     &g_OKTerminateU);
             }
             if (Message->ValidResponseOptions == OptionOkCancel)
             {
-                RtlStringCbCatW(TextStringU->Buffer,
-                                TextStringU->MaximumLength,
-                                L"Click on CANCEL to debug the program.");
+                RtlStringCbPrintfExW(pszBuffer, cszBuffer,
+                                     &pszBuffer, &cszBuffer,
+                                     STRSAFE_IGNORE_NULLS,
+                                     L"\n%wZ",
+                                     &g_CancelDebugU);
             }
         }
-
-        Status = STATUS_SUCCESS;
     }
     _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
     {
-        /* An exception occurred, use a default string */
+        /* An exception occurred, use a default string with the original parameters */
+        DPRINT1("Exception 0x%08lx occurred while building hard-error message, fall back to default message.\n",
+                _SEH2_GetExceptionCode());
+
         RtlStringCbPrintfW(TextStringU->Buffer,
                            TextStringU->MaximumLength,
-                           L"Exception processing message 0x%lx\n"
-                           L"Parameters 0x%lx 0x%lx 0x%lx 0x%lx",
+                           L"Exception processing message 0x%08lx\n"
+                           L"Parameters: 0x%p 0x%p 0x%p 0x%p",
                            Message->Status,
                            Parameters[0], Parameters[1],
                            Parameters[2], Parameters[3]);
-
-        /* Set error and free buffers */
-        Status = _SEH2_GetExceptionCode();
-        RtlFreeHeap(RtlGetProcessHeap(), 0, TextStringU->Buffer);
-        RtlFreeHeap(RtlGetProcessHeap(), 0, CaptionStringU->Buffer);
     }
     _SEH2_END;
 
-    if (NT_SUCCESS(Status))
-    {
-        TextStringU->Length = wcslen(TextStringU->Buffer) * sizeof(WCHAR);
-    }
+    TextStringU->Length = (USHORT)(wcslen(TextStringU->Buffer) * sizeof(WCHAR));
 
-    /* Free converted Unicode strings if the original format strings were Ansi */
+    /* Free converted Unicode strings */
     if (Format2A.Buffer) RtlFreeUnicodeString(&Format2U);
     if (FormatA.Buffer) RtlFreeUnicodeString(&FormatU);
 
-    return Status;
+Quit:
+    /* Final cleanup */
+    UserpFreeStringParameters(Parameters, Message);
+}
+
+static ULONG
+GetRegInt(
+    IN PCWSTR KeyName,
+    IN PCWSTR ValueName,
+    IN ULONG  DefaultValue)
+{
+    NTSTATUS Status;
+    ULONG Value = DefaultValue;
+    UNICODE_STRING String;
+    OBJECT_ATTRIBUTES ObjectAttributes;
+    HANDLE KeyHandle;
+    ULONG ResultLength;
+    UCHAR ValueBuffer[sizeof(KEY_VALUE_PARTIAL_INFORMATION) + sizeof(ULONG)];
+    PKEY_VALUE_PARTIAL_INFORMATION ValueInfo = (PKEY_VALUE_PARTIAL_INFORMATION)ValueBuffer;
+
+    RtlInitUnicodeString(&String, KeyName);
+    InitializeObjectAttributes(&ObjectAttributes,
+                               &String,
+                               OBJ_CASE_INSENSITIVE,
+                               NULL,
+                               NULL);
+
+    /* Open the registry key */
+    Status = NtOpenKey(&KeyHandle, KEY_READ, &ObjectAttributes);
+    if (NT_SUCCESS(Status))
+    {
+        /* Query the value */
+        RtlInitUnicodeString(&String, ValueName);
+        Status = NtQueryValueKey(KeyHandle,
+                                 &String,
+                                 KeyValuePartialInformation,
+                                 ValueInfo,
+                                 sizeof(ValueBuffer),
+                                 &ResultLength);
+
+        /* Close the registry key */
+        NtClose(KeyHandle);
+
+        if (NT_SUCCESS(Status) && (ValueInfo->Type == REG_DWORD))
+        {
+            /* Directly retrieve the data */
+            Value = *(PULONG)ValueInfo->Data;
+        }
+    }
+
+    return Value;
 }
 
 static BOOL
-UserpShowInformationBalloon(PWSTR Text,
-                            PWSTR Caption,
-                            PHARDERROR_MSG Message)
+UserpShowInformationBalloon(
+    IN PCWSTR Text,
+    IN PCWSTR Caption,
+    IN UINT   Type,
+    IN PHARDERROR_MSG Message)
 {
     ULONG ShellErrorMode;
     HWND hwnd;
@@ -570,7 +887,7 @@ UserpShowInformationBalloon(PWSTR Text,
     if (NT_SUCCESS(Message->Status))
         pdata->dwType = MB_OK;
     else if (Message->Status == STATUS_SERVICE_NOTIFICATION)
-        pdata->dwType = Message->Parameters[2];
+        pdata->dwType = Type;
     else
         pdata->dwType = MB_ICONINFORMATION;
 
@@ -596,118 +913,18 @@ UserpShowInformationBalloon(PWSTR Text,
     return (ret && dwResult) ? TRUE : FALSE;
 }
 
-
-static ULONG
-GetRegInt(
-    IN PCWSTR KeyName,
-    IN PCWSTR ValueName,
-    IN ULONG  DefaultValue)
-{
-    NTSTATUS Status;
-    ULONG Value = DefaultValue;
-    UNICODE_STRING String;
-    OBJECT_ATTRIBUTES ObjectAttributes;
-    HANDLE KeyHandle;
-    ULONG ResultLength;
-    UCHAR ValueBuffer[sizeof(KEY_VALUE_PARTIAL_INFORMATION) + sizeof(ULONG)];
-    PKEY_VALUE_PARTIAL_INFORMATION ValueInfo = (PKEY_VALUE_PARTIAL_INFORMATION)ValueBuffer;
-
-    RtlInitUnicodeString(&String, KeyName);
-    InitializeObjectAttributes(&ObjectAttributes,
-                               &String,
-                               OBJ_CASE_INSENSITIVE,
-                               NULL,
-                               NULL);
-
-    /* Open the registry key */
-    Status = NtOpenKey(&KeyHandle, KEY_READ, &ObjectAttributes);
-    if (NT_SUCCESS(Status))
-    {
-        /* Query the value */
-        RtlInitUnicodeString(&String, ValueName);
-        Status = NtQueryValueKey(KeyHandle,
-                                 &String,
-                                 KeyValuePartialInformation,
-                                 ValueInfo,
-                                 sizeof(ValueBuffer),
-                                 &ResultLength);
-
-        /* Close the registry key */
-        NtClose(KeyHandle);
-
-        if (NT_SUCCESS(Status) && (ValueInfo->Type == REG_DWORD))
-        {
-            /* Directly retrieve the data */
-            Value = *(PULONG)ValueInfo->Data;
-        }
-    }
-
-    return Value;
-}
-
 static
-ULONG
+HARDERROR_RESPONSE
 UserpMessageBox(
     IN PCWSTR Text,
     IN PCWSTR Caption,
-    IN ULONG ValidResponseOptions,
-    IN ULONG Severity,
-    IN ULONG Timeout)
+    IN UINT   Type,
+    IN ULONG  Timeout)
 {
-    ULONG Type, MessageBoxResponse;
+    ULONG MessageBoxResponse;
 
-    /* Set the message box type */
-    switch (ValidResponseOptions)
-    {
-        case OptionAbortRetryIgnore:
-            Type = MB_ABORTRETRYIGNORE;
-            break;
-        case OptionOk:
-            Type = MB_OK;
-            break;
-        case OptionOkCancel:
-            Type = MB_OKCANCEL;
-            break;
-        case OptionRetryCancel:
-            Type = MB_RETRYCANCEL;
-            break;
-        case OptionYesNo:
-            Type = MB_YESNO;
-            break;
-        case OptionYesNoCancel:
-            Type = MB_YESNOCANCEL;
-            break;
-        case OptionShutdownSystem:
-            Type = MB_RETRYCANCEL; // FIXME???
-            break;
-        case OptionOkNoWait:
-            /*
-             * At that point showing the balloon failed. Is that correct?
-             */
-            Type = MB_OK; // FIXME!
-            break;
-        case OptionCancelTryContinue:
-            Type = MB_CANCELTRYCONTINUE;
-            break;
-
-        /* Anything else is invalid */
-        default:
-        {
-            DPRINT1("Unknown ValidResponseOptions = %d\n", ValidResponseOptions);
-            return ResponseNotHandled;
-        }
-    }
-
-    /* Set severity */
-    // STATUS_SEVERITY_SUCCESS
-         if (Severity == STATUS_SEVERITY_INFORMATIONAL) Type |= MB_ICONINFORMATION;
-    else if (Severity == STATUS_SEVERITY_WARNING)       Type |= MB_ICONWARNING;
-    else if (Severity == STATUS_SEVERITY_ERROR)         Type |= MB_ICONERROR;
-
-    Type |= MB_SYSTEMMODAL | MB_SETFOREGROUND;
-
-    DPRINT("Text = '%S', Caption = '%S', Severity = %d, Type = 0x%lx\n",
-           Text, Caption, Severity, Type);
+    DPRINT("Text = '%S', Caption = '%S', Type = 0x%lx\n",
+           Text, Caption, Type);
 
     /* Display a message box */
     MessageBoxResponse = MessageBoxTimeoutW(NULL, Text, Caption, Type, 0, Timeout);
@@ -724,6 +941,7 @@ UserpMessageBox(
         case IDRETRY:    return ResponseRetry;
         case IDTRYAGAIN: return ResponseTryAgain;
         case IDCONTINUE: return ResponseContinue;
+        default:         return ResponseNotHandled;
     }
 
     return ResponseNotHandled;
@@ -772,13 +990,11 @@ UserServerHardError(
     IN PCSR_THREAD ThreadData,
     IN PHARDERROR_MSG Message)
 {
-    ULONG_PTR Parameters[MAXIMUM_HARDERROR_PARAMETERS] = {0};
-    OBJECT_ATTRIBUTES ObjectAttributes;
-    UNICODE_STRING TextU, CaptionU;
-    NTSTATUS Status;
-    HANDLE hProcess;
-    ULONG Size;
     ULONG ErrorMode;
+    UINT dwType = 0;
+    UNICODE_STRING TextU, CaptionU;
+    WCHAR LocalTextBuffer[256];
+    WCHAR LocalCaptionBuffer[256];
 
     ASSERT(ThreadData->Process != NULL);
 
@@ -788,54 +1004,32 @@ UserServerHardError(
     /* Make sure we don't have too many parameters */
     if (Message->NumberOfParameters > MAXIMUM_HARDERROR_PARAMETERS)
     {
-        // FIXME: Windows just fails (STATUS_INVALID_PARAMETER) & returns ResponseNotHandled.
+        // NOTE: Windows just fails (STATUS_INVALID_PARAMETER) & returns ResponseNotHandled.
+        DPRINT1("Invalid NumberOfParameters = %d\n", Message->NumberOfParameters);
         Message->NumberOfParameters = MAXIMUM_HARDERROR_PARAMETERS;
     }
     if (Message->ValidResponseOptions > OptionCancelTryContinue)
     {
-        // STATUS_INVALID_PARAMETER;
-        Message->Response = ResponseNotHandled;
-        return;
+        DPRINT1("Unknown ValidResponseOptions = %d\n", Message->ValidResponseOptions);
+        return; // STATUS_INVALID_PARAMETER;
     }
-    // TODO: More message validation: check NumberOfParameters wrt. Message Status code.
-
-    /* Initialize object attributes */
-    InitializeObjectAttributes(&ObjectAttributes, NULL, 0, NULL, NULL);
-
-    /* Open client process */
-    Status = NtOpenProcess(&hProcess,
-                           PROCESS_VM_READ | PROCESS_QUERY_INFORMATION,
-                           &ObjectAttributes,
-                           &Message->h.ClientId);
-    if (!NT_SUCCESS(Status))
-    {
-        DPRINT1("NtOpenProcess failed with status 0x%08lx\n", Status);
-        return;
-    }
-
-    /* Capture all string parameters from the process memory */
-    Status = UserpCaptureStringParameters(Parameters, &Size, Message, hProcess);
-    if (!NT_SUCCESS(Status))
+    if (Message->Status == STATUS_SERVICE_NOTIFICATION)
     {
-        NtClose(hProcess);
-        return;
+        if (Message->NumberOfParameters < 3)
+        {
+            DPRINT1("Invalid NumberOfParameters = %d for STATUS_SERVICE_NOTIFICATION\n", Message->NumberOfParameters);
+            return; // STATUS_INVALID_PARAMETER;
+        }
+        // (Message->UnicodeStringParameterMask & 0x3)
     }
 
-    /* Format the caption and message box text */
-    Status = UserpFormatMessages(&TextU,
-                                 &CaptionU,
-                                 Parameters,
-                                 Size,
-                                 Message,
-                                 hProcess);
+    /* Re-initialize the hard errors cache */
+    UserInitHardErrorsCache();
 
-    /* Cleanup */
-    UserpFreeStringParameters(Parameters, Message);
-    NtClose(hProcess);
-
-    /* If we failed, bail out */
-    if (!NT_SUCCESS(Status))
-        return;
+    /* Format the message caption and text */
+    RtlInitEmptyUnicodeString(&TextU, LocalTextBuffer, sizeof(LocalTextBuffer));
+    RtlInitEmptyUnicodeString(&CaptionU, LocalCaptionBuffer, sizeof(LocalCaptionBuffer));
+    UserpFormatMessages(&TextU, &CaptionU, &dwType, /* &Timeout, */ Message);
 
     /* Log the hard error message */
     UserpLogHardError(&TextU, &CaptionU);
@@ -846,7 +1040,7 @@ UserServerHardError(
     ErrorMode = GetRegInt(L"\\Registry\\Machine\\System\\CurrentControlSet\\Control\\Windows",
                           L"ErrorMode", 0);
 
-    if (ErrorMode != 0)
+    if (Message->Status != STATUS_SERVICE_NOTIFICATION && ErrorMode != 0)
     {
         /* Returns OK for the hard error */
         Message->Response = ResponseOk;
@@ -859,6 +1053,7 @@ UserServerHardError(
         Message->Response = ResponseOk;
         if (UserpShowInformationBalloon(TextU.Buffer,
                                         CaptionU.Buffer,
+                                        dwType,
                                         Message))
         {
             Message->Response = ResponseOk;
@@ -869,15 +1064,58 @@ UserServerHardError(
     /* Display the message box */
     Message->Response = UserpMessageBox(TextU.Buffer,
                                         CaptionU.Buffer,
-                                        Message->ValidResponseOptions,
-                                        (ULONG)(Message->Status) >> 30,
+                                        dwType,
                                         (ULONG)-1);
 
 Quit:
-    RtlFreeUnicodeString(&TextU);
-    RtlFreeUnicodeString(&CaptionU);
+    /* Free the strings if they have been reallocated */
+    if (TextU.Buffer != LocalTextBuffer)
+        RtlFreeUnicodeString(&TextU);
+    if (CaptionU.Buffer != LocalCaptionBuffer)
+        RtlFreeUnicodeString(&CaptionU);
 
     return;
 }
 
+VOID
+UserInitHardErrorsCache(VOID)
+{
+    NTSTATUS Status;
+    LCID CurrentUserLCID = 0;
+
+    Status = NtQueryDefaultLocale(TRUE, &CurrentUserLCID);
+    if (!NT_SUCCESS(Status) || CurrentUserLCID == 0)
+    {
+        /* Fall back to english locale */
+        DPRINT1("NtQueryDefaultLocale failed with Status = 0x%08lx\n", Status);
+        // LOCALE_SYSTEM_DEFAULT;
+        CurrentUserLCID = MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT);
+    }
+    if (g_CurrentUserLangId == LANGIDFROMLCID(CurrentUserLCID))
+    {
+        /* The current lang ID and the hard error strings have already been cached */
+        return;
+    }
+
+    /* Load the strings using the current system locale */
+    RtlLoadUnicodeString(UserServerDllInstance, IDS_SEVERITY_SUCCESS,
+                         &g_SuccessU, L"Success");
+    RtlLoadUnicodeString(UserServerDllInstance, IDS_SEVERITY_INFORMATIONAL,
+                         &g_InformationU, L"System Information");
+    RtlLoadUnicodeString(UserServerDllInstance, IDS_SEVERITY_WARNING,
+                         &g_WarningU, L"System Warning");
+    RtlLoadUnicodeString(UserServerDllInstance, IDS_SEVERITY_ERROR,
+                         &g_ErrorU, L"System Error");
+    // "unknown software exception"
+    RtlLoadUnicodeString(UserServerDllInstance, IDS_SYSTEM_PROCESS,
+                         &g_SystemProcessU, L"System Process");
+    RtlLoadUnicodeString(UserServerDllInstance, IDS_OK_TERMINATE_PROGRAM,
+                         &g_OKTerminateU, L"Click on OK to terminate the program.");
+    RtlLoadUnicodeString(UserServerDllInstance, IDS_CANCEL_DEBUG_PROGRAM,
+                         &g_CancelDebugU, L"Click on CANCEL to debug the program.");
+
+    /* Remember that we cached the hard error strings */
+    g_CurrentUserLangId = LANGIDFROMLCID(CurrentUserLCID);
+}
+
 /* EOF */