Exception messages in en-US only

StewartBW 1,235 Reputation points
2025-03-11T14:33:45.82+00:00

Hello

I've placed an exception sender in my VB.net app which in case of exceptions, will send them to me to check, seems on localized systems, the messages are not in en-US and I get them like this:

Exception Message: Erro genérico em GDI+.

Root Exception: System.Runtime.InteropServices.ExternalException (0x80004005): Erro genérico em GDI+.

How to get the exceptions internally as en-US only? Not going to change it globally in the app, the exceptions messages to the user should remain in localized language, just the one I need to send to myself, how to get it in English only?

Thanks.

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,800 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Jiachen Li-MSFT 33,781 Reputation points Microsoft External Staff
    2025-03-11T14:47:34.2+00:00

    Hi @StewartBW ,

    You can try temporarily switching to en-US.

    1. Save the current culture so you can restore it later.
    2. Temporarily set the thread's culture to en-US using Thread.CurrentThread.CurrentCulture.
    3. Send the exception message and stack trace.
    4. Restore the original culture to ensure the rest of the application behaves as expected.

    Best Regards.

    Jiachen Li


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Castorix31 87,806 Reputation points
    2025-03-14T08:47:45.1233333+00:00

    You can use FormatMessage API

    Test with some error codes :

                'Dim nError = &H80070005  ' E_ACCESSDENIED 
                'Dim nError = &H80004005  ' E_FAIL 
                Dim nError = &H8007000E  ' E_OUTOFMEMORY 
                'Dim nError = 3 ' ERROR_PATH_NOT_FOUND
                Dim ex As Exception = Marshal.GetExceptionForHR(nError)
                If (ex Is Nothing) Then
                    ex = New System.ComponentModel.Win32Exception(nError)
                End If
    
                MessageBox.Show("Original message: " + ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
    
                ' HRESULT => Win32 error
                If (nError And &HFFFF0000UI) = &H80070000UI Then
                    nError = nError And &HFFFF
                End If
                Dim sMessage = GetErrorMessageInEnglish(nError)
                MessageBox.Show("Message in english: " + sMessage, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
    
    

    with :

        Private Const FORMAT_MESSAGE_ALLOCATE_BUFFER As Integer = &H100
        Private Const FORMAT_MESSAGE_IGNORE_INSERTS As Integer = &H200
        Private Const FORMAT_MESSAGE_FROM_SYSTEM As Integer = &H1000
        Private Const FORMAT_MESSAGE_ARGUMENT_ARRAY As Integer = &H2000
    
        ' Language identifier for English (United States)
        Private Const LANG_ENGLISH_US As UInteger = &H409 ' 0x0409 (English - United States)
    
        <DllImport("Kernel32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)>
        Private Shared Function FormatMessage(dwFlags As Integer, lpSource As IntPtr, dwMessageId As Integer, dwLanguageId As UInteger,
                                              ByRef lpBuffer As IntPtr, nSize As Integer, Arguments As IntPtr) As Integer
        End Function
    
        <DllImport("Kernel32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)>
        Private Shared Function LocalFree(hMem As IntPtr) As IntPtr
        End Function
    
        Public Function GetErrorMessageInEnglish(nErrorCode As Integer) As String
            Dim pMessageBuffer As IntPtr = IntPtr.Zero
            Dim nFlags As Integer = FORMAT_MESSAGE_ALLOCATE_BUFFER Or FORMAT_MESSAGE_FROM_SYSTEM Or FORMAT_MESSAGE_IGNORE_INSERTS
            Dim nLangId As UInteger = LANG_ENGLISH_US
            Dim nSize As Integer = FormatMessage(nFlags, IntPtr.Zero, nErrorCode, nLangId, pMessageBuffer, 0, IntPtr.Zero)
            If nSize = 0 Then
                Return $"Unknown error code: {nErrorCode} (FormatMessage failed)"
            End If
            Dim sMessage As String = Marshal.PtrToStringUni(pMessageBuffer)
            LocalFree(pMessageBuffer)
            Return sMessage.Trim()
        End Function
    
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.