Jump to content
Sign in to follow this  

_WinAPI_SetFilePointer(), _WinAPI_SetEndOfFile()

Recommended Posts


I have sent proposition for this new UDF to Gary today.

So here it is.


#include <WinAPI.au3>

Global $sFile, $hFile, $sText, $nBytes, $tBuffer

; 1) create file and write data to it
$sFile = @ScriptDir & '\test.txt'
$sText = 'abcdefghijklmnopqrstuvwxyz'
$tBuffer = DLLStructCreate("byte[" & StringLen($sText) & "]")
DLLStructSetData($tBuffer, 1, $sText)
$hFile = _WinAPI_CreateFile($sFile, 1)
_WinAPI_WriteFile($hFile, DLLStructGetPtr($tBuffer), StringLen($sText), $nBytes)
ConsoleWrite('1) ' & FileRead($sFile) & @LF)

; 2) read 6 bytes from posision 3
$tBuffer = DLLStructCreate("byte[6]")
$hFile = _WinAPI_CreateFile($sFile, 2, 2)
_WinAPI_SetFilePointer($hFile, 3)
_WinAPI_ReadFile($hFile, DLLStructGetPtr($tBuffer), 6, $nBytes)
$sText = BinaryToString(DLLStructGetData($tBuffer, 1))
ConsoleWrite('2) ' & $sText & @LF)

; 3) write previously read 6 bytes from posision 3 to the same position but in UpperCase
DLLStructSetData($tBuffer, 1, StringUpper($sText))
$hFile = _WinAPI_CreateFile($sFile, 2, 4)
_WinAPI_SetFilePointer($hFile, 3)
_WinAPI_WriteFile($hFile, DLLStructGetPtr($tBuffer), 6, $nBytes)
$tBuffer = 0
ConsoleWrite('3) ' & FileRead($sFile) & @LF)

; 4) truncate file size to 12 bytes
$hFile = _WinAPI_CreateFile($sFile, 2, 4)
_WinAPI_SetFilePointer($hFile, 12)
ConsoleWrite('4) ' & FileRead($sFile) & @LF)

All packed in one file as attachment:

EDIT: This is included in AutoIt from version

EDIT2: Removed UDF code from post because now it's in AutoIt standard incudes


Edited by Zedna

Share this post

Link to post
Share on other sites

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
Sign in to follow this  


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.