File Standard Library
Also read a (binary) file on the local HDD and write to the memory/variable.
PS. Although there is a function InternetWriteFile(), it's not enough to upload a local file to the Web. So, please don't try it.
DOWNLOAD FileHelper.ahk or StandardLibrary File.ahk.
PS. I'll remove the actual address tomorrow.
#Include FileHelper.ahk hInet := InternetOpen() hUrl := InternetOpenUrl(hInet, "ftp://ftp.xxx.com/install.exe", 0x08000000) ; INTERNET_FLAG_PASSIVE hFile := CreateFile("C:\install.exe", 1) If (hUrl = -1) || (hFile = -1) ExitApp VarSetCapacity(buffer, 1024) Loop { If !nSize := InternetReadFile(hUrl, &buffer, 1024) Break WriteFile(hFile, &buffer, nSize) Sleep, 10 } CloseHandle(hFile) InternetCloseHandle(hUrl) InternetCloseHandle(hInet)
Well, I am not too fond of the choice of the first two names: why ReadMemory writes to a file? It seems a bit strange. WriteMemory indeed put data in memory, but the name doesn't tell it comes from a file.
Although it is unlikely to change, I am not fond of using boolean * 2 to create 0 or 2.
I am not sure that the SetEndOfFile is necessary in ReadMemory, but I admit I didn't knew the function so I am happy to discover it, it can be useful to truncate or extend a file (along with SetFileValidData).
In WriteMemory, the VarSetCapacity(sBuffer, -1) is useful if indeed the data read is textual, but isn't there a risk if it is binary? Mmm, probably not, if the buffer isn't shrunk.
That's lot of criticism, but your code is of course very useful, thanks for sharing.
Yes, I usually don't like to be bothered by the error messages.You like to go straight to the heart of stuff, unencumbered by most error checking...
So, I add the error checking only when creating the script, then after some tests I remove them.
If there are complaints to have a difficulty to debug, of course I can put them back.
I admit it's not intuitive at first usage.Well, I am not too fond of the choice of the first two names: why ReadMemory writes to a file? It seems a bit strange. WriteMemory indeed put data in memory, but the name doesn't tell it comes from a file.
However, they are created and mostly used as the counter part of ReadFile/WriteFile.
So, it became rather cumbersome after some usages, was so at least to me, to use different names, like ReadFromMemory, WriteToMemory, or ReadMemoryToFile, WriteMemoryFromFile, etc.
Yes, I realized that after post. It also does not accord with the usage of bFolder where I used "? : ".Although it is unlikely to change, I am not fond of using boolean * 2 to create 0 or 2.
I'll change it when I update the script.
As a matter of fact, it's only needed when overwriting a file.I am not sure that the SetEndOfFile is necessary in ReadMemory, but I admit I didn't knew the function so I am happy to discover it, it can be useful to truncate or extend a file (along with SetFileValidData).
If the original size were larger than the overwritten one, there will remain garbage at the end of the file, without it.
BTW, I never felt the need of SetFileValidData personally, so I didn't add it.
I think the buffer won't shrink according to the documentation.In WriteMemory, the VarSetCapacity(sBuffer, -1) is useful if indeed the data read is textual, but isn't there a risk if it is binary? Mmm, probably not, if the buffer isn't shrunk.
And, you're right it's only for the textual contents.
Thanks.That's lot of criticism, but your code is of course very useful, thanks for sharing.
I sometimes put error handling sometimes don't depending on mood and free time, but I always know its a bad thing.Yes, I usually don't like to be bothered by the error messages.
So, I add the error checking only when creating the script, then after some tests I remove them.
If there are complaints to have a difficulty to debug, of course I can put them back.
Particularly for scripts handling files: a file might not be writable, for example (or its name isn't legal, etc.).I sometimes put error handling sometimes don't depending on mood and free time, but I always know its a bad thing.
I suppose it is OK for Sean scripts made for himself, and I understand he just gave the work he uses internally, but it might not be suitable as general purpose functions to release to public, like a standard library.
This is just a warning to those wanting to use them. :-)
So, you don't do it just because of mood even though you know not doing is bad?I sometimes put error handling sometimes don't depending on mood and free time, but I always know its a bad thing.
Are you a kid?
Actually, error checking is quite easy for the file case.Particularly for scripts handling files: a file might not be writable, for example (or its name isn't legal, etc.).
I suppose it is OK for Sean scripts made for himself, and I understand he just gave the work he uses internally, but it might not be suitable as general purpose functions to release to public, like a standard library.
This is just a warning to those wanting to use them. :-)
Just check the returned hFile. So, a user can add the check when writing the script.
I added this check in WriteMemory() as a user can't check it himself in this case.
In ReadMemory() case, no file will be created anyway if there is an error which would be much rare than the case of WriteMemory().
Yes.So, you don't do it just because of mood even though you know not doing is bad?
Are you a kid?
A human kid.
Sad to know you have grown up. My deepest symphaties. :cry:
A human kid.
And all this time I thought you were a cat or a cookie monster. Thanks for clearing that up.
I wish I could be a cat, to see in the dark, to feel things, to be fed by negative energy. But as you are not a kid anymore, I beleive this story sounds not very interesting to you....
It can be downloaded from the first post in this thread.