2010-01-21 Jonathan Pobst <monkey@jpobst.com>
[mono.git] / mcs / class / System.Runtime.Remoting / System.Runtime.Remoting.Channels.Ipc.Win32 / NamedPipeHelper.cs
1 //
2 // System.Runtime.Remoting.Channels.Ipc.Win32.NamedPipeHelper.cs
3 //
4 // Author: Robert Jordan (robertj@gmx.net)
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
29 #if NET_2_0
30
31 using System;
32 using System.Runtime.InteropServices;
33 using System.Text;
34
35 namespace System.Runtime.Remoting.Channels.Ipc.Win32
36 {
37     /// <summary>
38     /// Named Pipe P/Invoke declarations.
39     /// </summary>
40     internal sealed class NamedPipeHelper
41     {
42         NamedPipeHelper()
43         {
44         }
45
46         /// <summary>
47         /// Returns a properly formatted local pipe name.
48         /// </summary>
49         /// <param name="pipeName"></param>
50         /// <returns></returns>
51         public static string FormatPipeName(string pipeName) 
52         {
53             return String.Format(@"\\.\pipe\{0}", pipeName);
54         }
55
56         #region P/Invoke
57
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;
62
63         // Named pipe wait states
64         public const uint PIPE_WAIT = 0;
65         public const uint PIPE_NOWAIT = 1;
66
67         // Named pipe message types
68         public const uint PIPE_TYPE_BYTE = 0;
69         public const uint PIPE_TYPE_MESSAGE = 4;
70
71         // Named pipe message read modes
72         public const uint PIPE_READMODE_BYTE = 0;
73         public const uint PIPE_READMODE_MESSAGE = 2;
74
75         // Named pipe endpoints
76         public const uint PIPE_CLIENT_END = 0;
77         public const uint PIPE_SERVER_END = 1;
78
79         // Named pipe misc flags
80         public const uint PIPE_UNLIMITED_INSTANCES = 255;
81
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;
86
87         // Create flags
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;
93         
94         // Access flags
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;
99
100         // Error results
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;
107
108         public const int INVALID_HANDLE_VALUE = -1;
109
110         [DllImport("kernel32.dll", SetLastError = true)]
111         public static extern IntPtr CreateNamedPipe(
112             string lpName,
113             uint dwOpenMode,
114             uint dwPipeMode,
115             uint nMaxInstances,
116             uint nOutBufferSize,
117             uint nInBufferSize,
118             uint nDefaultTimeOut,
119             IntPtr pipeSecurityDescriptor
120             );
121
122         [DllImport("kernel32.dll", SetLastError = true)]
123         public static extern bool ConnectNamedPipe(
124             IntPtr hPipe,
125             IntPtr lpOverlapped
126             );
127
128         [DllImport("kernel32.dll", SetLastError = true)]
129         public static extern IntPtr CreateFile(
130             String lpFileName,
131             uint dwDesiredAccess,
132             uint dwShareMode,
133             IntPtr attr,
134             uint dwCreationDisposition,
135             uint dwFlagsAndAttributes,
136             IntPtr hTemplateFile);
137
138         [DllImport("kernel32.dll", SetLastError = true)]
139         public static extern bool ReadFile(
140             IntPtr hHandle,
141             IntPtr lpBuffer,
142             uint nNumberOfBytesToRead,
143             out uint lpNumberOfBytesRead,
144             IntPtr lpOverlapped
145             );
146
147         [DllImport("kernel32.dll", SetLastError = true)]
148         public static extern bool WriteFile(
149             IntPtr hHandle,
150             IntPtr lpBuffer,
151             uint nNumberOfBytesToWrite,
152             out uint lpNumberOfBytesWritten,
153             IntPtr lpOverlapped
154             );
155
156         [DllImport("kernel32.dll", SetLastError = true)]
157         public static extern bool GetNamedPipeHandleState(
158             IntPtr hPipe,
159             out int lpState,
160             out int lpCurInstances,
161             out int lpMaxCollectionCount,
162             out int lpCollectDataTimeout,
163             StringBuilder lpUserName,
164             int nMaxUserNameSize
165             );
166
167         [DllImport("kernel32.dll", SetLastError = true)]
168         public static extern bool SetNamedPipeHandleState(
169             IntPtr hPipe,
170             ref uint lpMode,
171             ref uint lpMaxCollectionCount,
172             ref uint lpCollectDataTimeout
173             );
174
175         [DllImport("kernel32.dll", SetLastError = true)]
176         public static extern bool GetNamedPipeInfo(
177             IntPtr hPipe,
178             out int lpFlags,
179             out int lpOutBufferSize,
180             out int lpInBufferSize,
181             out int lpMaxInstances
182             );
183
184         [DllImport("kernel32.dll", SetLastError = true)]
185         public static extern bool PeekNamedPipe(
186             IntPtr hPipe,
187             IntPtr lpBuffer,
188             uint nBufferSize,
189             out uint lpBytesRead,
190             out uint lpTotalBytesAvail,
191             out uint lpBytesLeftThisMessage
192             );
193
194         [DllImport("kernel32.dll", SetLastError = true)]
195         public static extern bool WaitNamedPipe(
196             string name,
197             int timeout
198             );
199
200         [DllImport("kernel32.dll", SetLastError = true)]
201         public static extern bool DisconnectNamedPipe(
202             IntPtr hPipe
203             );
204
205         [DllImport("kernel32.dll", SetLastError = true)]
206         public static extern bool FlushFileBuffers(
207             IntPtr hFile
208             );
209
210         [DllImport("kernel32.dll", SetLastError = true)]
211         public static extern bool CloseHandle(
212             IntPtr hHandle
213             );
214
215         [DllImport("advapi32.dll", SetLastError = true)]
216         public static extern bool ImpersonateNamedPipeClient(
217             IntPtr hPipe
218             );
219
220         [DllImport("advapi32.dll", SetLastError = true)]
221         public static extern bool RevertToSelf();
222
223         #endregion
224
225     }
226
227 }
228
229 #endif