Results 1 to 6 of 6

Thread: [RESOLVED] which one of these is the sectors

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2009
    Posts
    573

    Resolved [RESOLVED] which one of these is the sectors

    i cant figure out which numbers to change to up the amount of sectors from one to 5 or 10
    thanks
    Code:
    Private Sub Command1_Click()
    'Read the Primary Master MBR
    
        Open "c:\mbr.bin" For Binary Access Write As #1
        For i = 0 To (1 - 1)
        DoEvents
        If i < (1 - 1) Then Put #1, i * 512 + 1, rawread("h:", i, False) 'PhysicalDrive0
        If i >= (1 - 1) Then Put #1, i * 512 + 1, rawread("h:", i, True) 'PhysicalDrive0
        Next i
        Close #1
    End Sub
    'module

    Code:
    Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As Any, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
    Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    Private Declare Function SetFilePointer Lib "kernel32" (ByVal hFile As Long, ByVal lDistanceToMove As Long, lpDistanceToMoveHigh As Long, ByVal dwMoveMethod As Long) As Long
    Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, ByVal lpOverlapped As Long) As Long
    Private Declare Function WriteFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, lpOverlapped As Long) As Long
    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
    
    Private Const INVALID_HANDLE_VALUE = -1
    Private Const OPEN_EXISTING = 3
    Private Const GENERIC_READ = &H80000000
    Private Const GENERIC_WRITE = &H40000000
    Private Const FILE_SHARE_READ = &H1
    Private Const FILE_SHARE_WRITE = &H2
    Private Const FILE_CURRENT = 1
    Public hDevice As Long
    
    Public Function rawread(ByVal rDrive As String, ByVal SectorToRead As Long, ByVal LastSectorToRead As Boolean) As String
    If Len(rDrive) < 4 Then rDrive = Left(rDrive, 2)
    
    Dim abBuff() As Byte
    Dim abResult() As Byte
    Dim nSectors As Long
    Dim nRead As Long
    
    iOffset = SectorToRead
    cBytes = 512
    BytesPerSector = 512
    If hDevice < 1 Then hDevice = CreateFile("\\.\" & rDrive, GENERIC_READ Or GENERIC_WRITE, 0, ByVal 0, OPEN_EXISTING, 0, 0)
    If hDevice = INVALID_HANDLE_VALUE Then Exit Function
    Call SetFilePointer(hDevice, SectorToRead * BytesPerSector, 0, FILE_BEGIN)
    ReDim abResult(cBytes - 1)
    ReDim abBuff(1 * BytesPerSector - 1)
    Call ReadFile(hDevice, abBuff(0), UBound(abBuff) + 1, nRead, 0&)
    CopyMemory abResult(0), abBuff(0), cBytes
    
    rawread = StrConv(abResult, vbUnicode)
        
    If LastSectorToRead = True Then CloseHandle hDevice: MsgBox "end"
    End Function
    
    Public Function rawwrite(ByVal rDrive As String, ByVal SectorToWrite As Integer, ByVal writestring As String, ByVal LastSectorToWrite As Boolean) As Integer
    If Len(rDrive) < 4 Then rDrive = Left(rDrive, 2)
    
    Dim BytesPerSector As Long
    Dim abBuff() As Byte
    Dim abResult() As Byte
    Dim nSectors As Long
    Dim nWritten As Long
    
    abResult = writestring
    iOffset = SectorToWrite
    cBytes = 512
    BytesPerSector = 512
        
    If hDevice < 1 Then hDevice = CreateFile("\\.\A:", GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0&, OPEN_EXISTING, 0, 0)
    
    If hDevice = INVALID_HANDLE_VALUE Then rawwrite = 0: Exit Function
    
    ReDim abBuff(1 * BytesPerSector - 1)
    CopyMemory abBuff(0), abResult(0), Len(abResult(0))
    Call SetFilePointer(hDevice, SectorToWrite * BytesPerSector, 0, FILE_BEGIN)
    Call WriteFile(hDevice, abResult(0), BytesPerSector, nWritten, ByVal 0&)
    rawwrite = nWritten
    
    If LastSectorToWrite = True Then CloseHandle hDevice
    End Function

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: which one of these is the sectors

    512 is bytes per sector, so "i" must be the sector.
    Note that you cannot hardcode 512. It is a default value, but can be modified. Drives can have different bytes per sector values
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2009
    Posts
    573

    Re: which one of these is the sectors

    is there a difference between a .bin and a .vhd file?
    and is this the right way to write to this type of file?

    i found another way to write a bigger file but not sure which would be best.

    Code:
    Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As Any, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
    
    
    Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    
    
    Private Declare Function SetFilePointer Lib "kernel32" (ByVal hFile As Long, ByVal lDistanceToMove As Long, lpDistanceToMoveHigh As Long, ByVal dwMoveMethod As Long) As Long
    
    
    Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, ByVal lpOverlapped As Long) As Long
    Private Declare Function WriteFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, lpOverlapped As Long) As Long
    
    
    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
        Private Const INVALID_HANDLE_VALUE = -1
        Private Const OPEN_EXISTING = 3
        Private Const GENERIC_READ = &H80000000
        Private Const GENERIC_WRITE = &H40000000
    
    
    Private Sub Command1_Click()
        Dim hDevice As Long
        iOffSet = 0
        cBytes = 512
        BytesPerSector = 512
        
        Dim abBuff() As Byte
        Dim abResult() As Byte
        Dim nSectors As Long
        Dim nRead As Long
        Dim writer As String
        nSectors = Int((iOffSet + cBytes - 1) / BytesPerSector) + 1
        hDevice = CreateFile("\\.\h:", GENERIC_READ Or GENERIC_WRITE, 0, ByVal 0, OPEN_EXISTING, 0, 0)
    
    
        For i = 0 To (2880 - 1) '2880/2 is 1440k
    
    
            DoEvents
                If hDevice = INVALID_HANDLE_VALUE Then Exit Sub
                Call SetFilePointer(hDevice, i * BytesPerSector, 0, FILE_BEGIN)
                ReDim abResult(cBytes - 1)
                ReDim abBuff(nSectors * BytesPerSector - 1)
                Call ReadFile(hDevice, abBuff(0), UBound(abBuff) + 1, nRead, 0&)
                CopyMemory abResult(0), abBuff(iOffSet), cBytes
                Open "c:\floppy.bin" For Binary Access Write As #1
                writer = StrConv(abResult, vbUnicode)
                Put #1, i * 512 + 1, writer
                Close #1
            Next i
            CloseHandle hDevice: MsgBox "end"
        End Sub

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2009
    Posts
    573

    Re: which one of these is the sectors

    i googled vhd then iso files ,there verry complacated aint they?

  5. #5
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: which one of these is the sectors

    The type of file is not an issue I think unless you want it compatible with some other software. If you can successfully ghost the harddrive, you can use whatever format you want to use so that your app can restore the ghost.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2009
    Posts
    573

    Re: which one of these is the sectors

    thanks lavolpe for all your help,some of this stuff is hard to understand.
    thanks again

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width