-
Notifications
You must be signed in to change notification settings - Fork 72
/
WinIniNet.ahk
348 lines (314 loc) · 10.8 KB
/
WinIniNet.ahk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
; ==========
; Origional FTP Functions by Olfen & Andreone
; See the following post:
; http://www.autohotkey.com/forum/viewtopic.php?t=10393
; ==========
; WININET_UrlGetContents() is based on heresy & DerRaphael's script here:
; http://www.autohotkey.com/forum/viewtopic.php?t=33506
; ==========
; by ahklerner
; ==========
WININET_Init(){
global
WININET_hModule := DllCall("LoadLibrary", "Str", "WinInet.Dll")
}
WININET_UnInit(){
global
DllCall("FreeLibrary", "UInt", WININET_hModule)
}
WININET_InternetOpenA(lpszAgent,dwAccessType=1,lpszProxyName=0,lpszProxyBypass=0,dwFlags=0){
;http://msdn.microsoft.com/en-us/library/aa385096(VS.85).aspx
return DllCall("WinINet\InternetOpenA"
, "Str" ,lpszAgent
, "UInt" ,dwAccessType
, "Str" ,lpszProxyName
, "Str" ,lpszProxyBypass
, "Uint" ,dwFlags )
}
WININET_InternetConnectA(hInternet,lpszServerName,nServerPort=80,lpszUsername=""
,lpszPassword="",dwService=3,dwFlags=0,dwContext=0){
;http://msdn.microsoft.com/en-us/library/aa384363(VS.85).aspx
; INTERNET_SERVICE_FTP = 1
; INTERNET_SERVICE_HTTP = 3
return DllCall("WinINet\InternetConnectA"
, "uInt" ,hInternet
, "Str" ,lpszServerName
, "Int" ,nServerPort
, "Str" ,lpszUsername
, "Str" ,lpszPassword
, "uInt" ,dwService
, "uInt" ,dwFlags
, "uInt*" ,dwContext )
}
WININET_HttpOpenRequestA(hConnect,lpszVerb,lpszObjectName,lpszVersion="HTTP/1.1"
,lpszReferer="",lplpszAcceptTypes="",dwFlags=0,dwContext=0){
;http://msdn.microsoft.com/en-us/library/aa384233(VS.85).aspx
return DllCall("WinINet\HttpOpenRequestA"
, "uInt" ,hConnect
, "Str" ,lpszVerb
, "Str" ,lpszObjectName
, "Str" ,lpszVersion
, "Str" ,lpszReferer
, "Str" ,lplpszAcceptTypes
, "uInt" ,dwFlags
, "uInt" ,dwContext )
}
WININET_HttpSendRequestA(hRequest,lpszHeaders="",lpOptional=""){
;http://msdn.microsoft.com/en-us/library/aa384247(VS.85).aspx
return DllCall("WinINet\HttpSendRequestA"
, "uInt" ,hRequest
, "Str" ,lpszHeaders
, "uInt" ,Strlen(lpszHeaders)
, "Str" ,lpOptional
, "uInt" ,Strlen(lpOptional) )
}
WININET_InternetReadFile(hFile){
;http://msdn.microsoft.com/en-us/library/aa385103(VS.85).aspx
dwNumberOfBytesToRead := 1024**2
VarSetCapacity(lpBuffer,dwNumberOfBytesToRead,0)
VarSetCapacity(lpdwNumberOfBytesRead,4,0)
Loop {
if DllCall("wininet\InternetReadFile","uInt",hFile,"uInt",&lpBuffer
,"uInt",dwNumberOfBytesToRead,"uInt",&lpdwNumberOfBytesRead ) {
VarSetCapacity(lpBuffer,-1)
TotalBytesRead := 0
Loop, 4
TotalBytesRead += *(&lpdwNumberOfBytesRead + A_Index-1) << 8*(A_Index-1)
If !TotalBytesRead
break
Else
Result .= SubStr(lpBuffer,1,TotalBytesRead)
}
}
return Result
}
WININET_FtpCreateDirectoryA(hConnect,lpszDirectory) {
;http://msdn.microsoft.com/en-us/library/aa384136(VS.85).aspx
r := DllCall("wininet\FtpCreateDirectoryA", "uint", hConnect, "str", lpszDirectory)
If (ErrorLevel or !r)
Return 0
else
Return 1
}
WININET_FtpRemoveDirectoryA(hConnect,lpszDirectory) {
;http://msdn.microsoft.com/en-us/library/aa384172(VS.85).aspx
r := DllCall("wininet\FtpRemoveDirectoryA", "uint", hConnect, "str", lpszDirectory)
If (ErrorLevel or !r)
Return 0
else
Return 1
}
WININET_FtpSetCurrentDirectoryA(hConnect,lpszDirectory) {
;http://msdn.microsoft.com/en-us/library/aa384178(VS.85).aspx
r := DllCall("wininet\FtpSetCurrentDirectoryA", "uint", hConnect, "str", lpszDirectory)
If (ErrorLevel or !r)
Return 0
else
Return 1
}
WININET_FtpPutFileA(hConnect,lpszLocalFile, lpszNewRemoteFile="", dwFlags=0,dwContext=0) {
;http://msdn.microsoft.com/en-us/library/aa384170(VS.85).aspx
If lpszNewRemoteFile =
lpszNewRemoteFile := lpszLocalFile
r := DllCall("wininet\FtpPutFileA"
, "uint" , hConnect
, "str" , lpszLocalFile
, "str" , lpszNewRemoteFile
, "uint" , dwFlags
, "uint" , dwContext )
If (ErrorLevel or !r)
Return 0
else
Return 1
}
WININET_FtpGetFileA(hConnect,lpszRemoteFile, lpszNewFile="", fFailIfExists=1
,dwFlagsAndAttributes=0,dwFlags=0,dwContext=0) {
;http://msdn.microsoft.com/en-us/library/aa384157(VS.85).aspx
If lpszNewFile=
lpszNewFile := lpszRemoteFile
r := DllCall("wininet\FtpGetFileA"
, "uint" , hConnect
, "str" , lpszRemoteFile
, "str" , lpszNewFile
, "int" , fFailIfExists
, "uint" , dwFlagsAndAttributes
, "uint" , dwFlags
, "uint" , dwContext )
If (ErrorLevel or !r)
Return 0
else
Return 1
}
WININET_FtpOpenFileA(hConnect,lpszFileName,dwAccess=0x80000000,dwFlags=0,dwContext=0){
;http://msdn.microsoft.com/en-us/library/aa384166(VS.85).aspx
Result := DllCall("wininet\FtpOpenFileA"
, "uint" , hConnect
, "str" , lpszFileName
, "uint" , dwAccess
, "uint" , dwFlags
, "uint" , dwContext ) ;dwContext
If (ErrorLevel or !r)
Return -1
Return Result
}
WININET_InternetCloseHandle(hInternet){
DllCall("wininet\InternetCloseHandle"
, "UInt" , hInternet )
}
WININET_FtpGetFileSize(hFile,lpdwFileSizeHigh=0) {
;http://msdn.microsoft.com/en-us/library/aa384159(VS.85).aspx
return DllCall("wininet\FtpGetFileSize"
, "uint" , hFile
, "uint" , lpdwFileSizeHigh )
}
WININET_FtpDeleteFileA(hConnect,lpszFileName) {
;http://msdn.microsoft.com/en-us/library/aa384142(VS.85).aspx
r := DllCall("wininet\FtpDeleteFileA"
, "uint" , hConnect
, "str" , lpszFileName)
If (ErrorLevel or !r)
Return 0
else
Return 1
}
WININET_FtpRenameFileA(hConnect,lpszExisting, lpszNew) {
;http://msdn.microsoft.com/en-us/library/aa384175(VS.85).aspx
r := DllCall("wininet\FtpRenameFileA"
, "uint" , hConnect
, "str" , lpszExisting
, "str" , lpszNew )
If (ErrorLevel or !r)
Return 0
else
Return 1
}
WININET_FindFirstFile(hConnect, lpszSearchFile, ByRef lpFindFileData,dwFlags=0,dwContext=0) {
;http://msdn.microsoft.com/en-us/library/aa384146(VS.85).aspx
SIZE_OF_WIN32_FIND_DATA := ( 4 + (3*8) + (4*4) + (260*4) + (14*4) )
VarSetCapacity(lpFindFileData, SIZE_OF_WIN32_FIND_DATA, 0)
hFind := DllCall("wininet\FtpFindFirstFileA"
, "uint" , hConnect
, "str" , lpszSearchFile
, "uint" , &lpFindFileData
, "uint" , dwFlags
, "uint" , dwContext )
If(!hFind)
VarSetCapacity(lpFindFileData, 0)
Return hFind
}
WININET_FindNextFile(hFind, ByRef lpvFindData) {
;http://msdn.microsoft.com/en-us/library/aa384698(VS.85).aspx
Return DllCall("wininet\InternetFindNextFileA"
, "uint", hFind
, "uint", &lpvFindData)
}
UrlGetContents():
UrlGetContents(sUrl,sUserName="",sPassword="",sPostData="",sUserAgent="Autohotkey"){
; based on heresy & DerRaphael's script here:
; http://www.autohotkey.com/forum/viewtopic.php?t=33506
;default ports
INTERNET_INVALID_PORT_NUMBER = 0
INTERNET_DEFAULT_HTTP_PORT = 80
INTERNET_DEFAULT_HTTPS_PORT = 443
;ssl flags
INTERNET_FLAG_SECURE = 0x00800000
SECURITY_FLAG_IGNORE_UNKNOWN_CA = 0x00000100
SECURITY_FLAG_IGNORE_CERT_CN_INVALID = 0x00001000
SECURITY_FLAG_IGNORE_CERT_DATE_INVALID = 0x00002000
SplitPath,sURL,sFileName,sPath,sExt,sNoExt,sServer
sProtocol := (RegExMatch(sServer,"(^http://|^ftp://|^https://)",pr)) ? pr1 : "unknown"
if (sProtocol = "http://"){
nPort := INTERNET_DEFAULT_HTTP_PORT
dwFlags = 0
} else if (sProtocol = "https://") {
nPort := INTERNET_DEFAULT_HTTPS_PORT
dwFlags := (INTERNET_FLAG_SECURE|SECURITY_FLAG_IGNORE_CERT_CN_INVALID
|SECURITY_FLAG_IGNORE_CERT_DATE_INVALID|SECURITY_FLAG_IGNORE_UNKNOWN_CA)
} else {
nPort := INTERNET_INVALID_PORT_NUMBER
dwFlags = 0
}
sPath := RegExReplace(sPath,"\Q" sServer "\E")
sServerName := (sProtocol!="unknown") ? RegExReplace(sServer,"\Q" sProtocol "\E") : sServer
sObjectName := sPath . "/" . sFileName
hInternet := WININET_InternetOpenA(sUserAgent)
hConnect := WININET_InternetConnectA(hInternet,sServerName,nPort,sUserName,sPassword)
if sPostData
sHTTPVerb:="POST", sHeaders:="Content-Type: application/x-www-form-urlencoded"
else
sHTTPVerb:="GET", sHeaders:=""
sVersion:="HTTP/1.1", sReferer:="", sAcceptTypes:=""
hRequest := WININET_HttpOpenRequestA(hConnect,sHTTPVerb,sObjectName,sVersion,sReferer,sAcceptTypes,dwFlags)
if WININET_HttpSendRequestA(hRequest,sHeaders,sPostData)
sData := WININET_InternetReadFile(hRequest)
WININET_InternetCloseHandle(hRequest)
WININET_InternetCloseHandle(hInternet)
WININET_InternetCloseHandle(hConnect)
return sData
}A couple test scripts:
;Get Web Page Contents Test
WININET_Init()
MsgBox % UrlGetContents("http://www.autohotkey.net")
WININET_UnInit()
return
;FTP Upload-Download Test
WININET_Init()
OnExit, CleanUp
sHost = autohotkey.net
nPort = 21
sUsername = YourUserName
sPassword = YourPassword
sLocalFile = C:\test_word_document.doc
sRemoteFile = /test_word_document.doc
sNewLocalFile = C:\new_test_word_document.doc
fFailIfExists := True ; False to overwrite existing file on download
INTERNET_OPEN_TYPE_DIRECT = 1
INTERNET_SERVICE_FTP = 1
; establish connection to FTP server
if hInternet := WININET_InternetOpenA(A_ScriptName,INTERNET_OPEN_TYPE_DIRECT)
hConnect := WININET_InternetConnectA(hInternet,sHost,nPort,sUserName,sPassword,INTERNET_SERVICE_FTP)
If (ErrorLevel or !hConnect) {
MsgBox, Error connecting to FTP server.
ExitApp
}
MsgBox Connected to Server
;Upload File
if !WININET_FtpPutFileA(hConnect,sLocalFile, sRemoteFile){
MsgBox, Error uploading file
ExitApp
}
MsgBox Upload Complete
; download file
if !WININET_FtpGetFileA(hConnect,sRemoteFile,sNewLocalFile, fFailIfExists){
MsgBox, Error downloading file
ExitApp
}
MsgBox Download Complete
ExitApp
CleanUp:
MsgBox Exiting.
WININET_InternetCloseHandle(hConnect)
WININET_InternetCloseHandle(hInternet)
WININET_UnInit()
ExitAppapi
/* api for wininet.ahk
WININET_FindNextFile( hFind, lpvFindData )
WININET_FindFirstFile( hConnect, lpszSearchFile, lpFindFileData [ , dwFlags, dwContext ] )
WININET_FtpRenameFileA( hConnect, lpszExisting, lpszNew )
WININET_FtpDeleteFileA( hConnect, lpszFileName )
WININET_FtpGetFileSize( hFile [ , lpdwFileSizeHigh ] )
WININET_InternetCloseHandle( hInternet )
WININET_FtpOpenFileA( hConnect, lpszFileName [ , dwAccess, dwFlags, dwContext ] )
WININET_FtpGetFileA( hConnect, lpszRemoteFile [ , lpszNewFile, fFailIfExists, dwFlagsAndAttributes, dwFlags, dwContext ] )
WININET_FtpPutFileA( hConnect, lpszLocalFile [ , lpszNewRemoteFile, dwFlags, dwContext ] )
WININET_FtpSetCurrentDirectoryA( hConnect, lpszDirectory )
WININET_FtpRemoveDirectoryA( hConnect, lpszDirectory )
WININET_FtpCreateDirectoryA( hConnect, lpszDirectory )
WININET_InternetReadFile( hFile )
WININET_HttpSendRequestA( hRequest [ , lpszHeaders, lpOptional ] )
WININET_HttpOpenRequestA( hConnect, lpszVerb, lpszObjectName [ , lpszVersion, lpszReferer, lplpszAcceptTypes, dwFlags, dwContext ] )
WININET_InternetConnectA( hInternet, lpszServerName [ , nServerPort, lpszUsername, lpszPassword, dwService, dwFlags, dwContext ] )
WININET_InternetOpenA( lpszAgent [ , dwAccessType, lpszProxyName, lpszProxyBypass, dwFlags ] )
WININET_UnInit( )
WININET_Init( )
*/