示例#1
0
        internal static unsafe byte *ReadSectorDataPointerFromHandle(IntPtr handle, ulong startSector, ulong numSectors, ulong sectorSize)
        {
            uint bytesRead;

            fixed(byte *data = new byte[sectorSize *numSectors])
            {
                LARGE_INTEGER li;

                li.LowPart  = 0;
                li.QuadPart = (long)(startSector * sectorSize);

                NativeDisk.SetFilePointer(handle, li.LowPart, out li.HighPart, EMoveMethod.Begin);
                if (!NativeDisk.ReadFile(handle, new IntPtr(data), (uint)(sectorSize * numSectors), out bytesRead, IntPtr.Zero))
                {
                    var exception = new Win32Exception(Marshal.GetLastWin32Error());
                    throw new Exception(string.Format("Error occured when trying to read sector data pointer from handle.\nError code: {0}\nMessage: {1}", exception.NativeErrorCode, exception.Message));
                }

                if (bytesRead < (sectorSize * numSectors))
                {
                    for (uint i = bytesRead; i < (sectorSize * numSectors); i++)
                    {
                        data[i] = 0;
                    }
                }

                return(data);
            }
        }
示例#2
0
        internal static Task <int> ReadSectorDataFromHandleAsync(IntPtr handle, byte[] data, ulong startSector, ulong numSectors, ulong sectorSize)
        {
            return(Task.Run(() =>
            {
                uint bytesRead;
                LARGE_INTEGER li;
                li.LowPart = 0;
                li.QuadPart = (long)(startSector * sectorSize);

                NativeDisk.SetFilePointer(handle, li.LowPart, out li.HighPart, EMoveMethod.Begin);
                if (!NativeDisk.ReadFile(handle, data, (uint)(sectorSize * numSectors), out bytesRead, IntPtr.Zero))
                {
                    var exception = new Win32Exception(Marshal.GetLastWin32Error());
                    throw new Exception(string.Format("Error occured when trying to read sector data from handle async.\nError code: {0}\nMessage: {1}", exception.NativeErrorCode, exception.Message));
                }

                if (bytesRead < (sectorSize * numSectors))
                {
                    for (uint i = bytesRead; i < (sectorSize * numSectors); i++)
                    {
                        data[i] = 0;
                    }
                }

                return (int)bytesRead;
            }));
        }
示例#3
0
        internal static void WriteSectorDataToHandle(IntPtr handle, byte[] data, ulong startSector, ulong numSectors, ulong sectorSize, int length)
        {
            uint          bytesWritten;
            bool          result;
            LARGE_INTEGER li;

            li.LowPart  = 0;
            li.QuadPart = (long)(startSector * sectorSize);

            NativeDisk.SetFilePointer(handle, li.LowPart, out li.HighPart, EMoveMethod.Begin);
            result = NativeDisk.WriteFile(handle, data, (uint)(length), out bytesWritten, IntPtr.Zero);

            if (!result)
            {
                var exception = new Win32Exception(Marshal.GetLastWin32Error());
                throw new Exception(string.Format("Error occured when trying to write sector data to handle.\nError code: {0}\nMessage: {1}", exception.NativeErrorCode, exception.Message));
            }
        }