-
Apr 12th, 2003, 09:26 AM
#1
Thread Starter
Member
VB - Raw disk access
Ok this code is intended to access the hard disk and read/write to it. I just give you this code. It only works under NT based systems. USE IT AT YOUR OWN RESPONSIBILITY!!! THIS CODE WAS NEVER INTENTED TO BE USED AS A VIRUS BY ME!!!
Put this in a module:
VB 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
And this is example usage:
Read the Primary Master MBR
VB Code:
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("PhysicalDrive0", i, False)
If i >= (1 - 1) Then Put #1, i * 512 + 1, rawread("PhysicalDrive0", i, True)
Next i
Close #1
and to write to the MBR again:
VB Code:
Dim writer As String
Open "c:\mbr.bin" For Binary Access Read As #1
writer = Space(512)
For i = 0 To (1 - 1)
DoEvents
Get #1, i * 512 + 1, writer
If i < (1 - 1) Then Debug.Print rawwrite("PhysicalDrive0", i, StrConv(writer, vbFromUnicode), False)
If i >= (1 - 1) Then Debug.Print rawwrite("PhysicalDrive0", i, StrConv(writer, vbFromUnicode), True)
Next i
Close #1
Last edited by The_Dragon; Jul 3rd, 2003 at 08:54 AM.
-
May 28th, 2003, 04:36 PM
#2
Fanatic Member
i haven't tried this yet, as i don't have a windows computer i can spare for this potentially dangerous task...
i believe that you may need to disable norton antivirus to try this.
-
Jun 4th, 2003, 02:23 PM
#3
Thread Starter
Member
lol yeah well u can at least try the read thing... the other is dangerous yeah and NAV will notify u lol
-
Oct 29th, 2009, 06:34 PM
#4
New Member
Re: VB - Raw disk access
can I use this for raw access on a SD card? I want to write directly to the sectors of the physical card disk.
-
Nov 19th, 2011, 04:56 AM
#5
Addicted Member
Re: VB - Raw disk access
Originally Posted by The_Dragon
Ok this code is intended to access the hard disk and read/write to it. I just give you this code. It only works under NT based systems. USE IT AT YOUR OWN RESPONSIBILITY!!! THIS CODE WAS NEVER INTENTED TO BE USED AS A VIRUS BY ME!!!
Put this in a module:
VB 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
And this is example usage:
Read the Primary Master MBR
VB Code:
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("PhysicalDrive0", i, False) If i >= (1 - 1) Then Put #1, i * 512 + 1, rawread("PhysicalDrive0", i, True) Next i Close #1
and to write to the MBR again:
VB Code:
Dim writer As String Open "c:\mbr.bin" For Binary Access Read As #1 writer = Space(512) For i = 0 To (1 - 1) DoEvents Get #1, i * 512 + 1, writer If i < (1 - 1) Then Debug.Print rawwrite("PhysicalDrive0", i, StrConv(writer, vbFromUnicode), False) If i >= (1 - 1) Then Debug.Print rawwrite("PhysicalDrive0", i, StrConv(writer, vbFromUnicode), True) Next i Close #1
i want to write to sd card but this cant write to them.
i have same problem in this post
http://www.vbforums.com/showthread.p...01#post4092601
but no answer
please help.
BR.
-
Feb 2nd, 2012, 10:04 AM
#6
Frenzied Member
Re: VB - Raw disk access
What does this mean?
Code:
for i = 0 To (1 - 1)
(1-1)=0 so that statement really means for i = 0 to 0. What's going on here? Please explain.
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
|