|
-
Nov 15th, 2011, 02:11 PM
#1
Thread Starter
Addicted Member
howto open disk and write file in some offsset
hi.
how can i open disk(removable disk like sd card) in Phisycal Media NOT LOGICAL and write a file(384 bytes) from 0 offset to 170 offset.
many thanks.
BR.
-
Nov 15th, 2011, 02:17 PM
#2
Re: howto open disk and write file in some offsset
BR
Do you know how to create a file on your hard drive?
Spoo
-
Nov 15th, 2011, 02:41 PM
#3
Thread Starter
Addicted Member
Re: howto open disk and write file in some offsset
i cant understand your question.
but i want to write a file tu sector 0 in disk partition (physical)(like open disk in Hex softwares)
thanks
-
Nov 15th, 2011, 04:32 PM
#4
Re: howto open disk and write file in some offsset
BR
I was trying to get a better feel for your situation.
- You know how to create a text file on drive C:, but don't know how to
write a file to sector 0 in disk partition (physical)(like open disk in Hex softwares) - You don't even know how to create a text file on drive C:
Could you perhaps elaborate as to which applies.
If "1" applies, then I don't know either .. 
Spoo
-
Nov 16th, 2011, 03:35 AM
#5
Thread Starter
Addicted Member
Re: howto open disk and write file in some offsset
hi
ok i think you cant understand.
i need somthing like this but i want write file in disk.
this only write some bytes, i want write file not bytes.
Code:
Option Explicit
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length 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, ByVal lpOverlapped As Long) As Long
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Long, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Private Declare Function FlushFileBuffers Lib "kernel32" (ByVal hFile As Long) As Long
Private Declare Function LockFile Lib "kernel32" (ByVal hFile As Long, ByVal dwFileOffsetLow As Long, ByVal dwFileOffsetHigh As Long, ByVal nNumberOfBytesToLockLow As Long, ByVal nNumberOfBytesToLockHigh As Long) As Long
Private Declare Function UnlockFile Lib "kernel32" (ByVal hFile As Long, ByVal dwFileOffsetLow As Long, ByVal dwFileOffsetHigh As Long, ByVal nNumberOfBytesToUnlockLow As Long, ByVal nNumberOfBytesToUnlockHigh As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Const GENERIC_READ = &H80000000
Private Const GENERIC_WRITE = &H40000000
Private Const FILE_SHARE_READ = &H1
Private Const FILE_SHARE_WRITE = &H2
Private Const OPEN_EXISTING = 3
Private Const INVALID_HANDLE_VALUE = -1&
Private Const BytesPerSector = 512
Private Const FILE_BEGIN = 0
Private Function DirectWriteDriveNT(ByVal sDrive As String, ByVal iStartSec As Long, ByVal iOffset As Long, ByVal sWrite As String) As Boolean
Dim hDevice As Long
Dim abBuff() As Byte
Dim nSectors As Long
Dim nRead As Long
Dim ab() As Byte
nSectors = Int((iOffset + Len(sWrite) - 1) / BytesPerSector) + 1
hDevice = CreateFile(sDrive, GENERIC_READ Or GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, 0&, OPEN_EXISTING, 0&, 0&)
If hDevice = INVALID_HANDLE_VALUE Then Exit Function
abBuff = DirectReadDriveNT(sDrive, iStartSec, 0, nSectors * BytesPerSector)
ab = StrConv(sWrite, vbFromUnicode)
CopyMemory abBuff(iOffset), ab(0), Len(sWrite)
Call SetFilePointer(hDevice, iStartSec * BytesPerSector, 0, FILE_BEGIN)
Call LockFile(hDevice, LoWord(iStartSec * BytesPerSector), HiWord(iStartSec * BytesPerSector), LoWord(nSectors * BytesPerSector), HiWord(nSectors * BytesPerSector))
DirectWriteDriveNT = WriteFile(hDevice, abBuff(0), UBound(abBuff) + 1, nRead, 0&)
Call FlushFileBuffers(hDevice)
Call UnlockFile(hDevice, LoWord(iStartSec * BytesPerSector), HiWord(iStartSec * BytesPerSector), LoWord(nSectors * BytesPerSector), HiWord(nSectors * BytesPerSector))
CloseHandle hDevice
End Function
Private Function DirectReadDriveNT(ByVal sDrive As String, ByVal iStartSec As Long, ByVal iOffset As Long, ByVal cBytes As Long) As Variant
Dim hDevice As Long
Dim nSectors As Long
Dim nRead As Long
Dim abResult() As Byte
nSectors = Int((iOffset + cBytes - 1) / BytesPerSector) + 1
hDevice = CreateFile(sDrive, GENERIC_READ Or GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, 0&, OPEN_EXISTING, 0&, 0&)
If hDevice = INVALID_HANDLE_VALUE Then Exit Function
Call SetFilePointer(hDevice, iStartSec * BytesPerSector, 0, FILE_BEGIN)
ReDim abResult(cBytes - 1)
ReDim abBuff(nSectors * BytesPerSector - 1)
Call ReadFile(hDevice, abBuff(0), UBound(abBuff) + 1, nRead, 0&)
CloseHandle hDevice
CopyMemory abResult(0), abBuff(iOffset), cBytes
DirectReadDriveNT = abResult '(0)
End Function
Private Function LoWord(ByVal dw As Long) As Integer
If dw And &H8000& Then
LoWord = dw Or &HFFFF0000
Else
LoWord = dw And &HFFFF&
End If
End Function
Private Function HiWord(ByVal dw As Long) As Integer
HiWord = (dw And &HFFFF0000) \ 65536
End Function
Private Function hex2ascii(ByVal hextext As String) As String
Dim y As Integer
Dim num
Dim Value
For y = 1 To Len(hextext)
num = Mid(hextext, y, 2)
Value = Value & Chr(Val("&h" & num))
y = y + 1
Next y
hex2ascii = Value
End Function
-
Nov 16th, 2011, 03:56 AM
#6
Re: howto open disk and write file in some offsset
 Originally Posted by kakablack
this only write some bytes, i want write file not bytes.
A 'file' is just a collection of bytes and only of use if there's a file system that points to it and it's format and contents adhere to some 'rules'.
What exactly are you trying to do? I don't quite see how you're going to fit 384 bytes in 171 locations. Also, messing around at offset zero and nearby may make the device unreadable.
-
Nov 16th, 2011, 01:17 PM
#7
Thread Starter
Addicted Member
Re: howto open disk and write file in some offsset
 Originally Posted by Doogle
A 'file' is just a collection of bytes and only of use if there's a file system that points to it and it's format and contents adhere to some 'rules'.
What exactly are you trying to do? I don't quite see how you're going to fit 384 bytes in 171 locations. Also, messing around at offset zero and nearby may make the device unreadable.
thanks for reply
but 384 bytes is decimal and 171(180) is hexdecimal
i want to open file and write to sector 0.
and no damage to disk because sector 0 is not important if open disk in physical mode
-
Nov 17th, 2011, 05:50 AM
#8
Thread Starter
Addicted Member
Re: howto open disk and write file in some offsset
-
Nov 17th, 2011, 03:49 PM
#9
Re: howto open disk and write file in some offsset
Maybe a look at Get HD Serial w/o WMI (which only reads some raw data) will give you an idea of what you are up against and how you might proceed. At least a start.
But note that in Windows after XP these devices are secured now and require elevated access. Too much malware was using this for bootsector virus and rootkit emplanting.
But surely that's not where you were going, right.... ?
-
Nov 18th, 2011, 12:10 PM
#10
Thread Starter
Addicted Member
Re: howto open disk and write file in some offsset
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
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 found this but cant write to removable storage like sd card.
how to solve that?
-
Nov 18th, 2011, 12:15 PM
#11
Thread Starter
Addicted Member
Re: howto open disk and write file in some offsset
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
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 found this but cant write to removable storage like sd card.
how to solve that?
-
Nov 18th, 2011, 12:22 PM
#12
Hyperactive Member
Re: howto open disk and write file in some offsset
Maybe sd cards have different storage structure than hard disk?
If your problem is solved, then drag down the Thread Tools and mark your thread as Resolved.
If I helped you solve your problem, inflate some air into my ego by rating my post and adding a comment too.
For notorious issues (elaborate yourself) contact me via PM. I don't answer them in the forums EVER.
-
Nov 18th, 2011, 12:48 PM
#13
Re: howto open disk and write file in some offsset
I don't think you can get at them as a PhysicalDriveX device.
See http://www.emmet-gray.com/Articles/U...ialNumbers.htm which may provide some help if you can wade through it all.
-
Nov 19th, 2011, 04:54 AM
#14
Thread Starter
Addicted Member
Re: howto open disk and write file in some offsset
thank to all
bu i can read from my sd card in offset 0 to my own length but can write to them.
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
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
|