当前位置: 首页>>代码示例>>C#>>正文


C# UnmanagedMemoryStream.Read方法代码示例

本文整理汇总了C#中System.IO.UnmanagedMemoryStream.Read方法的典型用法代码示例。如果您正苦于以下问题:C# UnmanagedMemoryStream.Read方法的具体用法?C# UnmanagedMemoryStream.Read怎么用?C# UnmanagedMemoryStream.Read使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.IO.UnmanagedMemoryStream的用法示例。


在下文中一共展示了UnmanagedMemoryStream.Read方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: UnpackBigEndian

        private void UnpackBigEndian(string path)
        {
            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);

            int count = *(bint*)(_source.Address + 0x08);

            for (int i = 0; i < count; i++)
                stringOffsets.Add(*(bint*)(_source.Address + (i * 0x04) + 0x10));
            for (int i = 0; i < count; i++)
                dataOffsets.Add(*(bint*)(_source.Address + (stringOffsets.Count * 4) + (i * 4) + 0x10));
            for (int i = 0; i < count; i++)
                sizes.Add(*(bint*)(_source.Address + (stringOffsets.Count * 4) + (dataOffsets.Count * 4) + (i * 4) + 0x10));

            foreach (int off in stringOffsets)
                strings.Add(new String((sbyte*)_source.Address + off));

            for (int i = 0; i < count; i++)
            {
                byte[] _fileData = new byte[sizes[i]];

                using (UnmanagedMemoryStream stream = new UnmanagedMemoryStream((byte*)(_source.Address + dataOffsets[i]), sizes[i]))
                    stream.Read(_fileData, 0, (int)stream.Length);

                try
                {
                    File.WriteAllBytes(path+"/" + strings[i], _fileData);
                }

                catch (Exception x) { Console.WriteLine(x.Message); }
            }
        }
开发者ID:chrisall76,项目名称:Sm4sh-Tools,代码行数:32,代码来源:Unpacker.cs

示例2: ToStream

		public void ToStream(byte* ptr, long count, Stream output)
		{
			using (var stream = new UnmanagedMemoryStream(ptr, count))
			{
				while (stream.Position < stream.Length)
				{
					var read = stream.Read(_buffer, 0, _buffer.Length);
					output.Write(_buffer, 0, read);
				}
			}
		}
开发者ID:WimVergouwe,项目名称:ravendb,代码行数:11,代码来源:DataCopier.cs

示例3: GetData

		public override object GetData (TransferDataType type)
		{
			if (type == TransferDataType.Uri)
				return (Uri)NSUrl.FromPasteboard (NSPasteboard.GeneralPasteboard);

			var data = NSPasteboard.GeneralPasteboard.GetDataForType (type.ToUTI ());
			if (data == null)
				return null;

			if (type == TransferDataType.Text)
				return data.ToString ();
			if (type == TransferDataType.Image)
				return new NSImage (data);

			unsafe {
				var bytes = new byte [data.Length];
				using (var stream = new UnmanagedMemoryStream ((byte*)data.Bytes, bytes.Length))
					stream.Read (bytes, 0, bytes.Length);
				return TransferDataSource.DeserializeValue (bytes);
			}
		}
开发者ID:StEvUgnIn,项目名称:xwt,代码行数:21,代码来源:MacClipboardBackend.cs

示例4: Unmanaged

        unsafe public IHttpActionResult Unmanaged()
        {
            //使用 UnmanagedMemoryStream 类读取和写入非托管内存。 使用 Marshal 类分配和解除分配非托管内存块。
            // Create some data to read and write.
            byte[] message = UnicodeEncoding.Unicode.GetBytes("Hello Angkor");

            // Allocate a block of unmanaged memory and return an IntPtr object.	
            IntPtr memIntPtr = Marshal.AllocHGlobal(message.Length);

            // Get a byte pointer from the IntPtr object.
            byte* memBytePtr = (byte*)memIntPtr.ToPointer();

            // Create an UnmanagedMemoryStream object using a pointer to unmanaged memory.
            UnmanagedMemoryStream writeStream = new UnmanagedMemoryStream(memBytePtr, message.Length, message.Length, FileAccess.Write);

            // Write the data.
            writeStream.Write(message, 0, message.Length);
            // Close the stream.
            writeStream.Close();
            // Create another UnmanagedMemoryStream object using a pointer to unmanaged memory.
            UnmanagedMemoryStream readStream = new UnmanagedMemoryStream(memBytePtr, message.Length, message.Length, FileAccess.Read);
            // Create a byte array to hold data from unmanaged memory.
            byte[] outMessage = new byte[message.Length];
            // Read from unmanaged memory to the byte array.
            readStream.Read(outMessage, 0, message.Length);
            // Close the stream.
            readStream.Close();
            // Display the data to the console.

            var msg = UnicodeEncoding.Unicode.GetString(outMessage);
            // Free the block of unmanaged memory.
            Marshal.FreeHGlobal(memIntPtr);
            return Json(msg);


        }
开发者ID:wjkhappy14,项目名称:aspnetwebstack,代码行数:36,代码来源:IOController.cs

示例5: Read_Count_Overlow

		public void Read_Count_Overlow ()
		{
			UnmanagedMemoryStream ums = new 
				UnmanagedMemoryStream(mem_byteptr, length, length, FileAccess.ReadWrite);
			ums.Write (testStreamData, 0, testStreamData.Length);
			ums.Position = 0;
			try {
				ums.Read (readData, 1, readData.Length);
				Assert.Fail ("#1");
			} catch (ArgumentException ex) {
				// Offset and length were out of bounds for the array or count
				// is greater than the number of elements from index to the end
				// of the source collection
				Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
				Assert.IsNull (ex.InnerException, "#3");
				Assert.IsNotNull (ex.Message, "#4");
				Assert.IsNull (ex.ParamName, "#5");
			}
		}
开发者ID:KonajuGames,项目名称:SharpLang,代码行数:19,代码来源:UnmanagedMemoryStreamTest.cs

示例6: PlaySoundResourceEvenIfNotPCM

 public static void PlaySoundResourceEvenIfNotPCM(UnmanagedMemoryStream unmanagedMemoryStream)
 {
     try
     {
         long n = unmanagedMemoryStream.Length;
         byte[] waveData = new byte[n];
         unmanagedMemoryStream.Read(waveData, 0, (int)n);
         // Note: I did not use SND_ASYNC here, because of the reasons described in this article: http://blogs.msdn.com/b/larryosterman/archive/2009/02/19/playsound-xxx-snd-memory-snd-async-is-almost-always-a-bad-idea.aspx
         PlaySound(waveData, 0, (int)(SND.SND_MEMORY | SND.SND_NOWAIT));
     }
     catch (Exception x)
     {
         Console.WriteLine("PlaySoundResourceEvenIfNotPCM: Error trying to access or play the resource: " + x.Message);
     }
 }
开发者ID:JamesWHurst,项目名称:MonsoonWpfSampleApplic,代码行数:15,代码来源:AudioLib.cs

示例7: Read_WriteOnly

		public void Read_WriteOnly ()
		{
			UnmanagedMemoryStream ums = new
				UnmanagedMemoryStream(mem_byteptr, length, length, FileAccess.Write);
			try {
				ums.Read(readData, 0, 1);
				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();
		}
开发者ID:KonajuGames,项目名称:SharpLang,代码行数:15,代码来源:UnmanagedMemoryStreamTest.cs

示例8: Write

		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();
		}
开发者ID:KonajuGames,项目名称:SharpLang,代码行数:23,代码来源:UnmanagedMemoryStreamTest.cs

示例9: Read_Offset_Negative

		public void Read_Offset_Negative ()
		{
			UnmanagedMemoryStream ums = new 
				UnmanagedMemoryStream(mem_byteptr, length, length, FileAccess.ReadWrite);
			ums.Write (testStreamData, 0, testStreamData.Length);
			ums.Position = 0;
			try {
				ums.Read (readData, -1, 0);
				Assert.Fail ("#1");
			} catch (ArgumentOutOfRangeException ex) {
				// Non-negative number required
				Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
				Assert.IsNull (ex.InnerException, "#3");
				Assert.IsNotNull (ex.Message, "#4");
				Assert.IsNotNull (ex.ParamName, "#5");
				Assert.AreEqual ("offset", ex.ParamName, "#6");
			}
		}
开发者ID:KonajuGames,项目名称:SharpLang,代码行数:18,代码来源:UnmanagedMemoryStreamTest.cs

示例10: Read_Count_Overflow

		public void Read_Count_Overflow ()
		{
			using (UnmanagedMemoryStream ums = new UnmanagedMemoryStream (mem_byteptr, length, length, FileAccess.ReadWrite)) {
				ums.Write (testStreamData, 0, testStreamData.Length);
				ums.Position = 0;
				try {
					ums.Read (readData, 0, Int32.MaxValue);
					Assert.Fail ("#1");
				}
				catch (ArgumentException ex) {
					Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
					Assert.IsNull (ex.InnerException, "#3");
					Assert.IsNotNull (ex.Message, "#4");
					Assert.IsNull (ex.ParamName, "#5");
				}
			}
		}
开发者ID:KonajuGames,项目名称:SharpLang,代码行数:17,代码来源:UnmanagedMemoryStreamTest.cs

示例11: ThreadFunc

        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();
            }
//.........这里部分代码省略.........
开发者ID:Zergatul,项目名称:ulHelper,代码行数:101,代码来源:AccountManagerModule.cs

示例12: ATAwriteDMA8Mem

        public void ATAwriteDMA8Mem(UnmanagedMemoryStream pMem, int size)
        {
            if (((xferMode & 0xF0) == 0x40) &&
                (dev9.Dev9Ru16((int)DEV9Header.SPD_R_IF_CTRL) & DEV9Header.SPD_IF_DMA_ENABLE) != 0)
            {
                //size >>= 1;
                Log_Verb("DEV9 : DMA write, size " + size + ", transferred " + wrTransferred + ", total size " + nsector * 512);
                Log_Info("wATA");

                //write
                byte[] temp = new byte[size];
                pMem.Read(temp, 0, size);
                hddImage.Write(temp, 0, size);

                wrTransferred += size;
                if (wrTransferred >= nsector * 512)
                {
                    hddImage.Flush();

                    //Set Sector
                    long currSect = HDD_GetLBA();
                    currSect += nsector;
                    HDD_SetLBA(currSect);

                    nsector = 0;
                    status = DEV9Header.ATA_STAT_READY | DEV9Header.ATA_STAT_SEEK;
                    if (sendIRQ) dev9.DEV9irq(3, 1); //0x6C
                    wrTransferred = 0;
                    //dev9.Dev9Wu16((int)DEV9Header.SPD_R_IF_CTRL, (UInt16)(dev9.Dev9Ru16((int)DEV9Header.SPD_R_IF_CTRL) & ~DEV9Header.SPD_IF_DMA_ENABLE));
                }
            }
        }
开发者ID:TheLastRar,项目名称:CLR-DEV9,代码行数:32,代码来源:ATA_State.cs

示例13: Read_Stream_Closed

		public void Read_Stream_Closed ()
		{
			UnmanagedMemoryStream ums = new 
				UnmanagedMemoryStream(mem_byteptr, length);
			ums.Close();
			ums.Read(readData, 0, 0);
		}
开发者ID:KonajuGames,项目名称:SharpLang,代码行数:7,代码来源:UnmanagedMemoryStreamTest.cs

示例14: Read_Buffer_Null

		public void Read_Buffer_Null ()
		{
			UnmanagedMemoryStream ums = new 
				UnmanagedMemoryStream(mem_byteptr, length);
			try {
				ums.Read((byte []) null, 0, 0);
				Assert.Fail ("#1");
			} catch (ArgumentNullException ex) {
				// Value cannot be null
				Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
				Assert.IsNull (ex.InnerException, "#3");
				Assert.IsNotNull (ex.Message, "#4");
				Assert.IsNotNull (ex.ParamName, "#5");
				Assert.AreEqual ("buffer", ex.ParamName, "#6");
			}
		}
开发者ID:KonajuGames,项目名称:SharpLang,代码行数:16,代码来源:UnmanagedMemoryStreamTest.cs

示例15: Read

		public void Read ()
		{
			UnmanagedMemoryStream ums = new 
				UnmanagedMemoryStream(mem_byteptr, length, capacity, FileAccess.ReadWrite);
			ums.Write (testStreamData, 0, testStreamData.Length);
			ums.Position = 0;

			Assert.AreEqual (length / 2, ums.Read (readData, 0, (length / 2)), "#1");
			VerifyTestData ("#2", readData, 0, (length / 2));
			Assert.AreEqual (length / 2, ums.Position, "#3");
			
			//Seek back to begining
			ums.Seek (0, SeekOrigin.Begin);
			
			//Read complete stream
			Assert.AreEqual (length, ums.Read (readData, 0, length), "#4");
			VerifyTestData ("#5", readData, 0, length);
			Assert.AreEqual (length, ums.Position, "#6");
			
			//Seek to mid of the stream and read till end
			ums.Seek ((length / 2), SeekOrigin.Begin);
			ums.Read (readData, 0, (length / 2));
			VerifyTestData ("#7", readData, (length / 2), (length / 2));
			Assert.AreEqual (length, ums.Position, "#8");
			ums.Close ();
		}
开发者ID:KonajuGames,项目名称:SharpLang,代码行数:26,代码来源:UnmanagedMemoryStreamTest.cs


注:本文中的System.IO.UnmanagedMemoryStream.Read方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。