 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
olfen
Joined: 04 Jun 2005 Posts: 99 Location: Stuttgart, Germany
|
Posted: Sat Jun 10, 2006 6:57 pm Post subject: DllCall: Basic FTP Functions |
|
|
Not heavily tested. Feel free to revise and add more.
I think the call to InternetOpenA needs refinement regarding the proxy settings. I don't have the knowledge to do so.
| Code: |
/*
http://msdn.microsoft.com/library/en-us/wininet/wininet/ftp_sessions.asp
http://msdn.microsoft.com/library/en-us/wininet/wininet/internetopen.asp
http://msdn.microsoft.com/library/en-us/wininet/wininet/internetconnect.asp
*/
FtpCreateDirectory(DirName) {
global ic_hInternet
r := DllCall("wininet\FtpCreateDirectoryA", "uint", ic_hInternet, "str", DirName)
If (ErrorLevel != 0 or r = 0)
return 0
else
return 1
}
FtpRemoveDirectory(DirName) {
global ic_hInternet
r := DllCall("wininet\FtpRemoveDirectoryA", "uint", ic_hInternet, "str", DirName)
If (ErrorLevel != 0 or r = 0)
return 0
else
return 1
}
FtpSetCurrentDirectory(DirName) {
global ic_hInternet
r := DllCall("wininet\FtpSetCurrentDirectoryA", "uint", ic_hInternet, "str", DirName)
If (ErrorLevel != 0 or r = 0)
return 0
else
return 1
}
FtpPutFile(LocalFile, NewRemoteFile="", Flags=0) {
;Flags:
;FTP_TRANSFER_TYPE_UNKNOWN = 0 (Defaults to FTP_TRANSFER_TYPE_BINARY)
;FTP_TRANSFER_TYPE_ASCII = 1
;FTP_TRANSFER_TYPE_BINARY = 2
If NewRemoteFile=
NewRemoteFile := LocalFile
global ic_hInternet
r := DllCall("wininet\FtpPutFileA"
, "uint", ic_hInternet
, "str", LocalFile
, "str", NewRemoteFile
, "uint", Flags
, "uint", 0) ;dwContext
If (ErrorLevel != 0 or r = 0)
return 0
else
return 1
}
FtpGetFile(RemoteFile, NewFile="", Flags=0) {
;Flags:
;FTP_TRANSFER_TYPE_UNKNOWN = 0 (Defaults to FTP_TRANSFER_TYPE_BINARY)
;FTP_TRANSFER_TYPE_ASCII = 1
;FTP_TRANSFER_TYPE_BINARY = 2
If NewFile=
NewFile := RemoteFile
global ic_hInternet
r := DllCall("wininet\FtpGetFileA"
, "uint", ic_hInternet
, "str", RemoteFile
, "str", NewFile
, "int", 1 ;do not overwrite existing files
, "uint", 0 ;dwFlagsAndAttributes
, "uint", Flags
, "uint", 0) ;dwContext
If (ErrorLevel != 0 or r = 0)
return 0
else
return 1
}
FtpGetFileSize(FileName, Flags=0) {
;Flags:
;FTP_TRANSFER_TYPE_UNKNOWN = 0 (Defaults to FTP_TRANSFER_TYPE_BINARY)
;FTP_TRANSFER_TYPE_ASCII = 1
;FTP_TRANSFER_TYPE_BINARY = 2
global ic_hInternet
fof_hInternet := DllCall("wininet\FtpOpenFileA"
, "uint", ic_hInternet
, "str", FileName
, "uint", 0x80000000 ;dwAccess: GENERIC_READ
, "uint", Flags
, "uint", 0) ;dwContext
If (ErrorLevel != 0 or fof_hInternet = 0)
return -1
FileSize := DllCall("wininet\FtpGetFileSize", "uint", fof_hInternet, "uint", 0)
DllCall("wininet\InternetCloseHandle", "UInt", fof_hInternet)
return, FileSize
}
FtpDeleteFile(FileName) {
global ic_hInternet
r := DllCall("wininet\FtpDeleteFileA", "uint", ic_hInternet, "str", FileName)
If (ErrorLevel != 0 or r = 0)
return 0
else
return 1
}
FtpRenameFile(Existing, New) {
global ic_hInternet
r := DllCall("wininet\FtpRenameFileA", "uint", ic_hInternet, "str", Existing, "str", New)
If (ErrorLevel != 0 or r = 0)
return 0
else
return 1
}
FtpOpen(Server, Port=21, Username=0, Password=0 ,Proxy="", ProxyBypass="") {
IfEqual, Username, 0, SetEnv, Username, anonymous
IfEqual, Password, 0, SetEnv, Password, anonymous
If (Proxy != "")
AccessType=3
Else
AccessType=1
;#define INTERNET_OPEN_TYPE_PRECONFIG 0 // use registry configuration
;#define INTERNET_OPEN_TYPE_DIRECT 1 // direct to net
;#define INTERNET_OPEN_TYPE_PROXY 3 // via named proxy
;#define INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY 4 // prevent using java/script/INS
global ic_hInternet, io_hInternet, hModule
hModule := DllCall("LoadLibrary", "str", "wininet.dll")
io_hInternet := DllCall("wininet\InternetOpenA"
, "str", A_ScriptName ;lpszAgent
, "UInt", AccessType
, "str", Proxy
, "str", ProxyBypass
, "UInt", 0) ;dwFlags
If (ErrorLevel != 0 or io_hInternet = 0) {
FtpClose()
return 0
}
ic_hInternet := DllCall("wininet\InternetConnectA"
, "uint", io_hInternet
, "str", Server
, "uint", Port
, "str", Username
, "str", Password
, "uint" , 1 ;dwService (INTERNET_SERVICE_FTP = 1)
, "uint", 0 ;dwFlags
, "uint", 0) ;dwContext
If (ErrorLevel != 0 or ic_hInternet = 0)
return 0
else
return 1
}
FtpClose() {
global ic_hInternet, io_hInternet, hModule
DllCall("wininet\InternetCloseHandle", "UInt", ic_hInternet)
DllCall("wininet\InternetCloseHandle", "UInt", io_hInternet)
DllCall("FreeLibrary", "UInt", hModule)
}
| Edit 20071020: Added global hModule in FtpOpen and FtpClose
Last edited by olfen on Sat Oct 20, 2007 2:29 pm; edited 1 time in total |
|
| Back to top |
|
 |
Chris Site Admin
Joined: 02 Mar 2004 Posts: 10464
|
Posted: Sat Jun 10, 2006 9:37 pm Post subject: |
|
|
For most purposes, this method seems superior to the FileAppend + Ftp method. Thanks for posting it.
Last edited by Chris on Tue Jun 13, 2006 3:18 am; edited 1 time in total |
|
| Back to top |
|
 |
corrupt
Joined: 29 Dec 2004 Posts: 2381
|
Posted: Sat Jun 10, 2006 11:21 pm Post subject: |
|
|
Thanks . Very handy. |
|
| Back to top |
|
 |
Thalon
Joined: 12 Jul 2005 Posts: 640
|
Posted: Sun Jun 11, 2006 9:04 am Post subject: |
|
|
For general use this is a great solution I was searching for!
But be shure not to use this with your ftp-settings in a freeware (or similar).
2 days ago I heard from a game that updates it's highscore via FTP.
A friend of me was asked to test the game from the author and sent him back servername, username and even password for full access!
The password was encrypted, but this is no problem even for low-level-debugger. (The game isn't online any more until finishing development ^^)
Thalon _________________ AHK-Icon-Changer
AHK-IRC
deutsches Forum
SacredVault |
|
| Back to top |
|
 |
Veovis
Joined: 13 Feb 2006 Posts: 390 Location: Utah
|
Posted: Sun Jun 11, 2006 10:19 pm Post subject: |
|
|
LOL _________________
"Power can be given overnight, but responsibility must be taught. Long years go into its making." |
|
| Back to top |
|
 |
olfen
Joined: 04 Jun 2005 Posts: 99 Location: Stuttgart, Germany
|
Posted: Mon Jun 12, 2006 5:33 pm Post subject: |
|
|
I fully agree with your security related objections, Thalon.
But even if the hardcoded access data were protected by means of encryption or even anti-debugging code (which is not an obstacle for some people), it still would be easy enough to sniff the relevant data using a network protocol analyzer. |
|
| Back to top |
|
 |
PhiLho
Joined: 27 Dec 2005 Posts: 6721 Location: France (near Paris)
|
Posted: Mon Jun 12, 2006 5:54 pm Post subject: |
|
|
I suppose that's why SFTP was invented... _________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2") |
|
| Back to top |
|
 |
olfen
Joined: 04 Jun 2005 Posts: 99 Location: Stuttgart, Germany
|
Posted: Mon Jun 12, 2006 6:06 pm Post subject: |
|
|
| PhiLho wrote: | | I suppose that's why SFTP was invented... |
Good guess
As for anti-debugging: This will detect OllyDbg and probably some others:
| Code: |
;http://msdn.microsoft.com/library/en-us/debug/base/isdebuggerpresent.asp
r := DllCall("IsDebuggerPresent")
IfEqual, r, 0, MsgBox No debugger present.
IfEqual, r, 1, MsgBox Debugger detected.
|
Edit: Google references tell me that many debuggers can be hidden from this function, so can OllyDbg  |
|
| Back to top |
|
 |
Ace_NoOne
Joined: 10 Oct 2005 Posts: 333 Location: Germany
|
Posted: Wed Dec 27, 2006 11:11 am Post subject: |
|
|
Great job, olfen; very useful!
I just noticed that a return value of 1 means success while 0 means failure.
ErrorLevel values are usually the other way round; any special reason for choosing to do it this way? _________________ Improving my world, one script at a time.
Join the AutoHotkey IRC channel: irc.freenode.net #autohotkey |
|
| Back to top |
|
 |
Elevator_Hazard
Joined: 28 Oct 2006 Posts: 303 Location: Illinois
|
Posted: Sun May 27, 2007 2:42 pm Post subject: |
|
|
Although this post is a little old, can someone show me an example usage that would work? Like no variables in the params so I know what the domain name should look like and such, because I always get confused about that. Of course you can omit the password, I just need to know how to work it. _________________ Changed siggy at request of ahklerner  |
|
| Back to top |
|
 |
Ace_NoOne
Joined: 10 Oct 2005 Posts: 333 Location: Germany
|
Posted: Sun May 27, 2007 8:17 pm Post subject: |
|
|
| Elevator_Hazard wrote: | | can someone show me an example usage that would work? | Sure; below are three essential (though very simple) routines, pulled from my FtpUploader script, which makes heavy use of these functions. I've also included some sample values for the variables used; that should give you everything you need to get started.
| Code: | ; sample values
FtpHost = autohotkey.net
FtpPort = 21
FtpUsername = AhkH4x0r
FtpPassword = Ahk1337
localFile = C:\FtpTest.txt
remoteFile = test/FtpTest.txt
; establish connection to FTP server
connect:
FtpConnection := FtpOpen(FtpHost, FtpPort, FtpUsername, FtpPassword)
If (FtpConnection != 1)
{
MsgBox, Error connecting to FTP server.
GoSub, quit
}
Return
; upload file
uploadFile:
; establish connection to FTP server
GoSub, connect
; store file on FTP server
fileTransfer := FtpPutFile(localFile, remoteFile)
If (fileTransfer != 1)
{
MsgBox, Error uploading file.
GoSub, quit
}
; close connection
FtpClose()
; confirmation message
MsgBox, Upload complete.
; terminate script
GoSub, quit
Return
; download file
downloadFile:
; establish connection to FTP server
GoSub, connect
; retrieve file from FTP server
fileTransfer := FtpGetFile(remoteFile, localFile)
If (fileTransfer != 1)
{
MsgBox, Error downloading file.
GoSub, quit
}
; close connection
FtpClose()
; confirmation message
MsgBox, Download complete.
; terminate script
GoSub, quit
Return |
HTH.
PS: That "Windows XP Scream" logo in your sig is awesome! (Though hotlinking it, that's evil... ) _________________ Improving my world, one script at a time.
Join the AutoHotkey IRC channel: irc.freenode.net #autohotkey |
|
| Back to top |
|
 |
Elevator_Hazard
Joined: 28 Oct 2006 Posts: 303 Location: Illinois
|
Posted: Mon May 28, 2007 3:30 pm Post subject: |
|
|
Oops I forgot to say thanks yesterday. Thanks It really helped. And I didn't want to discredit the people who used that picture that is my siggy
Again thanks. _________________ Changed siggy at request of ahklerner  |
|
| Back to top |
|
 |
Helpy Guest
|
Posted: Mon May 28, 2007 4:45 pm Post subject: |
|
|
| Elevator_Hazard wrote: | And I didn't want to discredit the people who used that picture that is my siggy  | I am not sure to understand this sentence.
Anyway, what Ace_NoOne meant is that you get the picture directly from apcmag.com site, which is stealing their bandwidth.
Even worse, this picture is quite heavy: 32KB, compare to size of most other avatars here. Sometime the loading of a topic is slow down because you posted there. Seriously (I am on broadband, not on dialup!). |
|
| Back to top |
|
 |
Elevator_Hazard
Joined: 28 Oct 2006 Posts: 303 Location: Illinois
|
Posted: Mon May 28, 2007 5:53 pm Post subject: |
|
|
Oopsies... I'll change it to .gif and put it on some site of mine. Thanks for bringing this up. _________________ Changed siggy at request of ahklerner  |
|
| Back to top |
|
 |
Helpy Guest
|
Posted: Mon May 28, 2007 10:06 pm Post subject: |
|
|
Aha, much better, thanks. I really can sense the difference in download time. It seems AHK isn't a very fast server (but good enough, mind you).
Sorry, olfen, for being off topic, although netiquette is an important aspect of the Web, so not far from FTP, somehow... |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|