C#BackupRead,BackupWrite - System.AccessViolationException

时间:2017-11-20 14:16:16

标签: c# winapi pinvoke

我正在尝试BackupRead()BackupWrite()使用http://pinvoke.net作为参考。我在google中找到的大多数答案都是引用C ++甚至Delphi ...我想用他们的ACL备份文件/文件夹。

编辑:

这是我当前的代码(我正在为SE_BACKUP_NAMESE_RESTORE_NAME以及SE_TAKE_OWNERSHIP_NAME设置权限:

            var bReadHandle = ArchiveWinApi.CreateFile(sourcePath, (uint)(ArchiveWinApi.ACCESS_MASK.GENERIC_READ | ArchiveWinApi.ACCESS_MASK.READ_CONTROL | ArchiveWinApi.ACCESS_MASK.ACCESS_SYSTEM_SECURITY), 0, IntPtr.Zero, FileMode.Open, ArchiveWinApi.ExtendedFileAttributes.BackupSemantics, IntPtr.Zero);
            if (!IsValid(bReadHandle))
            {
                var error = GetLastWin32Error();
                throw new Exception("InvalidFileHandle");
            }

            var bWriteHandle = ArchiveWinApi.CreateFile(destPath, (uint)(ArchiveWinApi.ACCESS_MASK.GENERIC_WRITE | ArchiveWinApi.ACCESS_MASK.WRITE_OWNER | ArchiveWinApi.ACCESS_MASK.WRITE_DAC | ArchiveWinApi.ACCESS_MASK.ACCESS_SYSTEM_SECURITY), 0, IntPtr.Zero, FileMode.Create, ArchiveWinApi.ExtendedFileAttributes.BackupSemantics, IntPtr.Zero);
            if (!IsValid(bWriteHandle))
            {
                var error = GetLastWin32Error();
                throw new Exception("InvalidFileHandle");
            }


            var bufferSize = 4096;
            var buffer = Marshal.AllocHGlobal(bufferSize);

            var backupReadContext = IntPtr.Zero;
            var backupWriteContext = IntPtr.Zero;

            uint lpNumberOfBytesRead = 0;
            uint lpNumberOfBytesWritten = 0;

            while (true)
            {
                var result = ArchiveWinApi.BackupRead(bReadHandle, out buffer, (uint) bufferSize, out lpNumberOfBytesRead, false, true, ref backupReadContext);
                if (!result)
                {
                    var error = GetLastWin32Error();
                    throw new Exception("BackupRead failed");
                }

                if (lpNumberOfBytesRead == 0) break;

                result = ArchiveWinApi.BackupWrite(bWriteHandle, buffer, lpNumberOfBytesRead, out lpNumberOfBytesWritten, false, true, ref backupWriteContext);
                if (!result)
                {
                    var error = GetLastWin32Error();
                    throw new Exception("BackupWrite failed");
                }
            }

            ArchiveWinApi.BackupRead(bReadHandle, out buffer, 0, out lpNumberOfBytesRead, true, true, ref backupReadContext);
            ArchiveWinApi.CloseHandle(bReadHandle);

            ArchiveWinApi.BackupWrite(bWriteHandle, buffer, 0, out lpNumberOfBytesWritten, true, true, ref backupWriteContext);
            ArchiveWinApi.CloseHandle(bWriteHandle);

我收到System.AccessViolationException:'尝试读取或写入受保护的内存。这通常表明其他内存已损坏。'

编辑2:

  [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
  internal static extern bool BackupRead(IntPtr hFile, out IntPtr lpBuffer, [MarshalAs(UnmanagedType.U4)] uint nNumberOfBytesToRead, [MarshalAs(UnmanagedType.U4)] out uint lpNumberOfBytesRead, [MarshalAs(UnmanagedType.Bool)] bool bAbort, [MarshalAs(UnmanagedType.Bool)] bool bProcessSecurity, ref IntPtr lpContext);

  [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
  internal static extern bool BackupWrite(IntPtr hFile, IntPtr lpBuffer, [MarshalAs(UnmanagedType.U4)] uint nNumberOfBytesToWrite, [MarshalAs(UnmanagedType.U4)] out uint lpNumberOfBytesWritten, [MarshalAs(UnmanagedType.Bool)] bool bAbort, [MarshalAs(UnmanagedType.Bool)] bool bProcessSecurity, ref IntPtr lpContext);

 [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
      internal static extern IntPtr CreateFile([MarshalAs(UnmanagedType.LPWStr)] string lpFileName, [MarshalAs(UnmanagedType.U4)] uint dwDesiredAccess, [MarshalAs(UnmanagedType.U4)] FileShare dwShareMode, IntPtr lpSecurityAttributes, [MarshalAs(UnmanagedType.U4)] FileMode dwCreationDisposition, [MarshalAs(UnmanagedType.U4)] ExtendedFileAttributes dwFlagsAndAttributes, IntPtr hTemplateFile);

public enum ACCESS_MASK : uint
  {
     READ_CONTROL = 0x00020000,
     WRITE_DAC = 0x00040000,
     WRITE_OWNER = 0x00080000,

     ACCESS_SYSTEM_SECURITY = 0x01000000,

     GENERIC_READ = 0x80000000,
     GENERIC_WRITE = 0x40000000,
     GENERIC_ALL = 0x10000000,
  }

  [Flags]
  public enum ExtendedFileAttributes
  {
     BackupSemantics = 33554432,
  }

2 个答案:

答案 0 :(得分:0)

检查问题/调试有点困难,因为您没有提供P / Invoke类(并且http://pinvoke.net可能不一定100%正确)。

是的,我知道帮助您解决这个具体问题是个好主意,但我想向您展示一些替代方法:

寻找https://github.com/alphaleonis/AlphaFS

这主要称为长路径类(长度> 259个字符的路径),但也有一个BackupRead类。我们多年来一直使用AlphaFS lib作为生产代码。

我写了一个小样本,快速而脏,没有错误检查:-)

using System.Security.AccessControl;
using Alphaleonis.Win32.Filesystem;
using Alphaleonis.Win32.Security;
...
...

var sourceFile = @"c:\temp\a.txt";
var destinationFile = @"c:\temp\b.txt";

var buffer = new byte[4096];

using (var privilegeEnabler = new PrivilegeEnabler(Privilege.Backup, Privilege.Restore))
{
    using (var readStream = new BackupFileStream(sourceFile,
                 System.IO.FileMode.Open, FileSystemRights.Read))
    using (var writeStream = new BackupFileStream(destinationFile,
                 System.IO.FileMode.Create, FileSystemRights.FullControl))
    {
        int count;

        while ((count = readStream.Read(buffer, 0, buffer.Length, true)) > 0)
        {
            writeStream.Write(buffer, 0, count, true);
        }
    }
}

这对我有用(使用NTFS备用数据流和安全性复制文件)。

我建议进一步测试稳定性,我没有使用AlphaFS的经验。

答案 1 :(得分:0)

在P / Invoke声明中

... BackupRead(IntPtr hFile, out IntPtr lpBuffer, ...

删除out。当然也在通话期间。

对于BackupRead和BackupWrite API,都给出了一个指向调用者提供的缓冲区的指针。这个指针将按值传递,不应在此处放置out参数。