uses "Console"
uses "File"
uses "UI"
dim myTime as DWord = Timer
#Region "Declarations"
%INVALID_HANDLE_VALUE = &HFFFFFFFF as dword
'[] [REF] https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-samr/262970b7-cd4a-41f4-8c4d-5a27f0092aaa
%GENERIC_READ = 0x80000000 as dword ' Specifies access control suitable for reading the object.
%GENERIC_WRITE = 0x40000000 as dword ' Specifies access control suitable for updating attributes on the object.
'[] [REF] https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilew
%ERROR_FILE_NOT_FOUND = 2
%FILE_SHARE_DELETE = 0x00000004 as dword ' Enables subsequent open operations on a file or device to request delete access. Otherwise, other processes cannot open the file or device if they request delete access.
%FILE_SHARE_READ = 0x00000001 as dword ' Enables subsequent open operations on a file or device to request read access. Otherwise, other processes cannot open the file or device if they request read access.
%FILE_SHARE_WRITE = 0x00000002 as dword ' Enables subsequent open operations on a file or device to request write access. Otherwise, other processes cannot open the file or device if they request write access.
%OPEN_EXISTING = 3 as dword ' Opens a file or device, only if it exists.
%FILE_FLAG_SEQUENTIAL_SCAN = 0x08000000 as dword ' Access is intended to be sequential from beginning to end.
%FILE_ATTRIBUTE_SYSTEM = 4 as dword ' The file is part of or used exclusively by an operating system.
%FILE_FLAG_OVERLAPPED = 0x40000000 as DWord ' The file or device is being opened or created for asynchronous I/O. When subsequent I/O operations are completed on this handle, the event specified in the OVERLAPPED structure will be set to the signaled state.
%FILE_FLAG_NO_BUFFERING = 0x20000000 as DWord 'The file or device is being opened with no system caching for data reads and writes. This flag does not affect hard disk caching or memory mapped files.
'[] [REF] https://docs.microsoft.com/fr-fr/windows/win32/api/minwinbase/ns-minwinbase-overlapped
Type overlapped
Internal as quad ' The status code for the I/O request. When the request is issued, the system sets this member to STATUS_PENDING to indicate that the operation has not yet started. When the request is completed, the system sets this member to the status code for the completed request.
InternalHigh as quad ' The number of bytes transferred for the I/O request. The system sets this member if the request is completed without errors.
Offset as DWord ' The low-order portion of the file position at which to start the I/O request, as specified by the user. This member is nonzero only when performing I/O requests on a seeking device that supports the concept of an offset (also referred to as a file pointer mechanism), such as a file. Otherwise, this member must be zero.
OffsetHigh as DWord ' The high-order portion of the file position at which to start the I/O request, as specified by the user. This member is nonzero only when performing I/O requests on a seeking device that supports the concept of an offset (also referred to as a file pointer mechanism), such as a file. Otherwise, this member must be zero.
Pointer as Long' Reserved for system use; do not use after initialization to zero.
hEvent as long' A handle to the event that will be set to a signaled state by the system when the operation has completed.
end type
'[] [REF] https://docs.microsoft.com/en-us/previous-versions/windows/desktop/legacy/aa379560(v%3Dvs.85)
TYPE SECURITY_ATTRIBUTES DWORD
nLength AS DWORD
lpSecurityDescriptor AS DWORD
bInheritHandle AS LONG
END TYPE
'[] [REF] https://docs.microsoft.com/fr-fr/windows/win32/api/fileapi/nf-fileapi-readfileex
Declare Function ReadFileEx lib "KERNEL32.DLL" Alias "ReadFileEx" (
ByVal hFile As Long, ' A handle to the file or I/O device
byref lpBuffer As string , ' A pointer to a buffer that receives the data read from the file or device.
ByVal nNumberOfBytesToRead As Long, ' The number of bytes to be read.
byref lpOverlapped As overlapped, ' A pointer to an OVERLAPPED data structure that supplies data to be used during the asynchronous (overlapped) file read operation.
ByVal lpCompletionRoutine As long ' A pointer to the completion routine to be called when the read operation is complete and the calling thread is in an alertable wait state.
) As Long
'[] [REF] https://docs.microsoft.com/fr-fr/windows/win32/api/fileapi/nf-fileapi-createfilea
DECLARE FUNCTION CreateFileA lib "KERNEL32.DLL" ALIAS "CreateFileA" (
byref lpFileName AS ASCIIZ, ' The name of the file or device to be created or opened.
byval dwDesiredAccess AS DWORD, ' The requested access to the file or device, which can be summarized as read, write, both or neither zero).
byval dwShareMode AS DWORD, ' The requested sharing mode of the file or device, which can be read, write, both, delete, all of these, or none
byref lpSecurityAttributes AS SECURITY_ATTRIBUTES, ' A pointer to a SECURITY_ATTRIBUTES structure. This parameter can be NULL.
byval dwCreationDisposition AS DWORD, ' An action to take on a file or device that exists or does not exist.
byval dwFlagsAndAttributes AS DWORD, ' The file or device attributes and flags, FILE_ATTRIBUTE_NORMAL being the most common default value for files.
byval hTemplateFile AS DWORD ' A valid handle to a template file with the GENERIC_READ access right. This parameter can be NULL.
) AS DWORD ' If the function succeeds, the return value is an open handle to the specified file, device, named pipe, or mail slot.
'[] [REF] https://docs.microsoft.com/fr-fr/windows/win32/api/handleapi/nf-handleapi-closehandle
Declare Function CloseHandle lib "KERNEL32.DLL" Alias "CloseHandle" (byval hObject as DWORD) AS LONG
'[] [REF] https://docs.microsoft.com/fr-fr/windows/win32/api/synchapi/nf-synchapi-sleepex
declare function SleepEx lib "KERNEL32.DLL" Alias "SleepEx" (
byval dwMilliseconds as dword,
byval bAlertable as long
)as Long
#EndRegion
'[] [REF] https://docs.microsoft.com/fr-fr/windows/win32/api/minwinbase/nc-minwinbase-lpoverlapped_completion_routine
function LpoverlappedCompletionRoutine(
byval dwErrorCode as DWord, ' The I/O completion status. This parameter can be one of the system error codes.
Byval dwNumberOfBytesTransfered as DWORD, ' The number of bytes transferred. If an error occurs, this parameter is zero.
byval lpOverlapped as long ' A pointer to the OVERLAPPED structure specified by the asynchronous I/O function.
) as Long
printl "inside LpoverlappedCompletionRoutine"
printl "errCode = " & dwErrorCode
printl "Bytes read = " & dwNumberOfBytesTransfered
CONTROL SET TEXT hDlg, %label2, lpBuffer
return 0
end Function
Begin ControlID
%Label1
%Label2
%Button1
End ControlID
'
' DIALOG SHOW MODELESS hDlg Call DlgCallback
'
' Do
'
' DIALOG DOEVENTS 0 To Count
'
' if not EOF
' if not cancelled
' doReadNextBlock
'
' Loop While Count
'
CALLBACK Function DlgCallback() As Long
Select Case CBMSG
Case %WM_Command
If CBCTLMSG = %BN_CLICKED Then
SELECT CASE cbctl
case %Button1
call start_read
end Select
endif
Case %WM_DESTROY
MSGBOX 0, "Window is to be destroyed."
End Select
End Function
function start_read()
Global voverlapped as overlapped
Global hFile as Long ' handle to file to be read
Global lpBuffer as string * 512 ' cluster size
Local lpSecurityAttributes as SECURITY_ATTRIBUTES
Local result as long
Local success as long
hFile = CreateFileA(sFileSource, %GENERIC_READ, %FILE_SHARE_READ, lpSecurityAttributes, %OPEN_EXISTING, %FILE_FLAG_NO_BUFFERING or %FILE_FLAG_OVERLAPPED, Null)
if hFile <> %INVALID_HANDLE_VALUE then
Printl "File handle = " & hFile
result = ReadFileEx(hFile, lpBuffer, 512, voverlapped, CodePtr(LpoverlappedCompletionRoutine))
readInProgress = %TRUE
printl "ReadFileEx result (<> 0 means no error) : " & result
Else
Printl "Failed to get a file handle." in %CONSOLE_FOREGROUND_RED or %CONSOLE_FOREGROUND_INTENSITY
success = %FALSE
endif
if success = %FALSE then
Return %FALSE
Else
Return %TRUE
endif
end Function
Function TBMain() as Long
Local count as Integer
Global hDlg as DWord ' Dialog holder
Global sFileSource as asciiz ' Full path to file to be read
Global fileSize as DWord
Global readInProgress as Boolean
sFileSource = "t:\pg200.txt" ' File to read
fileSize = FILE_Size(sFileSource)
Printl "File : " & sFileSource
Printl "Size (Bytes) : " & fileSize
call MakeUi
Do
DIALOG DOEVENTS 0 To Count
SleepEx(1,%TRUE)
updateLabel1
Loop While Count
closeHandle(hFile)
end function
Function updateLabel1()
Local sLabel1 as String
Local nTime as DWord
nTime = Timer - myTime
sLabel1 = "Script is running since : " & nTime & " seconds"
CONTROL SET TEXT hDlg, %label1, sLabel1
end function
Function MakeUi()
DIALOG NEW 0, "form1", -1, -1, 195, 180, %WS_DLGFRAME OR %DS_CENTER OR %WS_CAPTION OR %WS_SYSMENU, 0 TO hDlg
CONTROL ADD Label , hDlg, %label1, "...", 5, 15, 185, 15, %WS_BORDER OR %ES_RIGHT, %WS_EX_CLIENTEDGE
CONTROL ADD Label , hDlg, %label2, "...", 5, 35, 185, 15, %WS_BORDER OR %ES_RIGHT, %WS_EX_CLIENTEDGE
CONTROL ADD BUTTON , hDlg, %button1,"Start READ", 65, 80, 45, 20
DIALOG SHOW MODELESS hDlg Call DlgCallback
end function
Have still to find how to prog read_next_chunk function.
Bookmarks