GetFileSizeEx

Aus API-Wiki
Zur Navigation springenZur Suche springen


GetFileSizeEx ermittelt die Größe einer Datei, und unterstützt auch Dateien größer als 4 GB.

Declare Function GetFileSizeEx Lib "kernel32.dll" ( _
                 ByVal hFile As Long, _
                 lpFileSize As LARGE_INTEGER _
                 ) As Long


Parameter

hFile

Handle zu einer mit CreateFile geöffneten Datei.

lpFileSize

Pointer auf eine LARGE_INTEGER Struktur, die die Größe der Datei erhält.


Rückgabe

Glückt GetFileSizeEx, gibt es einen Wert ungleich 0 zurück. Andernfalls können genauere Informationen mit GetLastError angefordert werden.


Beispiel

Option Explicit

Private Declare Function CreateFile Lib "kernel32.dll" _
                         Alias "CreateFileA" ( _
                         ByVal lpFileName As String, _
                         ByVal dwDesiredAccess As Long, _
                         ByVal dwShareMode As Long, _
                         lpSecurityAttributes As Any, _
                         ByVal dwCreationDisposition As Long, _
                         ByVal dwFlagsAndAttributes As Long, _
                         ByVal hTemplateFile As Long _
                         ) As Long

Private Declare Function GetFileSizeEx Lib "kernel32.dll" ( _
                         ByVal hFile As Long, _
                         ByRef lpFileSize As LARGE_INTEGER _
                         ) As Long

Private Declare Function CloseHandle Lib "kernel32.dll" ( _
                         ByVal hObject As Long _
                         ) As Long

Private Type LARGE_INTEGER
    LoPart      As Long
    HiPart      As Long
End Type

Private Const OPEN_EXISTING = 3
Private Const FILE_SHARE_READ = &H1
Private Const FILE_SHARE_WRITE = &H2
Private Const GENERIC_READ = &H80000000

Private Sub Form_Load()
    Dim hFile       As Long
    Dim udtSize     As LARGE_INTEGER
    Dim varSize     As Variant
    
    hFile = CreateFile("D:\IsoFile.iso", GENERIC_READ, FILE_SHARE_READ Or FILE_SHARE_WRITE, _
                       ByVal 0&, OPEN_EXISTING, 0, 0)
                       
    If hFile <> -1 Then
        If GetFileSizeEx(hFile, udtSize) Then
            varSize = To64BitInt(udtSize.LoPart, udtSize.HiPart, False)
            Debug.Print "Größe: " & (varSize / (1000# * 1000#)) & " MB"
        Else
            Debug.Print "GetFileSizeEx fehlgeschlagen. Err: " & Err.LastDllError
        End If
        
        CloseHandle hFile
    Else
        Debug.Print "CreateFile fehlgeschlagen. Err: " & Err.LastDllError
    End If
End Sub

Private Function To64BitInt( _
    ByVal lngLo As Long, _
    ByVal lngHi As Long, _
    Optional ByVal signed As Boolean = True _
) As Variant

    Dim value   As Variant

    If (lngLo And &H80000000) Then
        value = CDec(lngLo And &H7FFFFFFF) + CDec(2147483648#)
    Else
        value = CDec(lngLo)
    End If
    
    If (Not signed) And (lngHi And &H80000000) Then
        lngHi = lngHi And &H7FFFFFFF
        value = value + (CDec(lngHi) + CDec(2147483648#)) * CDec(4294967296#)
    Else
        value = value + CDec(lngHi) * CDec(4294967296#)
    End If
    
    To64BitInt = value
End Function


Betriebssystem

  • Windows 2000 / XP
  • Windows Vista


Verweise

MSDN US-Libary - GetFileSizeEx



Diese Deklaration wurde von Arne Elster erstellt. Sollten Sie Fragen zur Verwendung der entsprechenden Funktion haben, so verwenden Sie hierfür bitte das Visual Basic Forum, die Artikeldiskussionsseite oder die Nutzerdiskussionsseite.