Kernel32.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. namespace XUnity.AutoTranslator.Plugin.Core
  7. {
  8. internal static class Kernel32
  9. {
  10. [DllImport( "kernel32.dll", SetLastError = true )]
  11. public static extern bool AllocConsole();
  12. [DllImport( "kernel32.dll", SetLastError = false )]
  13. public static extern bool FreeConsole();
  14. [DllImport( "kernel32.dll", SetLastError = true )]
  15. public static extern IntPtr GetStdHandle( int nStdHandle );
  16. [DllImport( "kernel32.dll", SetLastError = true )]
  17. public static extern bool SetStdHandle( int nStdHandle, IntPtr hConsoleOutput );
  18. [DllImport( "kernel32.dll", CharSet = CharSet.Auto, SetLastError = true )]
  19. public static extern IntPtr CreateFile(
  20. string fileName,
  21. int desiredAccess,
  22. int shareMode,
  23. IntPtr securityAttributes,
  24. int creationDisposition,
  25. int flagsAndAttributes,
  26. IntPtr templateFile );
  27. [DllImport( "kernel32.dll", ExactSpelling = true, SetLastError = true )]
  28. public static extern bool CloseHandle( IntPtr handle );
  29. [DllImport( "kernel32.dll" )]
  30. public static extern IntPtr SetConsoleOutputCP( uint codepage );
  31. [DllImport( "kernel32.dll", SetLastError = true )]
  32. public static extern int WideCharToMultiByte(
  33. uint codePage,
  34. uint dwFlags,
  35. [In, MarshalAs( UnmanagedType.LPArray )] char[] lpWideCharStr,
  36. int cchWideChar,
  37. [Out, MarshalAs( UnmanagedType.LPArray )] byte[] lpMultiByteStr,
  38. int cbMultiByte,
  39. IntPtr lpDefaultChar,
  40. IntPtr lpUsedDefaultChar );
  41. [DllImport( "kernel32.dll", SetLastError = true )]
  42. public static extern int MultiByteToWideChar(
  43. uint codePage,
  44. uint dwFlags,
  45. [In, MarshalAs( UnmanagedType.LPArray )] byte[] lpMultiByteStr,
  46. int cbMultiByte,
  47. [Out, MarshalAs( UnmanagedType.LPArray )] char[] lpWideCharStr,
  48. int cchWideChar );
  49. [DllImport( "kernel32.dll" )]
  50. internal static extern bool VirtualProtect( IntPtr lpAddress, UIntPtr dwSize, Protection flNewProtect, out Protection lpflOldProtect );
  51. }
  52. /// <summary>A bit-field of flags for protections</summary>
  53. [Flags]
  54. internal enum Protection
  55. {
  56. /// <summary>No access</summary>
  57. PAGE_NOACCESS = 0x01,
  58. /// <summary>Read only</summary>
  59. PAGE_READONLY = 0x02,
  60. /// <summary>Read write</summary>
  61. PAGE_READWRITE = 0x04,
  62. /// <summary>Write copy</summary>
  63. PAGE_WRITECOPY = 0x08,
  64. /// <summary>No access</summary>
  65. PAGE_EXECUTE = 0x10,
  66. /// <summary>Execute read</summary>
  67. PAGE_EXECUTE_READ = 0x20,
  68. /// <summary>Execute read write</summary>
  69. PAGE_EXECUTE_READWRITE = 0x40,
  70. /// <summary>Execute write copy</summary>
  71. PAGE_EXECUTE_WRITECOPY = 0x80,
  72. /// <summary>guard</summary>
  73. PAGE_GUARD = 0x100,
  74. /// <summary>No cache</summary>
  75. PAGE_NOCACHE = 0x200,
  76. /// <summary>Write combine</summary>
  77. PAGE_WRITECOMBINE = 0x400
  78. }
  79. }