|
-
Jan 24th, 2012, 09:18 AM
#1
Thread Starter
Fanatic Member
[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
-
Jan 24th, 2012, 11:44 AM
#2
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
-
Jan 24th, 2012, 03:10 PM
#3
Thread Starter
Fanatic Member
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
-
Jan 24th, 2012, 07:32 PM
#4
Thread Starter
Fanatic Member
Re: which one of these is the sectors
i googled vhd then iso files ,there verry complacated aint they?
-
Jan 25th, 2012, 08:45 AM
#5
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.
-
Jan 25th, 2012, 09:15 AM
#6
Thread Starter
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|