Exemple #1
0
        public static SafeFileHandle OpenDriver()
        {
            SafeFileHandle hDriver = DeviceApi.CreateFile(WIN32_ROOT_PREFIX,
                                                          0,
                                                          DeviceApi.FILE_SHARE_READ | DeviceApi.FILE_SHARE_WRITE,
                                                          IntPtr.Zero,
                                                          DeviceApi.OPEN_EXISTING,
                                                          0,
                                                          IntPtr.Zero);

            if (hDriver.IsInvalid)
            {
                LoadDriver();
                hDriver = DeviceApi.CreateFile(WIN32_ROOT_PREFIX,
                                               0,
                                               DeviceApi.FILE_SHARE_READ | DeviceApi.FILE_SHARE_WRITE,
                                               IntPtr.Zero,
                                               DeviceApi.OPEN_EXISTING,
                                               0,
                                               IntPtr.Zero);
                if (hDriver.IsInvalid)
                {
                    Win32Exception ex = new Win32Exception();
                    throw new TrueCryptException(TrueCryptException.ExceptionCause.DriverOpenFailed, ex.ErrorCode, ex.Message);
                }
            }

            return(hDriver);
        }
Exemple #2
0
        public static void Read(uint volume, Int64 address, uint length, byte[] data)
        {
            // try to open the current physical drive
            SafeFileHandle hndl = DeviceApi.CreateFile(string.Format("\\\\.\\PhysicalDrive{0}", volume),
                                                       DeviceApi.GENERIC_READ,
                                                       DeviceApi.FILE_SHARE_READ | DeviceApi.FILE_SHARE_WRITE,
                                                       IntPtr.Zero,
                                                       DeviceApi.OPEN_EXISTING,
                                                       DeviceApi.FILE_ATTRIBUTE_READONLY,
                                                       IntPtr.Zero);

            if (!hndl.IsInvalid)
            {
                // set the file pointer to the requested address
                if (DeviceApi.SetFilePointerEx(hndl, address, IntPtr.Zero, DeviceApi.EMoveMethod.Begin))
                {
                    // read the requested data from the physical drive
                    uint dummy;
                    if (!DeviceApi.ReadFile(hndl, data, length, out dummy, IntPtr.Zero))
                    {
                        throw new System.IO.IOException("\"ReadFile\" API call failed");
                    }
                }
                else
                {
                    throw new System.IO.IOException("\"SetFilePointerEx\" API call failed");
                }
                hndl.Close();
            }
        }