Exemple #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);
            }
        }
Exemple #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;
            }));
        }