G
Guest
I am trying to query the change journal using the deviceIOControl API. The API doesn't return an error, but all of the values in the output buffer are zero, and they shouldn't be. My code is below. I have also tried specifying different pack values in my USN_JOURNAL_DATA structure definition but this didn't change anything. Any help would be greatly appreciated
Private Const FILE_SHARE_READ = &H1
Private Const FILE_SHARE_WRITE = &H2
Private Const OPEN_EXISTING = 3
Private Const METHOD_BUFFERED = &H0
Private Const FILE_DEVICE_FILE_SYSTEM = &H9
Private Const FILE_ANY_ACCESS = &H0
Private Const FILE_SPECIAL_ACCESS = FILE_ANY_ACCESS
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> Public Structure USN_JOURNAL_DATA
Public UsnJournalID As Long
Public FirstUsn As Int64
Public NextUsn As Int64
Public LowestValidUsn As Int64
Public MaxUsn As Int64
Public MaximumSize As Long
Public AllocationDelta As Long
End Structure
Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal DesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Integer, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Declare Auto Function DeviceIoControl Lib "kernel32.dll" (ByVal hDevice As Long, ByVal dwIoControlCode As Integer, ByVal lpInBuffer As Integer, ByVal nInBufferSize As Integer, <MarshalAs(UnmanagedType.Struct)> ByRef lpOutBuffer As USN_JOURNAL_DATA, ByVal nOutBufferSize As Integer, ByRef lpBytesReturned As Integer, ByVal lpOverlapped As Integer) As Long
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim FSCTL_QUERY_USN_JOURNAL As Long = CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 61, METHOD_BUFFERED, FILE_SPECIAL_ACCESS)
Dim aHandle As Long
Dim anObject As Long
Dim byteBack As Long
Dim ujd As USN_JOURNAL_DATA
aHandle = CreateFile("\\.\I:", 0&, FILE_SHARE_READ Or FILE_SHARE_WRITE, 0&, OPEN_EXISTING, 0&, 0&)
anObject = DeviceIoControl(aHandle, FSCTL_QUERY_USN_JOURNAL, 0, 0, ujd, Marshal.SizeOf(ujd), byteBack, 0)
If anObject = 0 Then
MsgBox(Marshal.GetLastWin32Error.ToString & Marshal.GetHRForLastWin32Error.ToString)
Else
MsgBox(ujd.UsnJournalID.ToString)
MsgBox(ujd.UsnJournalID)
MsgBox(ujd.FirstUsn)
MsgBox(ujd.NextUsn)
MsgBox(ujd.MaxUsn)
MsgBox(ujd.LowestValidUsn)
MsgBox(ujd.AllocationDelta)
MsgBox(ujd.MaximumSize)
End If
End Sub
Public Function CTL_CODE(ByVal DeviceType As Integer, ByVal Func As Integer, ByVal Method As Integer, ByVal Access As Integer) As Long
Return (DeviceType << 16) Or (Access << 14) Or (Func << 2) Or Method
End Function
Private Const FILE_SHARE_READ = &H1
Private Const FILE_SHARE_WRITE = &H2
Private Const OPEN_EXISTING = 3
Private Const METHOD_BUFFERED = &H0
Private Const FILE_DEVICE_FILE_SYSTEM = &H9
Private Const FILE_ANY_ACCESS = &H0
Private Const FILE_SPECIAL_ACCESS = FILE_ANY_ACCESS
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> Public Structure USN_JOURNAL_DATA
Public UsnJournalID As Long
Public FirstUsn As Int64
Public NextUsn As Int64
Public LowestValidUsn As Int64
Public MaxUsn As Int64
Public MaximumSize As Long
Public AllocationDelta As Long
End Structure
Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal DesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Integer, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Declare Auto Function DeviceIoControl Lib "kernel32.dll" (ByVal hDevice As Long, ByVal dwIoControlCode As Integer, ByVal lpInBuffer As Integer, ByVal nInBufferSize As Integer, <MarshalAs(UnmanagedType.Struct)> ByRef lpOutBuffer As USN_JOURNAL_DATA, ByVal nOutBufferSize As Integer, ByRef lpBytesReturned As Integer, ByVal lpOverlapped As Integer) As Long
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim FSCTL_QUERY_USN_JOURNAL As Long = CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 61, METHOD_BUFFERED, FILE_SPECIAL_ACCESS)
Dim aHandle As Long
Dim anObject As Long
Dim byteBack As Long
Dim ujd As USN_JOURNAL_DATA
aHandle = CreateFile("\\.\I:", 0&, FILE_SHARE_READ Or FILE_SHARE_WRITE, 0&, OPEN_EXISTING, 0&, 0&)
anObject = DeviceIoControl(aHandle, FSCTL_QUERY_USN_JOURNAL, 0, 0, ujd, Marshal.SizeOf(ujd), byteBack, 0)
If anObject = 0 Then
MsgBox(Marshal.GetLastWin32Error.ToString & Marshal.GetHRForLastWin32Error.ToString)
Else
MsgBox(ujd.UsnJournalID.ToString)
MsgBox(ujd.UsnJournalID)
MsgBox(ujd.FirstUsn)
MsgBox(ujd.NextUsn)
MsgBox(ujd.MaxUsn)
MsgBox(ujd.LowestValidUsn)
MsgBox(ujd.AllocationDelta)
MsgBox(ujd.MaximumSize)
End If
End Sub
Public Function CTL_CODE(ByVal DeviceType As Integer, ByVal Func As Integer, ByVal Method As Integer, ByVal Access As Integer) As Long
Return (DeviceType << 16) Or (Access << 14) Or (Func << 2) Or Method
End Function