2 // System.Runtime.Remoting.Channels.Ipc.Win32.NamedPipeHelper.cs
4 // Author: Robert Jordan (robertj@gmx.net)
6 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
32 using System.Runtime.InteropServices;
35 namespace System.Runtime.Remoting.Channels.Ipc.Win32
38 /// Named Pipe P/Invoke declarations.
40 internal sealed class NamedPipeHelper
47 /// Returns a properly formatted local pipe name.
49 /// <param name="pipeName"></param>
50 /// <returns></returns>
51 public static string FormatPipeName(string pipeName)
53 return String.Format(@"\\.\pipe\{0}", pipeName);
58 // Named pipe acces flags
59 public const uint PIPE_ACCESS_INBOUND = 1;
60 public const uint PIPE_ACCESS_OUTBOUND = 2;
61 public const uint PIPE_ACCESS_DUPLEX = 3;
63 // Named pipe wait states
64 public const uint PIPE_WAIT = 0;
65 public const uint PIPE_NOWAIT = 1;
67 // Named pipe message types
68 public const uint PIPE_TYPE_BYTE = 0;
69 public const uint PIPE_TYPE_MESSAGE = 4;
71 // Named pipe message read modes
72 public const uint PIPE_READMODE_BYTE = 0;
73 public const uint PIPE_READMODE_MESSAGE = 2;
75 // Named pipe endpoints
76 public const uint PIPE_CLIENT_END = 0;
77 public const uint PIPE_SERVER_END = 1;
79 // Named pipe misc flags
80 public const uint PIPE_UNLIMITED_INSTANCES = 255;
82 // Named pipe wait flags
83 public const uint NMPWAIT_USE_DEFAULT_WAIT = 0;
84 public const uint NMPWAIT_NOWAIT = 1;
85 public const uint NMPWAIT_WAIT_FOREVER = 0xffffffff;
88 public const uint CREATE_NEW = 1;
89 public const uint CREATE_ALWAYS = 2;
90 public const uint OPEN_EXISTING = 3;
91 public const uint OPEN_ALWAYS = 4;
92 public const uint TRUNCATE_EXISTING = 5;
95 public const uint GENERIC_READ = 0x80000000;
96 public const uint GENERIC_WRITE = 0x40000000;
97 public const uint GENERIC_EXECUTE = 0x20000000;
98 public const uint GENERIC_ALL = 0x10000000;
101 public const int ERROR_FILE_NOT_FOUND = 2;
102 public const int ERROR_PIPE_BUSY = 231;
103 public const int ERROR_NO_DATA = 232;
104 public const int ERROR_PIPE_NOT_CONNECTED = 233;
105 public const int ERROR_PIPE_CONNECTED = 535;
106 public const int ERROR_PIPE_LISTENING = 536;
108 public const int INVALID_HANDLE_VALUE = -1;
110 [DllImport("kernel32.dll", SetLastError = true)]
111 public static extern IntPtr CreateNamedPipe(
118 uint nDefaultTimeOut,
119 IntPtr pipeSecurityDescriptor
122 [DllImport("kernel32.dll", SetLastError = true)]
123 public static extern bool ConnectNamedPipe(
128 [DllImport("kernel32.dll", SetLastError = true)]
129 public static extern IntPtr CreateFile(
131 uint dwDesiredAccess,
134 uint dwCreationDisposition,
135 uint dwFlagsAndAttributes,
136 IntPtr hTemplateFile);
138 [DllImport("kernel32.dll", SetLastError = true)]
139 public static extern bool ReadFile(
142 uint nNumberOfBytesToRead,
143 out uint lpNumberOfBytesRead,
147 [DllImport("kernel32.dll", SetLastError = true)]
148 public static extern bool WriteFile(
151 uint nNumberOfBytesToWrite,
152 out uint lpNumberOfBytesWritten,
156 [DllImport("kernel32.dll", SetLastError = true)]
157 public static extern bool GetNamedPipeHandleState(
160 out int lpCurInstances,
161 out int lpMaxCollectionCount,
162 out int lpCollectDataTimeout,
163 StringBuilder lpUserName,
167 [DllImport("kernel32.dll", SetLastError = true)]
168 public static extern bool SetNamedPipeHandleState(
171 ref uint lpMaxCollectionCount,
172 ref uint lpCollectDataTimeout
175 [DllImport("kernel32.dll", SetLastError = true)]
176 public static extern bool GetNamedPipeInfo(
179 out int lpOutBufferSize,
180 out int lpInBufferSize,
181 out int lpMaxInstances
184 [DllImport("kernel32.dll", SetLastError = true)]
185 public static extern bool PeekNamedPipe(
189 out uint lpBytesRead,
190 out uint lpTotalBytesAvail,
191 out uint lpBytesLeftThisMessage
194 [DllImport("kernel32.dll", SetLastError = true)]
195 public static extern bool WaitNamedPipe(
200 [DllImport("kernel32.dll", SetLastError = true)]
201 public static extern bool DisconnectNamedPipe(
205 [DllImport("kernel32.dll", SetLastError = true)]
206 public static extern bool FlushFileBuffers(
210 [DllImport("kernel32.dll", SetLastError = true)]
211 public static extern bool CloseHandle(
215 [DllImport("advapi32.dll", SetLastError = true)]
216 public static extern bool ImpersonateNamedPipeClient(
220 [DllImport("advapi32.dll", SetLastError = true)]
221 public static extern bool RevertToSelf();