Thanks for the code which was helpful for me to translate (to VB6) and get started.
I can not read the records as the DeviceIoControl function always returns the error "The supplied user buffer is not valid for the requested operation.". I have tried creating the buffer in every way I can conceive of (local, global, module variables and heap) but can't see what is going wrong. A pointer to get me started again would be great.
Thanks,
Bruce
Code:
Private Declare Function DeviceIoControlA Lib "kernel32.dll" Alias "DeviceIoControl" (ByVal hDevice As Long, ByVal dwIoControlCode As Long, lpInBuffer As Any, ByVal nInBufferSize As Long, ByVal lpOutBuffer As Long, ByVal nOutBufferSize As Long, ByRef lpBytesReturned As Long, lpOverlapped As OVERLAPPED) As Long
Private Type USN
Lo As Long
Hi As Long
End Type
Private Type CJDataType
NextUsn As USN
CJData(USN_PAGE_SIZE - 8) As Byte
End Type
Private CJData As CJDataType
Private Type READ_USN_JOURNAL_DATA
StartUsn As USN
ReasonMask As USN_REASONS
ReturnOnlyOnClose As Boolean
Timeout As Int64
BytesToWaitFor As Int64
UsnJournalID As Int64
End Type
Private Sub Form_Load()
Dim FSCTL_QUERY_USN_JOURNAL As Long
Dim FSCTL_READ_USN_JOURNAL As Long
Dim hCJ As Long
Dim ret As Long
Dim BytesReturned As Long
Dim ujd As USN_JOURNAL_DATA
Dim rujd As READ_USN_JOURNAL_DATA
Dim dPtr As Long, hHeap As Long
Dim CJData As CJDataType
FSCTL_QUERY_USN_JOURNAL = CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 61, METHOD_BUFFERED, FILE_SPECIAL_ACCESS)
FSCTL_READ_USN_JOURNAL = CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 46, METHOD_NEITHER, FILE_ANY_ACCESS)
hHeap = GetProcessHeap()
If hHeap = 0 Then Exit Sub
dPtr = HeapAlloc(hHeap, HEAP_GENERATE_EXCEPTIONS Or HEAP_ZERO_MEMORY, USN_PAGE_SIZE)
If dPtr = 0 Then
Debug.Print "memory allocation failed!"
Exit Sub
Else
Debug.Print Hex(dPtr) & " " & VarPtr(CJData)
Debug.Print HeapSize(hHeap, 0, dPtr)
End If
hCJ = CreateFile("\\.\C:", GENERIC_READ, FILE_SHARE_READ Or FILE_SHARE_WRITE, 0&, OPEN_EXISTING, 0&, 0&)
' Recall change cournal statistics
ret = DeviceIoControl(hCJ, FSCTL_QUERY_USN_JOURNAL, 0, 0, ujd, Len(ujd), BytesReturned, olData)
If ret = 0 Or BytesReturned <> Len(ujd) Then
MsgBox WinAPIError
Else
BytesReturned = 0
rujd.UsnJournalID = ujd.UsnJournalID
rujd.StartUsn = ujd.FirstUsn
rujd.ReasonMask = USN_MASK_ALL_REASONS ' Get every record
'rujd.ReturnOnlyOnClose = False
Do
' Get some records from the journal
'ret = DeviceIoControl(hCJ, FSCTL_READ_USN_JOURNAL, rujd, Len(rujd), CJData, USN_PAGE_SIZE, BytesReturned, olData)
ret = DeviceIoControlA(hCJ, FSCTL_READ_USN_JOURNAL, rujd, Len(rujd), dPtr, USN_PAGE_SIZE, BytesReturned, olData)
If ret = 0 Or BytesReturned <= Len(rujd) Then
Debug.Print WinAPIError()
Exit Do
End If
Stop
Loop While True
End If
ret = HeapFree(hHeap, 0, dPtr)
CloseHandle hCJ
End Sub