NTFileSize


Author: Dave
Date: 12.01.20 - 6:06am



You will never need this.

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





Comments: (0)

 
Leave Comment:
Name:
Email: (not shown)
Message: (Required)
Math Question: 37 + 88 = ? followed by the letter: F 



About Me
More Blogs
Main Site
Posts: (All)
2023 ( 9 )
2022 ( 4 )
2021 ( 2 )
2020 (4)
     NTFileSize
     BSTR from C Dll to VB
     Cpp Memory Manipulation
     ActiveX Binary Compatability
2019 (5)
     Console tricks
     FireFox temp dir
     OCX License
     Extract substring
     VB6 Console Apps
2018 (6)
     VB6 UDTs
     VB6 Debugger View As Hex tooltips
     VB6 - C Share registry data
     VB6 Addin Missing Menus
     VB6 Class Init Params
     VB6 isIn function
2017 ( 6 )
2016 ( 22 )
2015 ( 15 )
2014 ( 25 )
2013 ( 4 )
2012 ( 10 )
2011 ( 7 )
2010 ( 11 )
2009 ( 3 )