Jump to content

Recommended Posts

Posted (edited)

Looking for a way to pre-allocate space in your WinAPI created file, don't wanna write a bunch of zeros?

SetFileInformationByHandle looks promising for Vista+ but I never did get it working 

I did however find 'NtSetInformationFile' - Available starting with Windows 2000

;hfile - handle to file opened with $GENERIC_WRITE access
;$llAllocationBytes - 64 bit int size to allocate for file
__WinAPI_SetFileAllocationInfo($hFile, $llAllocationBytes)
Func __WinAPI_SetFileAllocationInfo($hFile, $llAllocationBytes)
    Local Const $eFileAllocationInformation = 0x13
    Local $t_FILE_ALLOCATION_INFO = DllStructCreate("Align 8;INT64")
    DllStructSetData($t_FILE_ALLOCATION_INFO, 1, $llAllocationBytes)
    Local $iRet = DllCall("ntdll.dll", "int", "NtSetInformationFile", _
            "handle", $hFile, _
            "int64*", 0, _
            "ptr", DllStructGetPtr($t_FILE_ALLOCATION_INFO), _
            "ulong", DllStructGetSize($t_FILE_ALLOCATION_INFO), _
            "int", $eFileAllocationInformation)
    If @error Then Return False
    Return $iRet[0] = 0
EndFunc   ;==>__WinAPI_SetFileAllocationInfo

In case you are wondering (wandering?) here is my failed Attempt to call SetFileInformationByHandle

Spoiler
#cs
Func __WinAPI_SetFileAllocationInfo($hFile, $llAllocationBytes)
    Local Const $eFileAllocationInfo = 0x5
    Local $t_FILE_ALLOCATION_INFO = DllStructCreate("INT64;")
    DllStructSetData($t_FILE_ALLOCATION_INFO, 1, $llAllocationBytes)
    Local $iRet = DllCall("Kernel32.dll", "int", "SetFileInformationByHandle", "handle", $hFile, _
            "int", $eFileAllocationInfo, _
            "ptr", DllStructGetPtr($t_FILE_ALLOCATION_INFO), _
            "dword", DllStructGetSize($t_FILE_ALLOCATION_INFO))
    If @error Then Return False
    Return $iRet[0] = 0
EndFunc
#ce

 

 

more info: https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/ntifs/ns-ntifs-_file_allocation_information

Edited by Bilgus

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...