示例#1
0
    public void TestFromInt64And32ToPtrAndBack()
    {
      var ipc = new IpcData();

      var random = new Random();
      var m = (int)(random.NextDouble() * int.MaxValue);
      var n = (long)(random.NextDouble() * long.MaxValue);

      var uuid = ipc.Guid;
      ipc.Add(m);
      ipc.Add(n);

      var payload = ipc.GetPtr();
      var dataBlockSize = ipc.GetSize();
      var byteArray = new byte[dataBlockSize];

      System.Runtime.InteropServices.Marshal.Copy(payload, byteArray, 0, dataBlockSize);
      var ipc2 = new IpcData(byteArray);

      Assert.AreEqual(uuid, ipc2.Guid);
      Assert.AreEqual(m, ipc2.Get<int>(0));
      Assert.AreEqual(n, ipc2.Get<long>(1));
    }
示例#2
0
    /// <summary>
    /// Send a message as a reconstructed string.
    /// </summary>
    /// <param name="msg">The IpcData we want to send.</param>
    /// <returns>The IpcData response data</returns>
    public IpcData Send( IpcData msg )
    {
      // look for the listener.
      var hWindow = ConnectedWindow;
      if (IntPtr.Zero == hWindow)
      {
        return null;
      }

      // the message we want to send.
      var cds = new Copydatastruct
      {
        dwData = (IntPtr)MessageType.Copy, //  copy message...
        cbData = msg.GetSize(),
        lpData = msg.GetPtr()
      };

      // open the shared memory that should have been created.
      using (var mmf = MemoryMappedFile.CreateNew( msg.Guid, 10000))
      {
        // cast to IntPtr because we know it is not null.
        if (0 == (int) SendMessage( hWindow, WmCopyData, IntPtr.Zero, ref cds))
        {
          return null;
        }

        // open the shared memory that should have been created.
        using (var mmfvs = mmf.CreateViewStream())
        {
          using (var reader = new BinaryReader(mmfvs))
          {
            // read the first 4 bytes for the size.
            var dataSize = BitConverter.ToInt32( reader.ReadBytes( sizeof(int)), 0 );

            // all the byes 
            var bytes = reader.ReadBytes( dataSize );

            // parse the response data into an IpcData
            return new IpcData( bytes );
          }
        }
      }
    }
示例#3
0
    public void TestFromInt32ToPtrAndBack()
    {
      var ipc = new IpcData();

      var uuid = ipc.Guid;
      var random = new Random();
      var n = (Int32)random.Next(0, Int32.MaxValue);

      ipc.Add(Int32.MaxValue);
      ipc.Add(n);

      var payload = ipc.GetPtr();
      var dataBlockSize = ipc.GetSize();
      var byteArray = new byte[dataBlockSize];

      System.Runtime.InteropServices.Marshal.Copy(payload, byteArray, 0, dataBlockSize);
      var ipc2 = new IpcData(byteArray);

      Assert.AreEqual(uuid, ipc2.Guid);
      Assert.AreEqual(Int32.MaxValue, ipc2.Get<Int32>(0));
      Assert.AreEqual(n, ipc2.Get<Int32>(1));
    }