Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get English exception message instead of local language

On Windows 8 using Visual Studio 2012 RC on a german system, I get all my Exceptions localized to german, which effectively means I can't google anything useful for them. To solve this, I already used the following to change my IDE to english language:

Tools --> Options --> Internetional Settings --> Language --> English

Nevertheless, I get my exceptions in the localized german language. I tried changing the ThreadUI Culture in code using this code:

Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-us");

Sadly, in WinRT the Thread namespace is gone in WinRT. Therefore I tried:

System.Globalization.CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo("en-us");

I get the german exception message still. Does anyone know how to get the un-localized version of the exception messages?

like image 254
Akku Avatar asked Nov 21 '12 12:11

Akku


1 Answers

Your other option is to retrieve and display the Exception.HResult value, which can be searched upon and turned into a useful error message in English.

Another possibility, if these exceptions have Win32 codes, albeit a hack:

[DllImport("kernel32.dll",
           EntryPoint = "FormatMessageW",
           SetLastError = true,
           CharSet = CharSet.Auto)]
private static extern int FormatMessage(
    int dwFlags,
    IntPtr lpSource,
    int dwMessageId,
    int dwLanguageId,
    StringBuilder lpBuffer,
    int nSize,
    IntPtr[] Arguments);

// used like:
var builder = new StringBuilder(2048);
var res = FormatMessage(
    0x1000|0x0200/*System Message, Ignore Inserts*/,
    IntPtr.Zero,
    exception.HResult,
    new CultureInfo("en-US").LCID,
    builder,
    builder.Capacity,
    null);
 Console.WriteLine("{0}", builder.ToString());
 // throw new StackOverflowException()
 // "Recursion too deep; the stack overflowed."
like image 53
user7116 Avatar answered Sep 20 '22 03:09

user7116