wininet.dll, lastdllerror=6, what does it mean?

  • Thread starter Sjaakie Helderhorst
  • Start date
S

Sjaakie Helderhorst

Hi all,
I'm trying to create a class which handles FTP. Knowing little of classes
I'm having some trouble making things work. Also having trouble finding the
meaning of some error-codes. The code below results in a lastdllerror 6 (and
no transfer)... can't find what this error means and how to solve it (yes I
already tried google). Could someone please help me out?

This code should, on button-click, connect to ftp-server (192.168.20.1), get
a file 'test.txt' from its root and store this file into 'c:\temp\test.txt'.
Result-string to 'label1'.
I created a class 'Ftp' to achieve this, without any luck sofar...

Thanks in advance!

### [ Class ]
Public Class Ftp

Private Declare Function InternetCloseHandle Lib "wininet" (ByRef hInet
As Long) As Long

Private 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

Private 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

Private Declare Function FtpGetFile Lib "wininet.dll" Alias
"FtpGetFileA" (ByVal hConnect As Long, ByVal lpszRemoteFile As String, ByVal
lpszNewFile As String, ByVal fFailIfExists As Long, ByVal
dwFlagsAndAttributes As Long, ByVal dwFlags As Long, ByRef dwContext As
Long) As Boolean

Private Declare Function FtpPutFile Lib "wininet.dll" Alias
"FtpPutFileA" (ByVal hConnect As Long, ByVal lpszLocalFile As String, ByVal
lpszNewRemoteFile As String, ByVal dwFlags As Long, ByVal dwContext As Long)
As Boolean

Private Declare Function InternetGetLastResponseInfo Lib "wininet.dll"
Alias "InternetGetLastResponseInfoA" (ByVal lpdwError As Long, ByVal
lpszBuffer As String, ByVal lpdwBufferLength As Long) As Boolean

Dim strServer, strLogin, strPassword As String
Dim INet, INetConn As Long

Property Server() As String
Get
Return strServer
End Get
Set(ByVal Value As String)
strServer = Value
End Set
End Property

Property Login() As String
Get
Return strLogin
End Get
Set(ByVal Value As String)
strLogin = Value
End Set
End Property

Property Password() As String
Get
Return strPassword
End Get
Set(ByVal Value As String)
strPassword = Value
End Set
End Property

Public Function showError()
Dim lErr As Long, sErr As String, lenBuf As Long
InternetGetLastResponseInfo(lErr, sErr, lenBuf)
sErr = lenBuf.ToString
InternetGetLastResponseInfo(lErr, sErr, lenBuf)
Return ("Error " + CStr(lErr) + ": " + sErr)
End Function

Public Sub openConnection()
INet = InternetOpen("AceGroup FTP", 0, vbNullString, vbNullString,
0)
INetConn = InternetConnect(INet, Server, 21, Login, Password, 1, 0,
0)
End Sub

Public Sub closeConnection()
InternetCloseHandle(INetConn)
InternetCloseHandle(INet)
INet = 0
INetConn = 0
End Sub

Public Function putFile(ByVal localFile As String, ByVal remoteFile As
String) As String
Dim result As String
Try
If INet = 0 Then openConnection()
result = FtpPutFile(INetConn, localFile, remoteFile, 0,
0).ToString
Catch ex As Exception
result = ex.ToString()
Finally
closeConnection()
End Try

Return result
End Function

Public Function getFile(ByVal remoteFile As String, ByVal localFile As
String) As String
Dim result As String
Try
If INet = 0 Then openConnection()
result = FtpGetFile(INetConn, remoteFile, localFile, 0, 0, 1,
0).ToString()
Catch ex As Exception
result = ex.ToString()
Finally
closeConnection()
End Try

Return result
End Function

End Class


### [Webform]
[ ... ]
Dim cFtp As Ftp = New Ftp
Dim Result As String

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

cFtp.Server = "192.168.20.1"
cFtp.Login = "[ my login ]"
cFtp.Password = "[ my pass ]"

cFtp.openConnection()
Result = cFtp.getFile("test.txt", "c:\temp\test.txt")
cFtp.closeConnection()
' output result to label
label1.text = Result
End Sub
 
H

Herfried K. Wagner [MVP]

* "Sjaakie Helderhorst said:
### [ Class ]
Public Class Ftp

Private Declare Function InternetCloseHandle Lib "wininet" (ByRef hInet
As Long) As Long

The declares are wrong. Replace the 'Long' with 'Int32', all parameters
with name 'h*' with 'IntPtr'.
 
T

Tom Shelton

Hi all,
I'm trying to create a class which handles FTP. Knowing little of classes
I'm having some trouble making things work. Also having trouble finding the
meaning of some error-codes. The code below results in a lastdllerror 6 (and
no transfer)... can't find what this error means and how to solve it (yes I
already tried google). Could someone please help me out?

This code should, on button-click, connect to ftp-server (192.168.20.1), get
a file 'test.txt' from its root and store this file into 'c:\temp\test.txt'.
Result-string to 'label1'.
I created a class 'Ftp' to achieve this, without any luck sofar...

Thanks in advance!

### [ Class ]
Public Class Ftp

Private Declare Function InternetCloseHandle Lib "wininet" (ByRef hInet
As Long) As Long

' Handles should be IntPtr, Make this return boolean
Private Declare Function InternetCloseHandle Lib "wininet" _
(ByVal hInternet As System.IntPtr) As Boolean
Private 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

' loose the alias - use auto, change longs to integers, change
' handles to be intptr, integers to short. I also usually create
' an enum for flags
Private Enum Services
Ftp = ...
Gopher = ...
Http = ...
End Enum

Private Enum ServiceFlags
...
End Enum

Private Declare Auto Function InternetConnect Lib "wininet" _
(ByVal hInternet As System.IntPtr, _
ByVal lpszServerName As String, _
ByVal nServerPort As Short, _
ByVal lpszUserName As String, _
ByVal lpszPassword As String, _
ByVal dwService As Services, _
ByVal dwFlags As ServiceFlags, _
ByVal lContext As Integer) As System.IntPtr


Anyway, that basic problems are you are using Longs where you should be
using Integers. Longs are 64-bit in .NET...

Long = 64-bit
Integer = 32-bit
Short = 16-bit


Also, you should not use the Alias's. You should drop them and use the
Auto modifier instead. This lets the runtime determine the proper
function to call based on the OS. Further, any handles (such as hWnd,
hInternet, etc) should be declared as IntPtr (System.IntPtr).

As for finding out what the error means... Here is a little function
that may help you out:

' get this for Win32Exception
Imports System.ComponentModel


Public Function GetWin32ErrorMsg(ByVal ErrorCode As Integer) As String
Dim ex As New Win32Exception(ErrorCode)
Return ex.Message
End Function

I have do have a FTP class I wrote using WinInet, if you would like to
look at it I will post it. The code is C#, but it is a fairly straight
forward conversion into VB.NET
 
S

Sjaakie Helderhorst

I have do have a FTP class I wrote using WinInet, if you would like to
look at it I will post it. The code is C#, but it is a fairly straight
forward conversion into VB.NET

--
Tom Shelton [MVP]
Powered By Gentoo Linux 1.4
Reality is bad enough, why should I tell the truth?
-- Patrick Sky

Thanks for your reply
I used an example I found on the Internet, apparently not very useful :S
Having trouble making it work but I'll try to figure it out myself first
before asking you guys again.
I'm an ASP (not .NET) programmer so I'm very interested in you FTP class, I
think it will help me alot in programming and understanding with dotNET.
Could you please e-mail me the class? (alter my e-mail address)

Thanks again!
 
S

Sjaakie Helderhorst

Herfried K. Wagner said:
* "Sjaakie Helderhorst said:
### [ Class ]
Public Class Ftp

Private Declare Function InternetCloseHandle Lib "wininet" (ByRef hInet
As Long) As Long

The declares are wrong. Replace the 'Long' with 'Int32', all parameters
with name 'h*' with 'IntPtr'.

Thanks!
 
S

Sjaakie Helderhorst

Cor said:
Hallo Sjaakie,

Kudzu has been active in this newsgroup, I read in other newsgroup that this
is for Delphi very good stuff.

Indy now includes FTP, NNTP, SMTP, POP3 and more for .Net.
http://www.indyproject.org/indy.html
I hope this helps?

Cor

Thanks for the link... I'll have a look.
But there's something about 'finding it out yourself' :)
These 'prefab' projects are nice, but they teach me little about how things
are done in the code behind these components.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top