I already have written a code using which I can send files over internet using a Winsock control, but there is one problem, my code can't read and write files bigger than 2GB and files which use Unicode characters in their names and paths. So now I want to write a new piece of code using wich I could read and write such type of files. I have read that I can do that using these API calls: CreateFile, SetFilePointer, ReadFile, WriteFile. I have written a code to test if it works using these API calls, but seems that there is a problem. ReadFile function each time reads 1024 bytes of file data to byte array and then this array is sent to WriteFile function. At first everything works fine but after some time program just stops working without any error table. Just like that:
![Name: Untitled.png
Views: 74
Size: 33.1 KB]()
I think that something happens with ReadFile function and I also have added counter to count after how many laps program stops to work and program always stops to work after 3029 laps. For testing I use JPG file in size of 4,94MB after 3029 laps program stops to work and copy of JPG file is only 2,95MB. (Here is calculation: 3029 laps * 1024 bytes per lap = 3101696 bytes (or 2,95MB)). In simple words, after 3029 times ReadFile function is called, program just stops to work.
Here is my code which I use for a testing purposes:
Please, maybe someone could help me to solve out this problem. Thank you in advance!
I think that something happens with ReadFile function and I also have added counter to count after how many laps program stops to work and program always stops to work after 3029 laps. For testing I use JPG file in size of 4,94MB after 3029 laps program stops to work and copy of JPG file is only 2,95MB. (Here is calculation: 3029 laps * 1024 bytes per lap = 3101696 bytes (or 2,95MB)). In simple words, after 3029 times ReadFile function is called, program just stops to work.
Here is my code which I use for a testing purposes:
Code:
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 SetFilePointer Lib "kernel32" (ByVal hFile As Long, ByVal lDistanceToMove As Long, lpDistanceToMoveHigh As Long, ByVal dwMoveMethod As Long) As Long
Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, ByVal lpOverlapped As Long) As Long
Private Declare Function WriteFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, ByVal lpOverlapped As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Const GENERIC_WRITE = &H40000000
Private Const GENERIC_READ = &H80000000
Private Const OPEN_ALWAYS = 4
Private Const FILE_ATTRIBUTE_NORMAL = &H80
Dim InputFile As String
Dim OutputFile As String
Dim InputFileSize As Long
Dim HandleA As Long
Dim HandleB As Long
Dim BytesArr(1023) As Byte
Dim i As Long
Private Sub Form_Load()
InputFile = "C:\Users\Unnamed\Desktop\1.jpg"
OutputFile = "C:\Users\Unnamed\Desktop\2.jpg"
HandleA = CreateFile(InputFile, GENERIC_WRITE Or GENERIC_READ, 0, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0)
InputFileSize = FileLen(InputFile)
HandleB = CreateFile(OutputFile, GENERIC_WRITE Or GENERIC_READ, 0, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0)
End Sub
Private Sub Command1_Click()
Call ReadFromFile
End Sub
Private Function ReadFromFile()
Call SetFilePointer(HandleA, 0, ByVal 0, 1)
Call ReadFile(HandleA, BytesArr(0), 1024, 0, 0)
Text1.Text = Val(Val(Text1.Text) + 1) '<-- Counter to test after how many laps program crashes. In my case it always crash after 3029 laps. :(
Me.Refresh
Call WriteToFile
End Function
Private Function WriteToFile()
Call WriteFile(HandleB, BytesArr(0), 1024, 0, 0)
i = i + 1024
If i >= InputFileSize Then
Call CloseHandle(HandleA)
Call CloseHandle(HandleB)
MsgBox "Done!"
End
Else
Call ReadFromFile
End If
End Function