Option Explicit
Private Type LARGE_INTEGER
low As Long
high As Long
End Type
Private Type FILE_STANDARD_INFORMATION
allocSize As LARGE_INTEGER
EndOfFile As LARGE_INTEGER
numberofLinks As Long
deletePending As Boolean 'should this be 2 bytes or 1, unused bigger is safer
directory As Boolean
End Type
'https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/ntifs/nf-ntifs-ntqueryinformationfile
'https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/wdm/ns-wdm-_io_status_block
Private Declare Function ZwQueryInformationFile Lib "ntdll" ( _
ByVal handle As Long, _
ByVal pioStatus As Long, _
ByVal pFileInfoStruct As Long, _
ByVal length As Long, _
ByVal InfoClass 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 CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Function h(x As Long) As String
h = Right("00000000" & Hex(x), 8)
End Function
Function NTFileSize(pth As String) As String
Dim hFile As Long, ret As Long, ioStatus As Currency '8 bytes
Dim stdInfo As FILE_STANDARD_INFORMATION
Const StdInfoClass = 5
Const FILE_SHARE_READ = &H1, OPEN_EXISTING = &H3, GENERIC_READ = &H80000000
hFile = CreateFile(pth, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0)
If hFile < 1 Then
Debug.Print "Failed to open handle to: " & pth
Exit Function
End If
ret = ZwQueryInformationFile(hFile, VarPtr(ioStatus), VarPtr(stdInfo), LenB(stdInfo), StdInfoClass)
CloseHandle hFile
If ret = 0 Then
NTFileSize = h(stdInfo.EndOfFile.high) & "`" & h(stdInfo.EndOfFile.low)
Else
Debug.Print "ZwQueryInformationFile Failed ret = " & ret
End If
End Function
Private Sub Form_Load()
On Error Resume Next
Dim pth As String, f As Long
pth = App.Path & "\test.txt"
Debug.Print "Target: " & pth
Kill pth
f = FreeFile
Open pth For Binary As f
Put f, , "test"
Close f
Debug.Print NTFileSize(pth)
Kill pth
End Sub