/***********************************************************************
*
* Copyright (C) 2005-2006 Novell, Inc. All Rights Reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; version 2.1
* of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, Novell, Inc.
*
* To contact Novell about this file by physical or electronic mail,
* you may find current contact information at www.novell.com.
*
***********************************************************************/
using System;
using System.Security;
using System.Runtime.InteropServices;
namespace AppModule.NamedPipes {
#region Comments
///
/// This utility class exposes kernel32.dll methods for named pipes communication.
///
///
/// Use the following links for complete information about the exposed methods:
///
/// -
/// Named Pipe Functions
///
/// -
/// File Management Functions
///
/// -
/// Handle and Object Functions
///
/// -
/// System Error Codes
///
///
///
#endregion
[SuppressUnmanagedCodeSecurity]
public sealed class NamedPipeNative {
#region Comments
///
/// Outbound pipe access.
///
#endregion
public const uint PIPE_ACCESS_OUTBOUND = 0x00000002;
#region Comments
///
/// Duplex pipe access.
///
#endregion
public const uint PIPE_ACCESS_DUPLEX = 0x00000003;
#region Comments
///
/// Inbound pipe access.
///
#endregion
public const uint PIPE_ACCESS_INBOUND = 0x00000001;
#region Comments
///
/// Pipe blocking mode.
///
#endregion
public const uint PIPE_WAIT = 0x00000000;
#region Comments
///
/// Pipe non-blocking mode.
///
#endregion
public const uint PIPE_NOWAIT = 0x00000001;
#region Comments
///
/// Pipe read mode of type Byte.
///
#endregion
public const uint PIPE_READMODE_BYTE = 0x00000000;
#region Comments
///
/// Pipe read mode of type Message.
///
#endregion
public const uint PIPE_READMODE_MESSAGE = 0x00000002;
#region Comments
///
/// Byte pipe type.
///
#endregion
public const uint PIPE_TYPE_BYTE = 0x00000000;
#region Comments
///
/// Message pipe type.
///
#endregion
public const uint PIPE_TYPE_MESSAGE = 0x00000004;
#region Comments
///
/// Pipe client end.
///
#endregion
public const uint PIPE_CLIENT_END = 0x00000000;
#region Comments
///
/// Pipe server end.
///
#endregion
public const uint PIPE_SERVER_END = 0x00000001;
#region Comments
///
/// Unlimited server pipe instances.
///
#endregion
public const uint PIPE_UNLIMITED_INSTANCES = 255;
#region Comments
///
/// Waits indefinitely when connecting to a pipe.
///
#endregion
public const uint NMPWAIT_WAIT_FOREVER = 0xffffffff;
#region Comments
///
/// Does not wait for the named pipe.
///
#endregion
public const uint NMPWAIT_NOWAIT = 0x00000001;
#region Comments
///
/// Uses the default time-out specified in a call to the CreateNamedPipe method.
///
#endregion
public const uint NMPWAIT_USE_DEFAULT_WAIT = 0x00000000;
#region Comments
///
///
///
#endregion
public const uint GENERIC_READ = (0x80000000);
#region Comments
///
/// Generic write access to the pipe.
///
#endregion
public const uint GENERIC_WRITE = (0x40000000);
#region Comments
///
/// Generic execute access to the pipe.
///
#endregion
public const uint GENERIC_EXECUTE = (0x20000000);
#region Comments
///
/// Read, write, and execute access.
///
#endregion
public const uint GENERIC_ALL = (0x10000000);
#region Comments
///
/// Create new file. Fails if the file exists.
///
#endregion
public const uint CREATE_NEW = 1;
#region Comments
///
/// Create new file. Overrides an existing file.
///
#endregion
public const uint CREATE_ALWAYS = 2;
#region Comments
///
/// Open existing file.
///
#endregion
public const uint OPEN_EXISTING = 3;
#region Comments
///
/// Open existing file. If the file does not exist, creates it.
///
#endregion
public const uint OPEN_ALWAYS = 4;
#region Comments
///
/// Opens the file and truncates it so that its size is zero bytes.
///
#endregion
public const uint TRUNCATE_EXISTING = 5;
#region Comments
///
/// Invalid operating system handle.
///
#endregion
public const int INVALID_HANDLE_VALUE = -1;
#region Comments
///
/// The operation completed successfully.
///
#endregion
public const ulong ERROR_SUCCESS = 0;
#region Comments
///
/// The system cannot find the file specified.
///
#endregion
public const ulong ERROR_CANNOT_CONNECT_TO_PIPE = 2;
#region Comments
///
/// All pipe instances are busy.
///
#endregion
public const ulong ERROR_PIPE_BUSY = 231;
#region Comments
///
/// The pipe is being closed.
///
#endregion
public const ulong ERROR_NO_DATA = 232;
#region Comments
///
/// No process is on the other end of the pipe.
///
#endregion
public const ulong ERROR_PIPE_NOT_CONNECTED = 233;
#region Comments
///
/// More data is available.
///
#endregion
public const ulong ERROR_MORE_DATA = 234;
#region Comments
///
/// There is a process on other end of the pipe.
///
#endregion
public const ulong ERROR_PIPE_CONNECTED = 535;
#region Comments
///
/// Waiting for a process to open the other end of the pipe.
///
#endregion
public const ulong ERROR_PIPE_LISTENING = 536;
#region Comments
///
/// Creates an instance of a named pipe and returns a handle for
/// subsequent pipe operations.
///
/// Pointer to the null-terminated string that
/// uniquely identifies the pipe.
/// Pipe access mode, the overlapped mode,
/// the write-through mode, and the security access mode of the pipe handle.
/// Type, read, and wait modes of the pipe handle.
/// Maximum number of instances that can be
/// created for this pipe.
/// Number of bytes to reserve for the output buffer.
/// Number of bytes to reserve for the input buffer.
/// Default time-out value, in milliseconds.
/// Pointer to a
/// SECURITY_ATTRIBUTES
/// object that specifies a security descriptor for the new named pipe.
/// If the function succeeds, the return value is a handle
/// to the server end of a named pipe instance.
#endregion
[DllImport("kernel32.dll", SetLastError=true)]
public static extern IntPtr CreateNamedPipe(
String lpName, // pipe name
uint dwOpenMode, // pipe open mode
uint dwPipeMode, // pipe-specific modes
uint nMaxInstances, // maximum number of instances
uint nOutBufferSize, // output buffer size
uint nInBufferSize, // input buffer size
uint nDefaultTimeOut, // time-out interval
IntPtr pipeSecurityDescriptor // SD
);
#region Comments
///
/// Enables a named pipe server process to wait for a client
/// process to connect to an instance of a named pipe.
///
/// Handle to the server end of a named pipe instance.
/// Pointer to an
/// Overlapped object.
/// If the function succeeds, the return value is nonzero.
#endregion
[DllImport("kernel32.dll", SetLastError=true)]
public static extern bool ConnectNamedPipe(
IntPtr hHandle, // handle to named pipe
Overlapped lpOverlapped // overlapped structure
);
#region Comments
///
/// Connects to a message-type pipe (and waits if an instance of the
/// pipe is not available), writes to and reads from the pipe, and then closes the pipe.
///
/// Pointer to a null-terminated string
/// specifying the pipe name.
/// Pointer to the buffer containing the data written
/// to the pipe.
/// Size of the write buffer, in bytes.
/// Pointer to the buffer that receives the data
/// read from the pipe.
/// Size of the read buffer, in bytes.
/// Pointer to a variable that receives the number
/// of bytes read from the pipe.
/// Number of milliseconds to wait for the
/// named pipe to be available.
/// If the function succeeds, the return value is nonzero.
#endregion
[DllImport("kernel32.dll", SetLastError=true)]
public static extern bool CallNamedPipe(
string lpNamedPipeName,
byte[] lpInBuffer,
uint nInBufferSize,
byte[] lpOutBuffer,
uint nOutBufferSize,
byte[] lpBytesRead,
int nTimeOut
);
#region Comments
///
/// Creates or opens a file, directory, physical disk, volume, console buffer,
/// tape drive, communications resource, mailslot, or named pipe.
///
/// Pointer to a null-terminated string that
/// specifies the name of the object to create or open.
/// Access to the object (reading, writing, or both).
/// Sharing mode of the object (reading, writing, both, or neither).
/// Pointer to a
/// SecurityAttributes
/// object that determines whether the returned handle can be inherited
/// by child processes.
/// Action to take on files that exist,
/// and which action to take when files do not exist.
/// File attributes and flags.
/// Handle to a template file, with the GENERIC_READ access right.
/// If the function succeeds, the return value is an open handle to the specified file.
#endregion
[DllImport("kernel32.dll", SetLastError=true)]
public static extern IntPtr CreateFile(
String lpFileName, // file name
uint dwDesiredAccess, // access mode
uint dwShareMode, // share mode
SecurityAttributes attr, // SD
uint dwCreationDisposition, // how to create
uint dwFlagsAndAttributes, // file attributes
uint hTemplateFile); // handle to template file
#region Comments
///
/// Reads data from a file, starting at the position indicated by the file pointer.
///
/// Handle to the file to be read.
/// Pointer to the buffer that receives the data read from the file.
/// Number of bytes to be read from the file.
/// Pointer to the variable that receives the number of bytes read.
/// Pointer to an
/// Overlapped object.
/// The ReadFile function returns when one of the following
/// conditions is met: a write operation completes on the write end of
/// the pipe, the number of bytes requested has been read, or an error occurs.
#endregion
[DllImport("kernel32.dll", SetLastError=true)]
public static extern bool ReadFile(
IntPtr hHandle, // handle to file
byte[] lpBuffer, // data buffer
uint nNumberOfBytesToRead, // number of bytes to read
byte[] lpNumberOfBytesRead, // number of bytes read
uint lpOverlapped // overlapped buffer
);
#region Comments
///
/// Writes data to a file at the position specified by the file pointer.
///
/// Handle to the file.
/// Pointer to the buffer containing the data to be written to the file.
///
/// Pointer to the variable that receives the number of bytes written.
/// Pointer to an
/// Overlapped object.
/// If the function succeeds, the return value is nonzero.
#endregion
[DllImport("kernel32.dll", SetLastError=true)]
public static extern bool WriteFile(
IntPtr hHandle, // handle to file
byte[] lpBuffer, // data buffer
uint nNumberOfBytesToWrite, // number of bytes to write
byte[] lpNumberOfBytesWritten, // number of bytes written
uint lpOverlapped // overlapped buffer
);
#region Comments
///
/// Retrieves information about a specified named pipe.
///
/// Handle to the named pipe for which information is wanted.
/// Pointer to a variable that indicates the current
/// state of the handle.
/// Pointer to a variable that receives the
/// number of current pipe instances.
/// Pointer to a variable that receives
/// the maximum number of bytes to be collected on the client's computer
/// before transmission to the server.
/// Pointer to a variable that receives
/// the maximum time, in milliseconds, that can pass before a remote named
/// pipe transfers information over the network.
/// Pointer to a buffer that receives the
/// null-terminated string containing the user name string associated
/// with the client application.
/// Size of the buffer specified by the
/// lpUserName parameter.
/// If the function succeeds, the return value is nonzero.
#endregion
[DllImport("kernel32.dll", SetLastError=true)]
public static extern bool GetNamedPipeHandleState(
IntPtr hHandle,
IntPtr lpState,
ref uint lpCurInstances,
IntPtr lpMaxCollectionCount,
IntPtr lpCollectDataTimeout,
IntPtr lpUserName,
IntPtr nMaxUserNameSize
);
#region Comments
///
/// Cancels all pending input and output (I/O) operations that were
/// issued by the calling thread for the specified file handle.
///
/// Handle to a file.
/// If the function succeeds, the return value is nonzero.
#endregion
[DllImport("kernel32.dll", SetLastError=true)]
public static extern bool CancelIo(
IntPtr hHandle
);
#region Comments
///
/// Waits until either a time-out interval elapses or an instance
/// of the specified named pipe is available for connection.
///
/// Pointer to a null-terminated string that specifies
/// the name of the named pipe.
/// Number of milliseconds that the function will
/// wait for an instance of the named pipe to be available.
/// If an instance of the pipe is available before the
/// time-out interval elapses, the return value is nonzero.
#endregion
[DllImport("kernel32.dll", SetLastError=true)]
public static extern bool WaitNamedPipe(
String name,
int timeout);
#region Comments
///
/// Retrieves the calling thread's last-error code value.
///
/// The return value is the calling thread's last-error code value.
#endregion
[DllImport("kernel32.dll", SetLastError=true)]
public static extern uint GetLastError();
#region Comments
///
/// Flushes the buffers of the specified file and causes all buffered data to be written to the file.
///
/// Handle to an open file.
/// If the function succeeds, the return value is nonzero.
#endregion
[DllImport("kernel32.dll", SetLastError=true)]
public static extern bool FlushFileBuffers(
IntPtr hHandle);
#region Comments
///
/// Disconnects the server end of a named pipe instance from a client process.
///
/// Handle to an instance of a named pipe.
/// If the function succeeds, the return value is nonzero.
#endregion
[DllImport("kernel32.dll", SetLastError=true)]
public static extern bool DisconnectNamedPipe(
IntPtr hHandle);
#region Comments
///
/// Sets the read mode and the blocking mode of the specified named pipe.
///
///
/// If the specified handle is to the client end of a named pipe and if
/// the named pipe server process is on a remote computer, the function
/// can also be used to control local buffering.
///
/// Handle to the named pipe instance.
/// Pointer to a variable that supplies the new mode.
/// Pointer to a variable that specifies the maximum
/// number of bytes collected on the client computer before
/// transmission to the server.
/// Pointer to a variable that specifies the
/// maximum time, in milliseconds, that can pass before a remote
/// named pipe transfers information over the network.
/// If the function succeeds, the return value is nonzero.
#endregion
[DllImport("kernel32.dll", SetLastError=true)]
public static extern bool SetNamedPipeHandleState(
IntPtr hHandle,
ref uint mode,
IntPtr cc,
IntPtr cd);
#region Comments
///
/// Closes an open object handle.
///
/// Handle to an open object.
/// If the function succeeds, the return value is nonzero.
#endregion
[DllImport("kernel32.dll", SetLastError=true)]
public static extern bool CloseHandle(
IntPtr hHandle);
#region Comments
///
/// Sets the security descriptor attributes
///
/// Reference to a SECURITY_DESCRIPTOR structure.
///
///
///
///
#endregion
[DllImport("Advapi32.dll", SetLastError=true)]
public static extern bool SetSecurityDescriptorDacl(ref SECURITY_DESCRIPTOR sd, bool bDaclPresent, IntPtr Dacl, bool bDaclDefaulted);
#region Comments
///
/// Initializes a SECURITY_DESCRIPTOR structure.
///
///
///
///
#endregion
[DllImport("Advapi32.dll", SetLastError=true)]
public static extern bool InitializeSecurityDescriptor(out SECURITY_DESCRIPTOR sd, int dwRevision);
#region Comments
///
/// Private constructor.
///
#endregion
private NamedPipeNative() {}
}
#region Comments
///
/// Security Descriptor structure
///
#endregion
[StructLayout(LayoutKind.Sequential)]
public struct SECURITY_DESCRIPTOR {
private byte Revision;
private byte Sbz1;
private ushort Control;
private IntPtr Owner;
private IntPtr Group;
private IntPtr Sacl;
private IntPtr Dacl;
}
#region Comments
///
/// Security Attributes structure.
///
#endregion
[StructLayout(LayoutKind.Sequential)]
public struct SECURITY_ATTRIBUTES {
public int nLength;
public IntPtr lpSecurityDescriptor;
public bool bInheritHandle;
}
#region Comments
///
/// This class is used as a dummy parameter only.
///
#endregion
[StructLayout(LayoutKind.Sequential)]
public class Overlapped {
}
#region Comments
///
/// This class is used as a dummy parameter only.
///
#endregion
[StructLayout(LayoutKind.Sequential)]
public class SecurityAttributes {
}
}