You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
7.6 KiB

  1. #if NET20
  2. using Apewer.Internals.Interop;
  3. using Microsoft.Win32.SafeHandles;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Runtime.ConstrainedExecution;
  8. using System.Runtime.InteropServices;
  9. using System.Security;
  10. using System.Text;
  11. using System.Threading;
  12. namespace Microsoft.Win32
  13. {
  14. [SuppressUnmanagedCodeSecurity]
  15. internal static class UnsafeNativeMethods
  16. {
  17. #region constants
  18. internal static readonly IntPtr NULL = IntPtr.Zero;
  19. #endregion
  20. #region dll methods
  21. [DllImport("kernel32.dll", SetLastError = true)]
  22. [return: MarshalAs(UnmanagedType.Bool)]
  23. internal unsafe static extern bool ConnectNamedPipe(SafePipeHandle handle, NativeOverlapped* overlapped);
  24. [DllImport("kernel32.dll", SetLastError = true)]
  25. [return: MarshalAs(UnmanagedType.Bool)]
  26. internal static extern bool ConnectNamedPipe(SafePipeHandle handle, IntPtr overlapped);
  27. [DllImport("kernel32.dll", BestFitMapping = false, CharSet = CharSet.Auto, SetLastError = true)]
  28. internal static extern SafePipeHandle CreateNamedPipe(string pipeName, int openMode, int pipeMode, int maxInstances, int outBufferSize, int inBufferSize, int defaultTimeout, SECURITY_ATTRIBUTES securityAttributes);
  29. [DllImport("kernel32.dll", BestFitMapping = false, CharSet = CharSet.Auto, EntryPoint = "CreateFile", SetLastError = true)]
  30. internal static extern SafePipeHandle CreateNamedPipeClient(string lpFileName, int dwDesiredAccess, FileShare dwShareMode, SECURITY_ATTRIBUTES securityAttrs, FileMode dwCreationDisposition, int dwFlagsAndAttributes, IntPtr hTemplateFile);
  31. [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  32. [return: MarshalAs(UnmanagedType.Bool)]
  33. internal static extern bool CreatePipe(out SafePipeHandle hReadPipe, out SafePipeHandle hWritePipe, SECURITY_ATTRIBUTES lpPipeAttributes, int nSize);
  34. [DllImport("kernel32.dll", SetLastError = true)]
  35. [return: MarshalAs(UnmanagedType.Bool)]
  36. internal static extern bool DisconnectNamedPipe(SafePipeHandle hNamedPipe);
  37. [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  38. [return: MarshalAs(UnmanagedType.Bool)]
  39. internal static extern bool DuplicateHandle(IntPtr hSourceProcessHandle, SafePipeHandle hSourceHandle, IntPtr hTargetProcessHandle, out SafePipeHandle lpTargetHandle, uint dwDesiredAccess, [MarshalAs(UnmanagedType.Bool)] bool bInheritHandle, uint dwOptions);
  40. [DllImport("kernel32.dll", SetLastError = true)]
  41. [return: MarshalAs(UnmanagedType.Bool)]
  42. internal static extern bool FlushFileBuffers(SafePipeHandle hNamedPipe);
  43. [DllImport("kernel32.dll", BestFitMapping = false, CharSet = CharSet.Auto)]
  44. internal static extern int FormatMessage(int dwFlags, IntPtr lpSource, int dwMessageId, int dwLanguageId, StringBuilder lpBuffer, int nSize, IntPtr va_list_arguments);
  45. [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  46. internal static extern IntPtr GetCurrentProcess();
  47. [DllImport("kernel32.dll")]
  48. internal static extern int GetFileType(SafePipeHandle handle);
  49. [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  50. [return: MarshalAs(UnmanagedType.Bool)]
  51. internal static extern bool GetNamedPipeHandleState(SafePipeHandle hNamedPipe, IntPtr lpState, out int lpCurInstances, IntPtr lpMaxCollectionCount, IntPtr lpCollectDataTimeout, IntPtr lpUserName, int nMaxUserNameSize);
  52. [DllImport("kernel32.dll", BestFitMapping = false, CharSet = CharSet.Auto, SetLastError = true)]
  53. [return: MarshalAs(UnmanagedType.Bool)]
  54. internal static extern bool GetNamedPipeHandleState(SafePipeHandle hNamedPipe, IntPtr lpState, IntPtr lpCurInstances, IntPtr lpMaxCollectionCount, IntPtr lpCollectDataTimeout, StringBuilder lpUserName, int nMaxUserNameSize);
  55. [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  56. [return: MarshalAs(UnmanagedType.Bool)]
  57. internal static extern bool GetNamedPipeHandleState(SafePipeHandle hNamedPipe, out int lpState, IntPtr lpCurInstances, IntPtr lpMaxCollectionCount, IntPtr lpCollectDataTimeout, IntPtr lpUserName, int nMaxUserNameSize);
  58. [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  59. [return: MarshalAs(UnmanagedType.Bool)]
  60. internal static extern bool GetNamedPipeInfo(SafePipeHandle hNamedPipe, out int lpFlags, IntPtr lpOutBufferSize, IntPtr lpInBufferSize, IntPtr lpMaxInstances);
  61. [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  62. [return: MarshalAs(UnmanagedType.Bool)]
  63. internal static extern bool GetNamedPipeInfo(SafePipeHandle hNamedPipe, IntPtr lpFlags, IntPtr lpOutBufferSize, out int lpInBufferSize, IntPtr lpMaxInstances);
  64. [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  65. [return: MarshalAs(UnmanagedType.Bool)]
  66. internal static extern bool GetNamedPipeInfo(SafePipeHandle hNamedPipe, IntPtr lpFlags, out int lpOutBufferSize, IntPtr lpInBufferSize, IntPtr lpMaxInstances);
  67. [DllImport("advapi32.dll", SetLastError = true)]
  68. [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
  69. [return: MarshalAs(UnmanagedType.Bool)]
  70. internal static extern bool ImpersonateNamedPipeClient(SafePipeHandle hNamedPipe);
  71. [DllImport("kernel32.dll", SetLastError = true)]
  72. internal unsafe static extern int ReadFile(SafePipeHandle handle, byte* bytes, int numBytesToRead, out int numBytesRead, IntPtr mustBeZero);
  73. [DllImport("kernel32.dll", SetLastError = true)]
  74. internal unsafe static extern int ReadFile(SafePipeHandle handle, byte* bytes, int numBytesToRead, IntPtr numBytesRead_mustBeZero, NativeOverlapped* overlapped);
  75. [DllImport("advapi32.dll", SetLastError = true)]
  76. [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
  77. [return: MarshalAs(UnmanagedType.Bool)]
  78. internal static extern bool RevertToSelf();
  79. [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  80. [return: MarshalAs(UnmanagedType.Bool)]
  81. internal unsafe static extern bool SetNamedPipeHandleState(SafePipeHandle hNamedPipe, int* lpMode, IntPtr lpMaxCollectionCount, IntPtr lpCollectDataTimeout);
  82. [DllImport("kernel32.dll", BestFitMapping = false, CharSet = CharSet.Auto, SetLastError = true)]
  83. [return: MarshalAs(UnmanagedType.Bool)]
  84. public static extern bool WaitNamedPipe(string name, int timeout);
  85. [DllImport("kernel32.dll", SetLastError = true)]
  86. internal unsafe static extern int WriteFile(SafePipeHandle handle, byte* bytes, int numBytesToWrite, out int numBytesWritten, IntPtr mustBeZero);
  87. [DllImport("kernel32.dll", SetLastError = true)]
  88. internal unsafe static extern int WriteFile(SafePipeHandle handle, byte* bytes, int numBytesToWrite, IntPtr numBytesWritten_mustBeZero, NativeOverlapped* lpOverlapped);
  89. #endregion
  90. #region manaegd methods
  91. [SecurityCritical]
  92. internal static string GetMessage(int errorCode)
  93. {
  94. StringBuilder stringBuilder = new StringBuilder(512);
  95. if (FormatMessage(12800, NULL, errorCode, 0, stringBuilder, stringBuilder.Capacity, NULL) != 0)
  96. {
  97. return stringBuilder.ToString();
  98. }
  99. return "UnknownError_Num " + errorCode;
  100. }
  101. internal static int MakeHRFromErrorCode(int errorCode)
  102. {
  103. return -2147024896 | errorCode;
  104. }
  105. #endregion
  106. }
  107. }
  108. #endif