| |
WinInet is a .dll (acrostic for dynamic link library) library that ships with Microsoft's browser, and presumeably is what
is used by the Internet Explorer browser to 'surf the web'.
It's a layer built upon the basic winsock, which can be used to send the same ftp commands, directly (and allowing for
whatever timing problems in file transfer, etc).
But WinInet is more powerful, and does simplify matters.
And as with FTP (or Microsoft's FSO, file system object), or most any other ActiveX, library or file,
there is the risk of any or all being compromised by outside programs looking to corrupt such
core libraries and files.
Wininet.dll is no exception.
Also, it does require that the server recognize the wininet.dll routines.
At least one knows that this is true for servers running NT 4 and Win 2000, and greater.
That said:
|
Public Const INTERNET_OPEN_TYPE_PRECONFIG = 0
Public Const INTERNET_OPEN_TYPE_DIRECT = 1
Public Const INTERNET_OPEN_TYPE_PROXY = 3
Public Const INTERNET_INVALID_PORT_NUMBER = 0
Public Const FTP_TRANSFER_TYPE_BINARY = &H2
Public Const FTP_TRANSFER_TYPE_ASCII = &H1
Public Const INTERNET_FLAG_PASSIVE = &H8000000
Public Const INTERNET_SERVICE_FTP = 1
Public Declare Function FtpPutFile Lib "wininet.dll" Alias "FtpPutFileA" _
(ByVal hFtpSession As Long, _
ByVal lpszLocalFile As String, _
ByVal lpszRemoteFile As String, _
ByVal dwFlags As Long, _
ByVal dwContext As Long) As Boolean
Public Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" _
(ByVal sAgent As String, _
ByVal lAccessType As Long, _
ByVal sProxyName As String, _
ByVal sProxyBypass As String, _
ByVal lFlags As Long) As Long
Public Declare Function InternetConnect Lib "wininet.dll" Alias "InternetConnectA" _
(ByVal hInternetSession As Long, _
ByVal sServerName As String, _
ByVal nServerPort As Integer, _
ByVal sUsername As String, _
ByVal sPassword As String, _
ByVal lService As Long, _
ByVal lFlags As Long, _
ByVal lContext As Long) As Long
Public Declare Function InternetCloseHandle Lib "wininet.dll" _
(ByVal hInet As Long) As Integer
' WinInet FTP file upload
' strFTPserver - FTP address, without the leading "ftp://" protocol - e.g. "ftp.microsoft.com"
' strFTPpath - from any home or root directory, down to the desired directory - e.g. "webroot/catalogs/photos/"
' (trailing slash makes no difference)
' strLocalPath - the full drive:directories path to the file to be uploaded
' strFilename - just the full name of the file, i.e. filename . extension - e.g. "vacation2.jpg"
' strUser - the user name you selected or were given to log into the ftp server
' strPW - the password you need to log into the ftp server
' boolImg - true is this is a binary file, e.g. a photo/image, and false if it's a text file, like an html page
Public Function fUploadFTP(strFTPserver As String, strFTPpath As String, strLocalPath As String, _
strFilename As String, strUser As String, strPW As String, boolImg As Boolean)
Dim lngFlag As Long, lngFTPtype As Long
Dim hFTPopen As Long, hFTPconnection As Long
Dim boolRtn As Boolean
lngFTPtype = IIf(boolImg, FTP_TRANSFER_TYPE_BINARY, FTP_TRANSFER_TYPE_ASCII)
hFTPopen = InternetOpen(strUserAgent, INTERNET_OPEN_TYPE_DIRECT, vbNullString, vbNullString, 0)
hFTPconnection = InternetConnect(hFTPopen, strFTPserver, INTERNET_INVALID_PORT_NUMBER, _
strUser, strPW, INTERNET_SERVICE_FTP, lngFlag, 0)
If hFTPconnection = 0 Then
MsgBox "Could not connect to the FTP server"
Else
boolRtn = FtpPutFile(hFTPconnection, strLocalPath & "/" & strFilename, strFTPpath & strFilename, lngFTPtype, 0)
If boolRtn = False Then MsgBox "Unable to upload file"
End If
If hFTPconnection <> 0 Then InternetCloseHandle (hFTPconnection)
If hFTPopen <> 0 Then InternetCloseHandle (hFTPopen)
hFTPconnection = 0
hFTPopen = 0
fUploadFTP = boolRtn
End Function
|
And so this is the simple routine used to upload a file
to the ftp server, somewhere in the world.
Again, the first stage was to collect the data.
So that's settled.
But now there's no "strWrite", creating that script for FTP.EXE, and there's no waiting
on the window handle to see if it has closed.
The last path is 'calculated', and then it's right to this routine.
So while one can log on to the ftp directly, with InternetConnect,
and send username and password, and even change directories, etc.,
if one tried to send a file, a file name would appear on the server.
But it would be zero-length.
Another routine has to be called to set up an 'environment' to
handle buffers needed for file transfer.
And that's what the InternetOpen call is about.
In that way, perhaps, it's similar to merely starting the FPT.EXE session, without logging in.
Perhaps one could check for an error return, but here it is done after trying to
get the connection.
And once ready to transfer files, and once a connection is established,
what is here called, FtpPutFile, is then used to upload the file.
The routine returns a flag, true if the upload was successful.
And it shouldn't return to the next Visual BASIC line until it has completed, then (returning the flag).
So one doesn't need to get any process handle, as before.
Quite obviously, there is a lot more to wininet.dll.
It somewhat simplifies the previous process, which was pretty simple to begin with.
No extraneous local files on disk, etc.
If a problem develops on one's system with this, there is always the FTP.EXE throwback - assuming
that was unaffected by whatever problem.
Wininet is just a neater solution, which is under branching program control, the whole while.
|