public static string GetSystemMessage(int error, int localeId)
{
int langId = 0;
switch (localeId)
{
case LOCALE_SYSTEM_DEFAULT:
{
langId = GetSystemDefaultLangID();
break;
}
case LOCALE_USER_DEFAULT:
{
langId = GetUserDefaultLangID();
break;
}
default:
{
langId = (0xFFFF & localeId);
break;
}
}
IntPtr buffer = Marshal.AllocCoTaskMem(MAX_MESSAGE_LENGTH);
int result = FormatMessageW(
(int)FORMAT_MESSAGE_FROM_SYSTEM,
IntPtr.Zero,
error,
langId,
buffer,
MAX_MESSAGE_LENGTH-1,
IntPtr.Zero);
if (result > 0)
{
string msg = Marshal.PtrToStringUni(buffer);
Marshal.FreeCoTaskMem(buffer);
if (msg != null && msg.Length > 0)
{
return msg.Trim();
}
}
return String.Format("0x{0:X8}", error);
}