RedCamel 2016-01-20 23:39 采纳率: 0%
浏览 2220

windows10下采用ReadFile读磁盘失败

求高手解答,给出解决方法,谢谢先!
我的小段程序原来可以读整个500G磁盘,XP,WIN7,WIN8下无论32位还是64位,
测试都OK,但在WIN10下失败,只能读到4G。
程序如下:
Public Class Form1
Public hDisk As Integer

Public Declare Function CreateFileA Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Integer, ByVal dwShareMode As Integer, ByVal lpSecurityAttributes As IntPtr, ByVal dwCreationDisposition As Integer, ByVal dwFlagsAndAttributes As Integer, ByVal hTemplateFile As IntPtr) As Integer
Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Integer) As Integer
Public Declare Function SetFilePointer Lib "kernel32" (ByVal hFile As Integer, ByVal lDistanceToMove As Integer, ByRef lpDistanceToMoveHigh As Integer, ByVal dwMoveMethod As Integer) As Integer
Public Declare Function ReadFile Lib "kernel32" (ByVal hFile As Integer, ByVal lpBuffer() As Byte, ByVal nNumberOfBytesToRead As Integer, ByRef lpNumberOfBytesRead As Integer, ByVal lpOverLapped As Integer) As Integer

Public Enum DesiredAccess
    Read = &H80000000
    Write = &H40000000
    Execute = &H20000000
    All = &H10000000
End Enum

Public Enum ShareMode
    Delete = &H4
    Write = &H2
    Read = &H1
    None = 0
End Enum

Public Enum CreateMode
    CreateNew = 1
    CreateAlways = 2
    OpenExisting = 3
    OpenAlways = 4
    TruncateExisting = 5
End Enum

Public Enum OpResult
    ERROR_SUCCESS = 0
    INVALID_HANDLE_VALUE = -1
End Enum

Public Enum FileSeek
    FILE_BEGIN = 0
    FILE_CURRENT = 1
    FILE_END = 2
End Enum

Public Function OpenDisk(ByVal fileName As String) As Boolean
    Try
        hDisk = CreateFileA(fileName, DesiredAccess.Read Or DesiredAccess.Write, ShareMode.Read Or ShareMode.Write, IntPtr.Zero, CreateMode.OpenExisting, 0, IntPtr.Zero)
        OpenDisk = Not (hDisk = OpResult.INVALID_HANDLE_VALUE)
    Catch ex As Exception
        hDisk = False
    End Try
    Return hDisk
End Function

Public Function CloseDisk() As Boolean
    Try
        CloseDisk = CType(CloseHandle(hDisk), Boolean)
    Catch ex As Exception
        CloseDisk = False
    End Try
    Return CloseDisk
End Function

Public Function FxSetFilePointer(ByVal hFile As Integer, ByVal lDistanceToMove As Long, ByVal dwMoveMethod As Integer) As Boolean
    Try
        Dim PosL As Integer
        Dim PosH As Integer
        Dim ret As Integer

        Size2Long(lDistanceToMove, PosL, PosH)
        ret = SetFilePointer(hFile, PosL, PosH, dwMoveMethod)
        If ret = PosL Then
            FxSetFilePointer = True
        Else
            FxSetFilePointer = False
        End If
    Catch ex As Exception
        FxSetFilePointer = False
    End Try
    Return FxSetFilePointer
End Function

Private Sub Size2Long(ByVal FileSize As Long, ByRef IntLow As Integer, ByRef IntHigh As Integer)
    Try
        '&HFFFFFFFF unsigned = 4294967295
        Dim Cutoff As Long

        Cutoff = 2147483647
        Cutoff = Cutoff + 2147483647
        Cutoff = Cutoff + 1 ' now we hold the value of 4294967295 and not -1
        IntHigh = 0
        Do Until FileSize < Cutoff
            IntHigh = IntHigh + 1
            FileSize = FileSize - Cutoff
        Loop
        If FileSize > 2147483647 Then
            IntLow = CType(-Cutoff + (FileSize - 1), Integer)
        Else
            IntLow = CType(FileSize, Integer)
        End If
    Catch ex As Exception
        Throw ex
    End Try
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim a(512 - 1) As Byte

    If OpenDisk("\\.\PhysicalDrive0") = False Then
        MessageBox.Show("Cannot access disk", "Warnning", MessageBoxButtons.OK, MessageBoxIcon.Stop)
        End
    End If

    If FxSetFilePointer(hDisk, 0L * 512, FileSeek.FILE_BEGIN) = False Then
        Exit Sub
    End If

    If ReadFile(hDisk, a, 512, 0, 0) = 0 Then
        Exit Sub
    End If

    If FxSetFilePointer(hDisk, 8388607L * 512, FileSeek.FILE_BEGIN) = False Then
        Exit Sub
    End If

    If ReadFile(hDisk, a, 512, 0, 0) = 0 Then
        Exit Sub
    End If

    If FxSetFilePointer(hDisk, 8388608L * 512, FileSeek.FILE_BEGIN) = False Then
        Exit Sub
    End If

    'when sectors number>8388607, ReadFile failed, why, what can I do
    If ReadFile(hDisk, a, 512, 0, 0) = 0 Then
        Exit Sub
    End If

    If CloseDisk() = False Then
        MessageBox.Show("Some errors have been occured", "Warnning", MessageBoxButtons.OK, MessageBoxIcon.Stop)
        End
    End If
End Sub

End Class

  • 写回答

3条回答

  • 91program 博客专家认证 2016-01-21 00:31
    关注

    建议你设个断点测试一下,看看哪个函数失败了?再从失败的函数处,获取失败码,再来分析原因。

    评论

报告相同问题?

悬赏问题

  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?