|
-
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.
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
|