Kernel32.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233
  1. using System;
  2. using System.Text;
  3. namespace Microsoft.Win32
  4. {
  5. /// <summary>
  6. /// Kernel32扩展
  7. /// </summary>
  8. public static partial class Util
  9. {
  10. /// <summary>
  11. /// 获取与指定的 Win32 错误代码关联的详细说明。
  12. /// </summary>
  13. /// <param name="nErrorCode">Win32 错误代码。</param>
  14. /// <returns>错误的详细说明。</returns>
  15. public static string GetErrorMessage(int nErrorCode)
  16. {
  17. StringBuilder lpBuffer = new StringBuilder(0x100);
  18. if (UnsafeNativeMethods.FormatMessage(NativeMethods.FORMAT_MESSAGE_IGNORE_INSERTS | NativeMethods.FORMAT_MESSAGE_FROM_SYSTEM | NativeMethods.FORMAT_MESSAGE_ARGUMENT_ARRAY, IntPtr.Zero, nErrorCode, 0, lpBuffer, lpBuffer.Capacity + 1, IntPtr.Zero) == 0)
  19. return ("Unknown error (0x" + Convert.ToString(nErrorCode, 0x10) + ")");
  20. int length = lpBuffer.Length;
  21. while (length > 0)
  22. {
  23. char ch = lpBuffer[length - 1];
  24. if ((ch > ' ') && (ch != '.'))
  25. break;
  26. length--;
  27. }
  28. return lpBuffer.ToString(0, length);
  29. }
  30. }
  31. }