Directdiskaccess

Soyez le premier à donner votre avis sur cette source.

Snippet vu 5 342 fois - Téléchargée 18 fois

Contenu du snippet

Petite classe d'accès direct au disque
L'exemple fonctionne en projet type console avec une présentation peu travaillée ;p
Par contre, sous Vista et Win 7, il faut les droits d'administrateurs ...

Source / Exemple :


Module Main
    Sub main()
        Dim tmp As New DiskDirectAccess
        tmp.OpenDrive(0)
        Dim buff() As Byte = tmp.Read(0, 2048)
        tmp.CloseDrive()
        Console.WriteLine("     | 00 01 02 03 04 05 06 07 | 0 1 2 3 4 5 6 7")
        Console.WriteLine("------------------------------------------------")
        For i = 0 To buff.Length - 1 Step 8
            Console.WriteLine(i.ToString("0000") & " | " & _
                              buff(i).ToString("X2") & " " & buff(i + 1).ToString("X2") & " " & buff(i + 2).ToString("X2") & " " & buff(i + 3).ToString("X2") & " " & buff(i + 4).ToString("X2") & " " & buff(i + 5).ToString("X2") & " " & buff(i + 6).ToString("X2") & " " & buff(i + 7).ToString("X2") & " | " & _
                              Chr(buff(i)) & " " & Chr(buff(i + 1)) & " " & Chr(buff(i + 2)) & " " & Chr(buff(i + 3)) & " " & Chr(buff(i + 4)) & " " & Chr(buff(i + 5)) & " " & Chr(buff(i + 6)) & " " & Chr(buff(i + 7)))
        Next
        Console.ReadKey()
    End Sub
End Module

Class DiskDirectAccess
    Public Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As UInteger, ByVal dwShareMode As UInteger, ByVal lpSecurityAttributes As IntPtr, ByVal dwCreationDisposition As UInteger, ByVal dwFlagsAndAttributes As UInteger, ByVal hTemplateFile As IntPtr) As IntPtr
    Public Declare Function CloseHandle Lib "kernel32.dll" (ByVal hFile As IntPtr) As Integer
    Public Declare Function ReadFile Lib "kernel32.dll" (ByVal hFile As IntPtr, ByVal lpBuffer As Byte(), ByVal nNumberOfBytesToRead As UInteger, ByRef lpNumberOfBytesRead As UInteger, ByVal lpOverlapped As IntPtr) As Integer
    Public Declare Function SetFilePointer Lib "kernel32.dll" (ByVal hFile As IntPtr, ByVal lDistanceToMove As Integer, ByRef lpDistanceToMoveHigh As Integer, ByVal dwMoveMethod As Integer) As Integer

    Public Const GENERIC_WRITE As UInteger = 1073741824 '&H40000000
    Public Const GENERIC_READ As UInteger = 2147483648 '&H80000000
    Public Const INVALID_HANDLE_VALUE As Integer = -1
    Public Const OPEN_EXISTING As Integer = 3
    Public Const FILE_ATTRIBUTE_NORMAL As Integer = 128
    Public Const FILE_SHARE_READ As Integer = &H1
    Public Const FILE_SHARE_WRITE As Integer = &H2

    Dim hDrive As IntPtr

    Function OpenDrive(ByVal driveid As UShort) As Boolean
        hDrive = CreateFile("\\.\PhysicalDrive" & driveid, GENERIC_READ, FILE_SHARE_READ Or FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, IntPtr.Zero)

        If hDrive <> INVALID_HANDLE_VALUE Then
            Return True
        End If
    End Function

    Sub CloseDrive()
        CloseHandle(hDrive)
    End Sub

    Function Read(ByVal start As ULong, ByVal lenght As ULong) As Byte()
        Dim buffer(lenght - 1) As Byte
        Dim ret As Integer

        'Seek part
        Dim hex As String
        Dim low As Integer, high As Integer
        hex = (Convert.ToInt64(start)).ToString("X16")
        low = Convert.ToInt32(hex.Substring(8, 8), 16)
        high = Convert.ToInt32(hex.Substring(0, 8), 16)
        ret = SetFilePointer(hDrive, low, high, 0)
        If ret = -1 Then
            Err.Raise(513, "DiskDirectAccess", "Error occured while trying to access the hard drive")
            Exit Function
        End If

        'Read part
        Dim BytesRead As UInteger
        ret = ReadFile(hDrive, buffer, lenght, BytesRead, IntPtr.Zero)
        If ret = 0 Then
            Err.Raise(514, "DiskDirectAccess", "Error occured while reading the hard drive")
            Exit Function
        End If

        Return buffer
    End Function
End Class

Conclusion :


Finalement, ce n'est pas si compliqué que ça les accès directs :D
Source fortement inspirée de celle de Galain (http://www.vbfrance.com/codes/ACCES-DIRECT-DISQUES-PARTITIONS-VB-NET_46012.aspx) donc un grand merci à lui :P sauf que j'ai fais la séparation du code et un peu simplifié, modifié, et compacté certaines choses

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.