/// <summary> /// Sends the command. /// </summary> /// <param name="command">The command.</param> /// <param name="id1">The id1.</param> /// <param name="id2">The id2.</param> /// <param name="text">The text.</param> /// <returns></returns> private static int SendCommand(IbnCommand command, int id1, int?id2, string text) { int result; IntPtr hPipe = OpenPipe(); try { byte[] data = CreateBuffer(command, id1, id2, text); byte[] outBuffer = { 0, 0, 0, 0 }; UInt32[] bytesRead = { 0 }; if (!NativeMethods.TransactNamedPipe(hPipe, data, Convert.ToUInt32(data.Length), outBuffer, Convert.ToUInt32(outBuffer.Length), bytesRead, IntPtr.Zero)) { int error = Marshal.GetLastWin32Error(); throw new IMHelperException(string.Format(CultureInfo.InvariantCulture, "Pipe '{2}' command error. Command: {0}. Last Win32 error: {1}.", command, error, GetIbnServerPipeName())); } else { result = Convert.ToInt32((UInt32)outBuffer[0] | ((UInt32)outBuffer[1] << 8) | ((UInt32)outBuffer[2] << 16) | ((UInt32)outBuffer[3] << 24)); } } finally { NativeMethods.CloseHandle(hPipe); } return(result); }
/// <summary> /// Creates the buffer. /// </summary> /// <param name="command">The command.</param> /// <param name="number1">The number1.</param> /// <param name="number2">The number2.</param> /// <param name="text">The text.</param> /// <returns></returns> private static byte[] CreateBuffer(IbnCommand command, int number1, int?number2, string text) { int dataLength = 4; if (number2 != null) { dataLength += 4; } if (text != null) { dataLength += (text.Length + 1) * 2; } using (MemoryStream stream = new MemoryStream()) { using (BinaryWriter writer = new BinaryWriter(stream, System.Text.Encoding.Unicode)) { writer.Write((Int32)1); writer.Write((Int32)command); writer.Write((Int32)dataLength); writer.Write((Int32)number1); if (number2 != null) { writer.Write((Int32)number2.Value); } if (text != null) { writer.Write(Encoding.Unicode.GetBytes(text)); } writer.Write((byte)0); writer.Write((byte)0); writer.Flush(); } return(stream.ToArray()); } }
/// <summary> /// Sends the command. /// </summary> /// <param name="command">The command.</param> /// <param name="id1">The id1.</param> /// <param name="id2">The id2.</param> /// <param name="text">The text.</param> /// <returns></returns> private static int SendCommand(IbnCommand command, int id1, int? id2, string text) { int result; IntPtr hPipe = OpenPipe(); try { byte[] data = CreateBuffer(command, id1, id2, text); byte[] outBuffer = { 0, 0, 0, 0 }; UInt32[] bytesRead = { 0 }; if (!NativeMethods.TransactNamedPipe(hPipe, data, Convert.ToUInt32(data.Length), outBuffer, Convert.ToUInt32(outBuffer.Length), bytesRead, IntPtr.Zero)) { int error = Marshal.GetLastWin32Error(); throw new IMHelperException(string.Format(CultureInfo.InvariantCulture, "Pipe '{2}' command error. Command: {0}. Last Win32 error: {1}.", command, error, GetIbnServerPipeName())); } else { result = Convert.ToInt32((UInt32)outBuffer[0] | ((UInt32)outBuffer[1] << 8) | ((UInt32)outBuffer[2] << 16) | ((UInt32)outBuffer[3] << 24)); } } finally { NativeMethods.CloseHandle(hPipe); } return result; }
/// <summary> /// Creates the buffer. /// </summary> /// <param name="command">The command.</param> /// <param name="number1">The number1.</param> /// <param name="number2">The number2.</param> /// <param name="text">The text.</param> /// <returns></returns> private static byte[] CreateBuffer(IbnCommand command, int number1, int? number2, string text) { int dataLength = 4; if (number2 != null) dataLength += 4; if (text != null) dataLength += (text.Length + 1) * 2; using (MemoryStream stream = new MemoryStream()) { using (BinaryWriter writer = new BinaryWriter(stream, System.Text.Encoding.Unicode)) { writer.Write((Int32)1); writer.Write((Int32)command); writer.Write((Int32)dataLength); writer.Write((Int32)number1); if (number2 != null) writer.Write((Int32)number2.Value); if (text != null) writer.Write(Encoding.Unicode.GetBytes(text)); writer.Write((byte)0); writer.Write((byte)0); writer.Flush(); } return stream.ToArray(); } }