ConsoleEncoding.PInvoke.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // --------------------------------------------------
  2. // UnityInjector - ConsoleEncoding.PInvoke.cs
  3. // Copyright (c) Usagirei 2015 - 2015
  4. // --------------------------------------------------
  5. using System;
  6. using System.Runtime.InteropServices;
  7. namespace UnityInjector.ConsoleUtil
  8. {
  9. // --------------------------------------------------
  10. // Code ported from
  11. // https://gist.github.com/asm256/9bfb88336a1433e2328a
  12. // Which in turn was seemingly ported from
  13. // http://jonskeet.uk/csharp/ebcdic/
  14. // using only safe (managed) code
  15. // --------------------------------------------------
  16. internal partial class ConsoleEncoding
  17. {
  18. [DllImport("kernel32.dll")]
  19. private static extern uint GetConsoleOutputCP();
  20. [DllImport("kernel32.dll")]
  21. private static extern uint GetACP();
  22. [DllImport("kernel32.dll", SetLastError = true)]
  23. private static extern int MultiByteToWideChar(
  24. uint codePage,
  25. uint dwFlags,
  26. [In, MarshalAs(UnmanagedType.LPArray)] byte[] lpMultiByteStr,
  27. int cbMultiByte,
  28. [Out, MarshalAs(UnmanagedType.LPWStr)]
  29. char[] lpWideCharStr,
  30. int cchWideChar);
  31. [DllImport("kernel32.dll")]
  32. private static extern IntPtr SetConsoleOutputCP(uint codepage);
  33. [DllImport("kernel32.dll", SetLastError = true)]
  34. private static extern int WideCharToMultiByte(
  35. uint codePage,
  36. uint dwFlags,
  37. [In, MarshalAs(UnmanagedType.LPWStr)] char[] lpWideCharStr,
  38. int cchWideChar,
  39. [Out, MarshalAs(UnmanagedType.LPArray)]
  40. byte[] lpMultiByteStr,
  41. int cbMultiByte,
  42. IntPtr lpDefaultChar,
  43. IntPtr lpUsedDefaultChar);
  44. }
  45. }