ConsoleEncoding.PInvoke.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. 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.LPArray)] char[] lpWideCharStr,
  29. int cchWideChar);
  30. [DllImport("kernel32.dll")]
  31. private static extern IntPtr SetConsoleOutputCP(uint codepage);
  32. [DllImport("kernel32.dll", SetLastError = true)]
  33. private static extern int WideCharToMultiByte(
  34. uint codePage,
  35. uint dwFlags,
  36. [In, MarshalAs(UnmanagedType.LPArray)] char[] lpWideCharStr,
  37. int cchWideChar,
  38. [Out, MarshalAs(UnmanagedType.LPArray)] byte[] lpMultiByteStr,
  39. int cbMultiByte,
  40. IntPtr lpDefaultChar,
  41. IntPtr lpUsedDefaultChar);
  42. }
  43. }