Quote Originally Posted by The_Dragon View Post
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:
  1. 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
  2. Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
  3. Private Declare Function SetFilePointer Lib "kernel32" (ByVal hFile As Long, ByVal lDistanceToMove As Long, lpDistanceToMoveHigh As Long, ByVal dwMoveMethod As Long) As Long
  4. 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
  5. 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
  6. Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
  7.  
  8. Private Const INVALID_HANDLE_VALUE = -1
  9. Private Const OPEN_EXISTING = 3
  10. Private Const GENERIC_READ = &H80000000
  11. Private Const GENERIC_WRITE = &H40000000
  12. Private Const FILE_SHARE_READ = &H1
  13. Private Const FILE_SHARE_WRITE = &H2
  14. Private Const FILE_CURRENT = 1
  15. Public hDevice As Long
  16.  
  17. Public Function rawread(ByVal rDrive As String, ByVal SectorToRead As Long, ByVal LastSectorToRead As Boolean) As String
  18. If Len(rDrive) < 4 Then rDrive = Left(rDrive, 2)
  19.  
  20. Dim abBuff() As Byte
  21. Dim abResult() As Byte
  22. Dim nSectors As Long
  23. Dim nRead As Long
  24.  
  25. iOffset = SectorToRead
  26. cBytes = 512
  27. BytesPerSector = 512
  28. If hDevice < 1 Then hDevice = CreateFile("\\.\" & rDrive, GENERIC_READ Or GENERIC_WRITE, 0, ByVal 0, OPEN_EXISTING, 0, 0)
  29. If hDevice = INVALID_HANDLE_VALUE Then Exit Function
  30. Call SetFilePointer(hDevice, SectorToRead * BytesPerSector, 0, FILE_BEGIN)
  31. ReDim abResult(cBytes - 1)
  32. ReDim abBuff(1 * BytesPerSector - 1)
  33. Call ReadFile(hDevice, abBuff(0), UBound(abBuff) + 1, nRead, 0&)
  34. CopyMemory abResult(0), abBuff(0), cBytes
  35.  
  36. rawread = StrConv(abResult, vbUnicode)
  37.    
  38. If LastSectorToRead = True Then CloseHandle hDevice: MsgBox "end"
  39. End Function
  40.  
  41. Public Function rawwrite(ByVal rDrive As String, ByVal SectorToWrite As Integer, ByVal writestring As String, ByVal LastSectorToWrite As Boolean) As Integer
  42. If Len(rDrive) < 4 Then rDrive = Left(rDrive, 2)
  43.  
  44. Dim BytesPerSector As Long
  45. Dim abBuff() As Byte
  46. Dim abResult() As Byte
  47. Dim nSectors As Long
  48. Dim nWritten As Long
  49.  
  50. abResult = writestring
  51. iOffset = SectorToWrite
  52. cBytes = 512
  53. BytesPerSector = 512
  54.    
  55. If hDevice < 1 Then hDevice = CreateFile("\\.\A:", GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0&, OPEN_EXISTING, 0, 0)
  56.  
  57. If hDevice = INVALID_HANDLE_VALUE Then rawwrite = 0: Exit Function
  58.  
  59. ReDim abBuff(1 * BytesPerSector - 1)
  60. CopyMemory abBuff(0), abResult(0), Len(abResult(0))
  61. Call SetFilePointer(hDevice, SectorToWrite * BytesPerSector, 0, FILE_BEGIN)
  62. Call WriteFile(hDevice, abResult(0), BytesPerSector, nWritten, ByVal 0&)
  63. rawwrite = nWritten
  64.  
  65. If LastSectorToWrite = True Then CloseHandle hDevice
  66. End Function

And this is example usage:

Read the Primary Master MBR
VB Code:
  1. Open "c:\mbr.bin" For Binary Access Write As #1
  2. For i = 0 To (1 - 1)
  3. DoEvents
  4. If i < (1 - 1) Then Put #1, i * 512 + 1, rawread("PhysicalDrive0", i, False)
  5. If i >= (1 - 1) Then Put #1, i * 512 + 1, rawread("PhysicalDrive0", i, True)
  6. Next i
  7. Close #1

and to write to the MBR again:
VB Code:
  1. Dim writer As String
  2.  
  3. Open "c:\mbr.bin" For Binary Access Read As #1
  4.  
  5. writer = Space(512)
  6.  
  7. For i = 0 To (1 - 1)
  8. DoEvents
  9. Get #1, i * 512 + 1, writer
  10. If i < (1 - 1) Then Debug.Print rawwrite("PhysicalDrive0", i, StrConv(writer, vbFromUnicode), False)
  11. If i >= (1 - 1) Then Debug.Print rawwrite("PhysicalDrive0", i, StrConv(writer, vbFromUnicode), True)
  12. Next i
  13.  
  14. 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.