private static unsafe void ThreadFunc() { try { int size = 65536; bool createdMain = false; var hFile = WinAPI.OpenFileMapping( WinAPI.FileMapAccess.FileMapAllAccess, false, "ulHelper_fm"); if (hFile == IntPtr.Zero) { hFile = WinAPI.CreateFileMapping( new IntPtr(-1), IntPtr.Zero, WinAPI.FileMapProtection.PageReadWrite, (uint)0, (uint)size, "ulHelper_fm"); createdMain = true; } var lpBaseAddress = WinAPI.MapViewOfFile( hFile, WinAPI.FileMapAccess.FileMapAllAccess, 0, 0, 0); Mutex = new Mutex(false, "ulHelper_mutex"); Stream = new UnmanagedMemoryStream((byte*)lpBaseAddress.ToPointer(), size, size, FileAccess.ReadWrite); eventWH = new EventWaitHandle(false, EventResetMode.AutoReset, "ulHelper_event"); int accCount; byte[] buf = new byte[size]; if (createdMain) { Mutex.WaitOne(); try { buf[0] = buf[1] = buf[2] = buf[3] = 0; Stream.Position = 0; Stream.Write(buf, 0, 4); } finally { Mutex.ReleaseMutex(); } } eventWH.Set(); while (true) { eventWH.WaitOne(); if (MainForm.NeedTerminate) break; Mutex.WaitOne(); try { Stream.Position = 0; accCount = Stream.ReadByte(); lock (Accounts.List) if (Accounts.List.Count != accCount) { foreach (var acc in Accounts.List) acc.Active = false; for (int i = 0; i < accCount; i++) { string accountName = ""; int index = 0; Stream.Position = 8 + i * 128; Stream.Read(buf, 0, 128); while (buf[index] != 0) accountName += (char)buf[index++]; if (!Accounts.List.Any(acc => acc.Name == accountName)) { var accData = new AccountData(accountName); Accounts.List.Add(accData); } Accounts.List.First(acc => acc.Name == accountName).Active = true; } PerformNewAccount(); } } finally { Mutex.ReleaseMutex(); } } WinAPI.UnmapViewOfFile(lpBaseAddress); WinAPI.CloseHandle(hFile); } catch (ThreadAbortException) { throw; } catch (Exception ex) { var f = new ExceptionForm(ex); f.ShowDialog(); } }
public override int ReadByte() { return(_unmanagedStream.ReadByte()); }
public void WriteByte () { UnmanagedMemoryStream ums = new UnmanagedMemoryStream(mem_byteptr, 3, 5, FileAccess.ReadWrite); ums.WriteByte (testStreamData [0]); Assert.AreEqual (5, ums.Capacity, "#A1"); Assert.AreEqual (1, ums.Position, "#A2"); Assert.AreEqual (3, ums.Length, "#A3"); ums.WriteByte (testStreamData [1]); Assert.AreEqual (5, ums.Capacity, "#B1"); Assert.AreEqual (2, ums.Position, "#B2"); Assert.AreEqual (3, ums.Length, "#B3"); ums.WriteByte (testStreamData [2]); Assert.AreEqual (5, ums.Capacity, "#C1"); Assert.AreEqual (3, ums.Position, "#C2"); Assert.AreEqual (3, ums.Length, "#C3"); ums.WriteByte (testStreamData [3]); Assert.AreEqual (5, ums.Capacity, "#D1"); Assert.AreEqual (4, ums.Position, "#D2"); Assert.AreEqual (4, ums.Length, "#D3"); ums.WriteByte (testStreamData [4]); Assert.AreEqual (5, ums.Capacity, "#E1"); Assert.AreEqual (5, ums.Position, "#E2"); Assert.AreEqual (5, ums.Length, "#E3"); ums.Seek (0, SeekOrigin.Begin); Assert.AreEqual (testStreamData [0], ums.ReadByte (), "#F1"); Assert.AreEqual (testStreamData [1], ums.ReadByte (), "#F2"); Assert.AreEqual (testStreamData [2], ums.ReadByte (), "#F3"); Assert.AreEqual (testStreamData [3], ums.ReadByte (), "#F4"); Assert.AreEqual (testStreamData [4], ums.ReadByte (), "#F5"); ums.Close (); }
private Bitmap fetchImage(UnmanagedMemoryStream memStream) { int buffer; string id =""; int width; int height; int max; byte[] rgbValues = null; Bitmap bitmap; do { buffer = memStream.ReadByte(); id += (char)buffer; } while (buffer != '\n' && buffer != ' '); string dimension = ""; do { buffer = memStream.ReadByte(); dimension += (char)buffer; } while (buffer != '\n' && buffer != ' '); width = Convert.ToInt16(dimension); dimension = ""; do { buffer = memStream.ReadByte(); dimension += (char)buffer; } while (buffer != '\n' && buffer != ' '); height = Convert.ToInt16(dimension); string maxRGB = ""; do { buffer = memStream.ReadByte(); maxRGB += (char)buffer; } while (buffer != '\n' && buffer != ' '); max = Convert.ToInt16(maxRGB); rgbValues = new byte[height * width * 3]; for (int i = 0; i < rgbValues.Length; i++) { rgbValues[i] = (byte)memStream.ReadByte(); } memStream.Close(); try { bitmap = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format24bppRgb); Rectangle rect = new Rectangle(0, 0, width, height); System.Drawing.Imaging.BitmapData bmpData = bitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly, bitmap.PixelFormat); IntPtr ptr = bmpData.Scan0; byte[] bgrValues = new byte[rgbValues.Length]; for (int i = 0; i < rgbValues.Length; i += 3) { bgrValues[i] = rgbValues[i + 2]; bgrValues[i + 1] = rgbValues[i + 1]; bgrValues[i + 2] = rgbValues[i]; } System.Runtime.InteropServices.Marshal.Copy(bgrValues, 0, ptr, bgrValues.Length); bitmap.UnlockBits(bmpData); return bitmap; } catch (Exception ex) { Console.WriteLine("Error creating new bitmap: " + ex.ToString()); } return null; }
public void Seek_Current_Overflow () { using (UnmanagedMemoryStream ums = new UnmanagedMemoryStream (mem_byteptr, 5, 10, FileAccess.Read)) { ums.ReadByte (); try { ums.Seek (Int64.MaxValue, SeekOrigin.Current); Assert.Fail ("#1"); } catch (IOException ex) { Assert.AreEqual (typeof (IOException), ex.GetType (), "#2"); Assert.IsNull (ex.InnerException, "#3"); Assert.IsNotNull (ex.Message, "#4"); } } }
public void Write () { UnmanagedMemoryStream ums = new UnmanagedMemoryStream(mem_byteptr, length, capacity, FileAccess.ReadWrite); ums.Write (testStreamData, 0, length); Assert.AreEqual (capacity, ums.Capacity, "#A1"); Assert.AreEqual (length, ums.Position, "#A2"); Assert.AreEqual (length, ums.Length, "#A3"); ums.Position = 0; ums.Read (readData, 0, length); Assert.AreEqual (capacity, ums.Capacity, "#B1"); Assert.AreEqual (length, ums.Position, "#B2"); Assert.AreEqual (length, ums.Length, "#B3"); VerifyTestData ("#B4", readData, 0, length); ums.Write (testStreamData, 2, 2); Assert.AreEqual (capacity, ums.Capacity, "#C1"); Assert.AreEqual (length + 2, ums.Position, "#C1"); Assert.AreEqual (length + 2, ums.Length, "#C2"); ums.Position = length; Assert.AreEqual (testStreamData [2], ums.ReadByte (), "#D1"); Assert.AreEqual (testStreamData [3], ums.ReadByte (), "#D2"); ums.Close(); }
public void ReadByte_WriteOnly () { UnmanagedMemoryStream ums = new UnmanagedMemoryStream(mem_byteptr, length, capacity, FileAccess.Write); try { ums.ReadByte (); Assert.Fail ("#1"); } catch (NotSupportedException ex) { // Stream does not support reading Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2"); Assert.IsNull (ex.InnerException, "#3"); Assert.IsNotNull (ex.Message, "#4"); } ums.Close(); }
public void ReadByte_EndOfStream () { UnmanagedMemoryStream ums = new UnmanagedMemoryStream(mem_byteptr, length, capacity, FileAccess.ReadWrite); ums.Write (testStreamData, 0, testStreamData.Length); ums.Position = 0; ums.Seek(length, SeekOrigin.Begin); Assert.AreEqual (-1, ums.ReadByte (), "#3"); ums.Seek(length + 1, SeekOrigin.Begin); Assert.AreEqual (-1, ums.ReadByte (), "#4"); ums.Seek(capacity, SeekOrigin.Begin); Assert.AreEqual (-1, ums.ReadByte (), "#5"); ums.Seek(capacity + 1, SeekOrigin.Begin); Assert.AreEqual (-1, ums.ReadByte (), "#6"); ums.Close(); }
public void ReadByte_Stream_Closed () { UnmanagedMemoryStream ums = new UnmanagedMemoryStream(mem_byteptr, length); ums.Close(); ums.ReadByte(); }
public void ReadByte () { UnmanagedMemoryStream ums = new UnmanagedMemoryStream(mem_byteptr, length, length, FileAccess.ReadWrite); ums.Write (testStreamData, 0, testStreamData.Length); ums.Position = 0; Assert.AreEqual (testStreamData [0], ums.ReadByte (), "#1"); Assert.AreEqual (testStreamData [1], ums.ReadByte (), "#2"); ums.Close(); }
public void PositionPointer_Set () { byte [] n = new byte [8]; n [4] = 65; fixed (byte* p = n) { UnmanagedMemoryStream m = new UnmanagedMemoryStream (p, 8, 8, FileAccess.ReadWrite); m.PositionPointer = p + 4; Assert.AreEqual (65, m.ReadByte (), "read"); m.WriteByte (42); } Assert.AreEqual (42, n [5], "write"); }