NamedPipeHelper.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. //
  2. // System.Runtime.Remoting.Channels.Ipc.Win32.NamedPipeHelper.cs
  3. //
  4. // Author: Robert Jordan ([email protected])
  5. //
  6. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  7. //
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. #if NET_2_0
  29. using System;
  30. using System.Runtime.InteropServices;
  31. using System.Text;
  32. namespace System.Runtime.Remoting.Channels.Ipc.Win32
  33. {
  34. /// <summary>
  35. /// Named Pipe P/Invoke declarations.
  36. /// </summary>
  37. internal sealed class NamedPipeHelper
  38. {
  39. NamedPipeHelper()
  40. {
  41. }
  42. /// <summary>
  43. /// Returns a properly formatted local pipe name.
  44. /// </summary>
  45. /// <param name="pipeName"></param>
  46. /// <returns></returns>
  47. public static string FormatPipeName(string pipeName)
  48. {
  49. return String.Format(@"\\.\pipe\{0}", pipeName);
  50. }
  51. #region P/Invoke
  52. // Named pipe acces flags
  53. public const uint PIPE_ACCESS_INBOUND = 1;
  54. public const uint PIPE_ACCESS_OUTBOUND = 2;
  55. public const uint PIPE_ACCESS_DUPLEX = 3;
  56. // Named pipe wait states
  57. public const uint PIPE_WAIT = 0;
  58. public const uint PIPE_NOWAIT = 1;
  59. // Named pipe message types
  60. public const uint PIPE_TYPE_BYTE = 0;
  61. public const uint PIPE_TYPE_MESSAGE = 4;
  62. // Named pipe message read modes
  63. public const uint PIPE_READMODE_BYTE = 0;
  64. public const uint PIPE_READMODE_MESSAGE = 2;
  65. // Named pipe endpoints
  66. public const uint PIPE_CLIENT_END = 0;
  67. public const uint PIPE_SERVER_END = 1;
  68. // Named pipe misc flags
  69. public const uint PIPE_UNLIMITED_INSTANCES = 255;
  70. // Named pipe wait flags
  71. public const uint NMPWAIT_USE_DEFAULT_WAIT = 0;
  72. public const uint NMPWAIT_NOWAIT = 1;
  73. public const uint NMPWAIT_WAIT_FOREVER = 0xffffffff;
  74. // Create flags
  75. public const uint CREATE_NEW = 1;
  76. public const uint CREATE_ALWAYS = 2;
  77. public const uint OPEN_EXISTING = 3;
  78. public const uint OPEN_ALWAYS = 4;
  79. public const uint TRUNCATE_EXISTING = 5;
  80. // Access flags
  81. public const uint GENERIC_READ = 0x80000000;
  82. public const uint GENERIC_WRITE = 0x40000000;
  83. public const uint GENERIC_EXECUTE = 0x20000000;
  84. public const uint GENERIC_ALL = 0x10000000;
  85. // Error results
  86. public const int ERROR_FILE_NOT_FOUND = 2;
  87. public const int ERROR_PIPE_BUSY = 231;
  88. public const int ERROR_NO_DATA = 232;
  89. public const int ERROR_PIPE_NOT_CONNECTED = 233;
  90. public const int ERROR_PIPE_CONNECTED = 535;
  91. public const int ERROR_PIPE_LISTENING = 536;
  92. public const int INVALID_HANDLE_VALUE = -1;
  93. [DllImport("kernel32.dll", SetLastError = true)]
  94. public static extern IntPtr CreateNamedPipe(
  95. string lpName,
  96. uint dwOpenMode,
  97. uint dwPipeMode,
  98. uint nMaxInstances,
  99. uint nOutBufferSize,
  100. uint nInBufferSize,
  101. uint nDefaultTimeOut,
  102. IntPtr pipeSecurityDescriptor
  103. );
  104. [DllImport("kernel32.dll", SetLastError = true)]
  105. public static extern bool ConnectNamedPipe(
  106. IntPtr hPipe,
  107. IntPtr lpOverlapped
  108. );
  109. [DllImport("kernel32.dll", SetLastError = true)]
  110. public static extern IntPtr CreateFile(
  111. String lpFileName,
  112. uint dwDesiredAccess,
  113. uint dwShareMode,
  114. IntPtr attr,
  115. uint dwCreationDisposition,
  116. uint dwFlagsAndAttributes,
  117. IntPtr hTemplateFile);
  118. [DllImport("kernel32.dll", SetLastError = true)]
  119. public static extern bool ReadFile(
  120. IntPtr hHandle,
  121. IntPtr lpBuffer,
  122. uint nNumberOfBytesToRead,
  123. out uint lpNumberOfBytesRead,
  124. IntPtr lpOverlapped
  125. );
  126. [DllImport("kernel32.dll", SetLastError = true)]
  127. public static extern bool WriteFile(
  128. IntPtr hHandle,
  129. IntPtr lpBuffer,
  130. uint nNumberOfBytesToWrite,
  131. out uint lpNumberOfBytesWritten,
  132. IntPtr lpOverlapped
  133. );
  134. [DllImport("kernel32.dll", SetLastError = true)]
  135. public static extern bool GetNamedPipeHandleState(
  136. IntPtr hPipe,
  137. out int lpState,
  138. out int lpCurInstances,
  139. out int lpMaxCollectionCount,
  140. out int lpCollectDataTimeout,
  141. StringBuilder lpUserName,
  142. int nMaxUserNameSize
  143. );
  144. [DllImport("kernel32.dll", SetLastError = true)]
  145. public static extern bool SetNamedPipeHandleState(
  146. IntPtr hPipe,
  147. ref uint lpMode,
  148. ref uint lpMaxCollectionCount,
  149. ref uint lpCollectDataTimeout
  150. );
  151. [DllImport("kernel32.dll", SetLastError = true)]
  152. public static extern bool GetNamedPipeInfo(
  153. IntPtr hPipe,
  154. out int lpFlags,
  155. out int lpOutBufferSize,
  156. out int lpInBufferSize,
  157. out int lpMaxInstances
  158. );
  159. [DllImport("kernel32.dll", SetLastError = true)]
  160. public static extern bool PeekNamedPipe(
  161. IntPtr hPipe,
  162. IntPtr lpBuffer,
  163. uint nBufferSize,
  164. out uint lpBytesRead,
  165. out uint lpTotalBytesAvail,
  166. out uint lpBytesLeftThisMessage
  167. );
  168. [DllImport("kernel32.dll", SetLastError = true)]
  169. public static extern bool WaitNamedPipe(
  170. string name,
  171. int timeout
  172. );
  173. [DllImport("kernel32.dll", SetLastError = true)]
  174. public static extern bool DisconnectNamedPipe(
  175. IntPtr hPipe
  176. );
  177. [DllImport("kernel32.dll", SetLastError = true)]
  178. public static extern bool FlushFileBuffers(
  179. IntPtr hFile
  180. );
  181. [DllImport("kernel32.dll", SetLastError = true)]
  182. public static extern bool CloseHandle(
  183. IntPtr hHandle
  184. );
  185. [DllImport("advapi32.dll", SetLastError = true)]
  186. public static extern bool ImpersonateNamedPipeClient(
  187. IntPtr hPipe
  188. );
  189. [DllImport("advapi32.dll", SetLastError = true)]
  190. public static extern bool RevertToSelf();
  191. #endregion
  192. }
  193. }
  194. #endif