/// <summary> /// Get a named attribute from the pipe. /// </summary> /// <param name="attribute_type">The attribute type to query.</param> /// <param name="name">The name of the attribute.</param> /// <param name="throw_on_error">True to throw on error.</param> /// <returns>The attribute value as a byte array.</returns> /// <exception cref="NtException">Thrown on error.</exception> public NtResult <byte[]> GetAttribute(PipeAttributeType attribute_type, string name, bool throw_on_error) { NtIoControlCode ioctl = GetAttributeToIoCtl(attribute_type); int size = 128; byte[] name_buffer = Encoding.ASCII.GetBytes(name + "\0"); while (size < 4096) { var result = FsControl(ioctl, name_buffer, size, false); if (result.IsSuccess) { return(result); } if (result.Status != NtStatus.STATUS_BUFFER_TOO_SMALL) { result.Status.ToNtException(throw_on_error); return(result); } size *= 2; } return(NtStatus.STATUS_BUFFER_TOO_SMALL.CreateResultFromError <byte[]>(throw_on_error)); }
/// <summary> /// Get a named attribute from the pipe as an integer. /// </summary> /// <param name="attribute_type">The attribute type to query.</param> /// <param name="name">The name of the attribute.</param> /// <param name="throw_on_error">True to throw on error.</param> /// <returns>The attribute value as an integer.</returns> /// <exception cref="NtException">Thrown on error.</exception> public NtResult <string> GetAttributeString(PipeAttributeType attribute_type, string name, bool throw_on_error) { var result = GetAttribute(attribute_type, name, throw_on_error); if (result.IsSuccess && ((result.Result.Length & 1) == 0)) { return(Encoding.Unicode.GetString(result.Result).TrimEnd('\0').CreateResult()); } return(NtStatus.STATUS_BUFFER_TOO_SMALL.CreateResultFromError <string>(throw_on_error)); }
/// <summary> /// Get a named attribute from the pipe as an integer. /// </summary> /// <param name="attribute_type">The attribute type to query.</param> /// <param name="name">The name of the attribute.</param> /// <param name="throw_on_error">True to throw on error.</param> /// <returns>The attribute value as an integer.</returns> /// <exception cref="NtException">Thrown on error.</exception> public NtResult <int> GetAttributeInt(PipeAttributeType attribute_type, string name, bool throw_on_error) { var result = GetAttribute(attribute_type, name, throw_on_error); if (result.IsSuccess && result.Result.Length == 4) { return(BitConverter.ToInt32(result.Result, 0).CreateResult()); } return(NtStatus.STATUS_BUFFER_TOO_SMALL.CreateResultFromError <int>(throw_on_error)); }
/// <summary> /// Set a named attribute for a pipe. /// </summary> /// <param name="attribute_type">The attribute type to set.</param> /// <param name="name">The name of the attribute.</param> /// <param name="value">The value to set.</param> /// <param name="throw_on_error">True to throw on error.</param> /// <returns>The status code for the attribute.</returns> /// <exception cref="NtException">Thrown on error.</exception> public NtStatus SetAttribute(PipeAttributeType attribute_type, string name, byte[] value, bool throw_on_error) { NtIoControlCode ioctl = SetAttributeToIoCtl(attribute_type); byte[] name_bytes = Encoding.ASCII.GetBytes(name); MemoryStream stm = new MemoryStream(); stm.Write(name_bytes, 0, name_bytes.Length); stm.WriteByte(0); stm.Write(value, 0, value.Length); return(FsControl(ioctl, stm.ToArray(), 0, throw_on_error).Status); }
private static NtIoControlCode SetAttributeToIoCtl(PipeAttributeType attribute_type) { switch (attribute_type) { case PipeAttributeType.Pipe: return(NtWellKnownIoControlCodes.FSCTL_PIPE_SET_PIPE_ATTRIBUTE); case PipeAttributeType.Connection: return(NtWellKnownIoControlCodes.FSCTL_PIPE_SET_CONNECTION_ATTRIBUTE); case PipeAttributeType.Handle: return(NtWellKnownIoControlCodes.FSCTL_PIPE_SET_HANDLE_ATTRIBUTE); default: throw new ArgumentException("Invalid attribute type"); } }
/// <summary> /// Get a named attribute from the pipe as an integer. /// </summary> /// <param name="attribute_type">The attribute type to query.</param> /// <param name="name">The name of the attribute.</param> /// <returns>The attribute value as an integer.</returns> /// <exception cref="NtException">Thrown on error.</exception> public string GetAttributeString(PipeAttributeType attribute_type, string name) { return(GetAttributeString(attribute_type, name, true).Result); }
/// <summary> /// Get a named attribute from the pipe as an integer. /// </summary> /// <param name="attribute_type">The attribute type to query.</param> /// <param name="name">The name of the attribute.</param> /// <returns>The attribute value as an integer.</returns> /// <exception cref="NtException">Thrown on error.</exception> public int GetAttributeInt(PipeAttributeType attribute_type, string name) { return(GetAttributeInt(attribute_type, name, true).Result); }
/// <summary> /// Get a named attribute from the pipe. /// </summary> /// <param name="attribute_type">The attribute type to query.</param> /// <param name="name">The name of the attribute.</param> /// <returns>The attribute value as a byte array.</returns> /// <exception cref="NtException">Thrown on error.</exception> public byte[] GetAttribute(PipeAttributeType attribute_type, string name) { return(GetAttribute(attribute_type, name, true).Result); }
/// <summary> /// Set a named attribute for a pipe. /// </summary> /// <param name="attribute_type">The attribute type to set.</param> /// <param name="name">The name of the attribute.</param> /// <param name="value">The value to set.</param> /// <exception cref="NtException">Thrown on error.</exception> public void SetAttribute(PipeAttributeType attribute_type, string name, string value) { SetAttribute(attribute_type, name, value, true); }
/// <summary> /// Set a named attribute for a pipe. /// </summary> /// <param name="attribute_type">The attribute type to set.</param> /// <param name="name">The name of the attribute.</param> /// <param name="value">The value to set.</param> /// <param name="throw_on_error">True to throw on error.</param> /// <returns>The status code for the attribute.</returns> /// <exception cref="NtException">Thrown on error.</exception> public NtStatus SetAttribute(PipeAttributeType attribute_type, string name, string value, bool throw_on_error) { return(SetAttribute(attribute_type, name, Encoding.Unicode.GetBytes(value + "\0"), throw_on_error)); }
/// <summary> /// Set a named attribute for a pipe. /// </summary> /// <param name="attribute_type">The attribute type to set.</param> /// <param name="name">The name of the attribute.</param> /// <param name="value">The value to set.</param> /// <param name="throw_on_error">True to throw on error.</param> /// <returns>The status code for the attribute.</returns> /// <exception cref="NtException">Thrown on error.</exception> public NtStatus SetAttribute(PipeAttributeType attribute_type, string name, int value, bool throw_on_error) { return(SetAttribute(attribute_type, name, BitConverter.GetBytes(value), throw_on_error)); }