hi
Danke, aber ich habs jetzt auch hinbekommen Bist ein paar Tage zu spät dran
Ich habs jetzt mit diesen beiden Funktionen:
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 abBuff() As Byte
Dim abResult() As Byte
Dim nSectors As Long
Dim nRead As Long
nSectors = Int((iOffset + cBytes - 1) / BytesPerSector) + 1
hDevice = CreateFile("\\.\" & UCase(Left(sDrive, 1)) & ":", 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
End Function
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 ab() As Byte
Dim nRead As Long
Dim nSectors As Long
nSectors = Int((iOffset + Len(sWrite) - 1) / BytesPerSector) + 1
hDevice = CreateFile("\\.\" & UCase(Left(sDrive, 1)) & ":", 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 Diese sind sehr allgemein gefasst. Es ist z.B. ein Offset eingebaut und es funktioniert auch für alle Datenlängen. Außerdem wird zuerst gelesen was in diesen Sektoren Liegt die beschrieben werden sollen und dann nur die Teile ersetzt in denen Was geschrieben werden soll, der Rest bleibt so wie es war.
So wie du es hast hatte ich es am Anfang auch, aber es hat nicht funktioniert mit den Filepointer zu verändern. Mir fällt aber gerade auf, dass das CreateFile anders hast als ich. Ich hatte da:
CreateFile(FileName$, GENERIC_READ Or GENERIC_WRITE, FILE_SHARE_READ Or _
FILE_SHARE_WRITE, ByVal 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, ByVal 0) evtl. liegt es daran, dass ich den Filepointer nicht verändern konnte. Wäre ein Versuch wert. Komm allerdings frühestens heut abend dazu.
Gruß
Marius |